0

I have a large dataframe which is taking me away from my comfort tidyverse tools. I have one column in my data frame that I need to multiply the others by. How would I do this with data.table?

For example I have the following toy data:

multiplier a1  a2  
1          1   2    
2          1   2    
3          1   2   

And the desired result

   multiplier a1  a2  
    1          1   2    
    2          2   4    
    3          3   6   

I dplyr I would gather the as then multiply, then finally spread, but I am getting memory issues. How would I multiply the muliplier column by each row in data.table

Alex
  • 2,603
  • 4
  • 40
  • 73
  • 4
    Your problem is that you don't know base R. You don't need neither `gather` or `spread` or what ever. Neither you need data.table. You can easily achieve your desired output by simply doing `df[-1] <- df[-1] * df[, 1]` – David Arenburg Apr 18 '18 at 15:55
  • Maybe you could post code that creates an example big enough for memory to be a concern... If you really want to do it this way (with wide data ... but not a matrix) there's this similar question: https://stackoverflow.com/q/16846380 just switch `-1` to `multiplier`, I guess. – Frank Apr 18 '18 at 15:58

2 Answers2

1

You can do this without spreading the data:

my_data %>% 
  mutate_at(c("a1", "a2"), funs(. * multiplier))

# A tibble: 3 x 3
#   multiplier    a1    a2
#        <int> <int> <int>
# 1          1     1     2
# 2          2     2     4
# 3          3     3     6

Data

my_data <- tibble(multiplier = 1:3, 
                  a1 = c(1L, 1L, 1L), 
                  a2 = c(2L, 2L, 2L))
kath
  • 7,624
  • 17
  • 32
1

Based on David Arenburg base R can be very fast. Using his example above, you get the same output without installing any libraries:

multiplier = 1:3 
a1 = c(1, 1, 1)
a2 = c(2, 2, 2)
data <- data.frame(multiplier,a1,a2)

data1<-data

Option 1

data[,2:3] <- data[,2:3] * data[, 1]

Option 2

data1[,2:nrow(data1)] <- data1[,2:nrow(data1)] * data1[, 1]

Output:

data
data1

  multiplier a1 a2
1          1  1  2
2          2  2  4
3          3  3  6
Kill3rbee Lee Mtoti
  • 246
  • 1
  • 2
  • 11
  • This answered my `data.table` question. – Alex Apr 18 '18 at 16:22
  • 2
    So why not just `df[-1] <- df[-1] * df[, 1]` like in the comment? – David Arenburg Apr 18 '18 at 22:29
  • Not all people understand base R. `[…]` is a general subscripting operator that can be confusing to understand. So I just wanted to give Alex a different way of understanding the operator. Someone looking at `df[-1]` may not know that it means to remove first column – Kill3rbee Lee Mtoti Apr 19 '18 at 05:20