Concatenating Vector of Strings in R

sak

New Member
#1
I have a vector (Label) of length 500 with first few elements as:
AAA
ABM
ACD
ADR

I want to create a new vector (NewLabel) such that it's elements are:
AAA_trades.dat
ABM_trades.dat
ACD_trades.dat
ADR_trades.dat

basically add the string "_trades.dat" to each element of vector Label.

How do I do this?
 

Dason

Ambassador to the humans
#2
paste is probably what you're looking for:
Code:
ex.data <- c("AAA", "ABM", "ACD", "ADR")
paste(ex.data, "_trades.dat", sep = "")
 

sak

New Member
#3
paste is probably what you're looking for:
Code:
ex.data <- c("AAA", "ABM", "ACD", "ADR")
paste(ex.data, "_trades.dat", sep = "")
Sorry I should have mentioned this in my previous post. The Label vector (your c) is generated by reading from a file. So, I can't do the step:
ex.data <- c("AAA", "ABM", "ACD", "ADR")

So, I basically have a text file with 500 of these labels in them and I pull them in a vector.
 

Dason

Ambassador to the humans
#4
So what's the problem? I was just creating example data to show you that it works. Whatever you call the vector just replace the label ex.data with the name of your vector.
 

sak

New Member
#5
Sorry I am new to R and so my questions might be bit too naive.

Here is what I did to test my case:
Code:
c("a","b","c","d")
paste(c,"_data",sep="")
And I get the error:
Code:
Error in paste(c, "_data", sep = "") : 
  cannot coerce type 'builtin' to vector of type 'character'
 

Dason

Ambassador to the humans
#6
Just literally run the code I gave you.
Code:
c
is a function. You aren't saving that vector anywhere.
Code:
paste(c("a", "b", "c", "d"), "_data", sep = "")
will give you something because here you pass a vector as the first argument. When you just do what you did you were passing a function as the first argument. So try the code I gave you. Realize that ex.data is just the name that I assigned to that vector.
 

Dason

Ambassador to the humans
#8
Oh no problem. Sorry if I sounded a little abrasive in that last post - I just got back from a run and was a little tired out and wasn't thinking about what I was posting as much as I usually do. I'm glad I could help out and feel free to come back anytime and ask more questions. I love R and helping people learn to use the R language is something I really enjoy doing.