I would like to create a text string from a dataframe, but specify which rows will add to the text string.
Here is an example dataframe:
x <- as.data.frame(matrix(nrow = 4, ncol = 2))
x$V1 <- c(1, 1, 1, 2)
x$V2 <- c("aa", "bb", "cc", 1)
x$V3 <- c(1, 2, 3, NA
I only want to build the text strings where x$V1 = 1.
The result I am looking for is something like "aa 1 bb 2 cc 3", where the row where x$V1 = 2 is ignored for the building of the text string.
I have tried the following:
x$V4 <- for(i in 1:length(x$V1)){
if (x[i, 1] == 1){
paste(x[i,2], x[i,3])
} else {""}
}
paste(x$V3, collapse = "")
The above code does not even create a V4 column in my dataframe.
Thanks for the help.
NA?