user4797853 user4797853 - 1 year ago 40
R Question

Calling a list inside a rowSums

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 ')

l1 and l2 looks as follow:

1 300
2 314
3 400
4 500

1 100
2 324
3 540
4 360

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=",")

This what the paste function output:

> paste(new.variable1.v[1:9], sep="", collapse="," )
[1] "193,194,196,197,198,199,201,202,203"

My code doesn't output the desired results:

> DF3<[,c(d1_)]),GROUP2=rowSums(prep[,c(d2_)]))
Error in `[.data.frame`(prep, , c(d1_)) : undefined columns selected

Answer Source

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 l1 and 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() %>%

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).

  var1sums = rowSums(sampData[, var1])
  , var2sums = rowSums(sampData[, var2])

Of note, cat returns 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.