Mutating multiple columns in a data frame using dplyr

I have the following data frame df:

  v1 v2 v3 v4
1  1  5  7  4
2  2  6 10  3

And I want to get the following data frame by df2multiplying the columns v1 * v3 and v2 * v4:

  v1 v2 v3 v4 v1v3 v2v4
1  1  5  7  4    7   20
2  2  6 10  3   20   18

How to do this using dplyr? Using mutate_each?

I need a solution that can be generalized to a large number of variables , not just 4 (from v1 to v4). This is the code to generate the example:

v1 <- c(1, 2)
v2 <- c(5,6)
v3 <- c(7, 10)
v4 <- c(4, 3)
df <- data.frame(v1, v2, v3, v4)
v1v3 <- c(v1 * v3)
v2v4 <- c(v2 * v4)
df2 <- cbind(df, v1v3, v2v4)
+9
source share
4 answers

You are really close.

df2 <- 
    df %>% 
    mutate(v1v3 = v1 * v3,
           v2v4 = v2 * v4)

such a beautiful simple language, right?

For more great tricks, please see here.

: @Facottons : fooobar.com/questions/1238742/..., . . , Base R, , , /. , , .

# prep the product column names (also acting as row numbers)
df <- 
    df %>%
    mutate(prod_grp = paste0("v", row_number(), "v", row_number() + 2)) 

# converting data to tidy format and pairing columns to be multiplied together.
tidy_df <- 
    df %>%
    gather(column, value, -prod_grp) %>% 
    mutate(column = as.numeric(sub("v", "", column)),
           pair = column - 2) %>% 
    mutate(pair = if_else(pair < 1, pair + 2, pair))

# summarize the products for each column
prod_df <- 
    tidy_df %>% 
    group_by(prod_grp, pair) %>% 
    summarize(val = prod(value)) %>% 
    spread(prod_grp, val) %>% 
    mutate(pair = paste0("v", pair, "v", pair + 2)) %>% 
    rename(prod_grp = pair)

# put the original frame and summary frames together
final_df <- 
    df %>% 
    left_join(prod_df) %>% 
    select(-prod_grp)
+18

, :

df %>%
  mutate(n = df[1:(ncol(df)/2)] * df[(1+ncol(df)/2):(ncol(df))]) %>% head()

. . :

  v1 v2 v3 v4 n.v1 n.v2
1  1  5  7  4    7   20
2  2  6 10  3   20   18
+3

mutate , mutate(df,"v1v3"=v1*v3,"v2v4"= v2*v4)

+2
source

We can use base Rinstead of using any additional packages, such as dplyrordata.table

We can use mapplyto vectorize operations for several vectors simultaneously

n <- ncol(df)/2
mapply('*', df[1:n], df[(n + 1):ncol(df)])

#     v1 v2
#[1,]  7 20
#[2,] 20 18

Then we can combine ( cbind) this data frame with your original one.


If you are interested in a solution tidyversethen the equivalent purrrwill be optionsmap2

purrr::map2_df(df[1:n], df[(n + 1):ncol(df)], '*')

# A tibble: 2 x 2
#     v1    v2
#  <dbl> <dbl>
#1     7    20
#2    20    18
+2
source

Source: https://habr.com/ru/post/1660313/


All Articles