I have a data frame with 1000 observations and 200 string variables that are named text.1, text.2, text.3, etc. I would like to create one variable that combines all of the text into one variable (with 1000 observations) named text. As an example, I can create the following data frame:
df <- data.frame(c("text1a","text1b","text1c"),c("text2a","text2b","text2c"),c("text3a","text3b","text3c"))
names(df) <- c('text.1','text.2','text.3')
What I want is: "text 1atext 2atext 3a" for observation 1, "text 1btext 2btext 3b" for observation 2, etc.
My idea was to create an empty variable (df$text) and loop through all the text variables (text.1-text.3) combining df$text with the text from the new variable. However, this code a) doesn't work, and b) looks clunky and inefficient. I've read that using assign or one of the apply functions might be preferable, but haven't been able to get that to work for my problem.
df$text <- ''
for (t in 1:3) {
df$text <- paste(df$text,eval(parse(text=paste("df$text",t,sep=""))))
}
Thank you in advance for your help!