Hello I have a DF with multiple columns all containing numeric values. My df contains over 200 columns but the sample should do. I would like to take the values from the list of indices and using them in a RowSums loop so that the list name is the new column and the sums are the combo of indices
Main <- c(rep(1, times = 6), rep(2, times = 6))
Feature1 <- sample(1:20, 12, replace = T)
Feature2 <- sample(400:500, 12, replace = T)
Feature3 <- sample(1:5, 12, replace = T)
df.main <- data.frame(Main, Feature1, Feature2,
Feature3, stringsAsFactors = FALSE)
Main Feature1 Feature2 Feature3
1 1 6 483 3
2 1 9 405 1
3 1 18 494 5
4 1 7 499 5
5 1 13 436 1
6 1 2 451 3
7 2 4 456 3
8 2 19 442 5
9 2 16 437 2
10 2 4 497 4
11 2 7 497 3
12 2 5 466 1
list(`Cool Ranch|Cool Chipotle` = c(1L, 4L,), `Trust|Scotia` = c(3L,
4L))
I want my output to look like this
Main Feature1 Feature2 Feature3 cool_ranch trust_scotia
1 1 6 483 3 4 486
2 1 9 405 1 2 406
3 1 18 494 5 6 499
4 1 7 499 5 6 504
5 1 13 436 1 2 437
6 1 2 451 3 4 454
7 2 4 456 3 5 459
8 2 19 442 5 7 447
9 2 16 437 2 4 439
10 2 4 497 4 6 501
11 2 7 497 3 5 500
12 2 5 466 1 3 467
I have tried a few things along the same lines as below
> sum.test<- apply(df.main, 2, function(i) rowSums[vlist.imps$i])
Error in rowSums[vlist.imps$i] :
object of type 'closure' is not subsettable
We can use loop over the 'vlist.imps', extract the columns of 'df.main' with those index, get the rowSums
and assign the output back to create new columns
df.main[names(vlist.imps)] <- lapply(vlist.imps, function(x) rowSums(df.main[x]))