Reputation: 570
I am very new to R.
I have a raw data set with approximately 300 columns. The column names are similar to: "Image on Cover of Book", "Title on Cover of Book", "Author on Cover of Book", "Cover of Book", "Title", "Author", "Dustjacket Blurb", "Dustjacket Author", "Dustjacket Summary", "Summary on Front of Book", "Back of Book Reviews", "Price"
I want to concatenate all the text on the cover of the book into a new column named 'Cover'. New columns could be added later and I want it to be automated a possible. I figured out how to get grep to output which columns have the word "Cover" in them, but I cannot figure out how to concatenate them together.
cdf<- names(rawdata) #column name data frame
cols<-grep("Cover",cdf) #returns column numbers that have the word Cover in them.
paste(rawdata[c(cols)],sep=" ")
A lot of topics on here talked about using paste, but I cannot seem to get the syntax correct. It is most likely some basic misunderstanding on my part, but I appreciate any and all help you can give.
Upvotes: 1
Views: 519
Reputation: 1664
Here is a version with apply()
rawdata = matrix(c(1:20), ncol=5)
colnames(rawdata) = c("Cover_a", "Cover_b", "c", "d", "ee")
rawdata[,2] = "some text"
cdf <- colnames(rawdata) #column name data frame
cols <- grep("Cover",cdf) #returns column numbers that have the word Cover in them.
apply(rawdata[,cols], 1, paste, collapse=" ")
Upvotes: 1