I am trying to create a new data frame with 2 columns: GROUP1 and GROUP2, each one of them is the sum of specific columns in data frame prep. l1 is a table with one column holding column indexes as well as l2.
l1<-sqldf('select ID2 from DFT where part$cluster=1 ')
l2<-sqldf('select ID2 from DFT where part$cluster=2 ')
new.variable1.v <- l1$ID2
new.variable2.v <- l2$ID2 # here I am changing to a vector
d1_<-paste(new.variable1.v[1:9], sep="", collapse="," )
d2_<-paste(new.variable2.v[1:11], sep="", collapse=",")
> paste(new.variable1.v[1:9], sep="", collapse="," )
Error in `[.data.frame`(prep, , c(d1_)) : undefined columns selected
I think I have managed to figure out what you are asking, but if I am wrong, let me know.
You are trying to select columns from
prep that match the values in
l2, and sum across the rows, limited to the columns that matched each.
It is always better to provide reproducible data, here is some for this case (using
dplyr to build it):
sampData <- rnorm(260) %>% matrix(ncol = 26) %>% data.frame() %>% setNames(LETTERS) var1 <- c("A", "B", "C") var2 <- c("D", "E", "F", "G")
Then, you don't need to concatenate the column indices at all -- just use the variable (or column, in your case) directly. Here, I have made the ID's letters and will match the letters. However, if your ID's are numeric, it will match that index (e.g.,
3 will return the third column).
data.frame( var1sums = rowSums(sampData[, var1]) , var2sums = rowSums(sampData[, var2]) )
NULL after printing to the screen. If you need to concatenate values, you will need to use
paste (or similar), but that will not work for what you are trying to do here.