How can I manipulate dataframe columns with different values from an external vector (with dplyr)

dpprdan

In R, I would like to manipulate (say multiply) data.frame columns with appropriately named values stored in a vector (or data.frame, if that's easier).

Let's say, I want to first summarise the variables disp, hp, and wt from the mtcars dataset.

vars <- c("disp", "hp", "wt")
mtcars %>% 
  summarise_at(vars, funs(sum(.))

(throw a group_by(cyl) into the mix, or use mutate_at if you'd like to have more rows)

Now I'd like to multiply each of the resulting columns with a particular value, given by

multiplier <- c("disp" = 2, "hp" = 3, "wt" = 4)

Is it possible to refer to these within the summarise_at function?

The result should look like this (and I don't want to have to refer to the variable names directly while getting there):

disp    hp    wt
14766.2 14082 411.808

UPDATE:

Maybe my MWE was too minimal. Let's say I want to do the same operation with a data.frame grouped by cyl

mtcars %>% 
  group_by(cyl) %>% 
  summarise_at(vars, sum) 

The result should thus be:

    cyl   disp   hp      wt
1     4 2313.0 2727 100.572
2     6 2566.4 2568  87.280
3     8 9886.8 8787 223.956

UPDATE 2:

Maybe I was not explicit enough here either, but the columns in the data.frame should be multiplied by the respective values in the vector (and only those columns mentioned in the vector), so e.g. disp should be multiplied by 2, hp by 3 and wt by 4, all other variables (e.g. cyl) should remain untouched by the multiplication.

akrun

We could also do this with map function from purrr

library(purrr)
mtcars %>%
    summarise_at(vars, sum) %>%
    map2_df(multiplier, `*`)
#      disp    hp      wt
#     <dbl> <dbl>   <dbl>
# 1 14766.2 14082 411.808

For the updated question

d1 <- mtcars %>% 
         group_by(cyl) %>% 
         summarise_at(vars, sum) 
d1 %>% 
   select(one_of(vars)) %>% 
   map2_df(multiplier[vars], ~ .x * .y) %>%
   bind_cols(d1 %>% select(-one_of(vars)), .) 
#    cyl   disp    hp      wt
#  <dbl>  <dbl> <dbl>   <dbl>
#1     4 2313.0  2727 100.572
#2     6 2566.4  2568  87.280
#3     8 9886.8  8787 223.956

Or we can use gather/spread

library(tidyr)
mtcars %>% 
    group_by(cyl) %>% 
    summarise_at(vars, sum) %>% 
    gather(var, val, -cyl) %>% 
    mutate(val = val*multiplier[match(var, names(multiplier))]) %>% 
    spread(var, val)
#     cyl   disp    hp      wt
#   <dbl>  <dbl> <dbl>   <dbl>
#1     4 2313.0  2727 100.572
#2     6 2566.4  2568  87.280
#3     8 9886.8  8787 223.956

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I split columns into a different vector

How can I insert values from a list to DataFrame columns?

How can I transform a dataframe by putting different values of a column into different columns?

How can I check in a pandas dataframe for a different values in different columns at the same time?

How can I groupby a DataFrame at the same time I count the values and put in different columns?

How can I unnest a vector from a dataframe?

How to remove columns in a dataframe in R based on the values from another vector?

Manipulate values of a subset of dataframe columns

How can I save only those rows from a DataFrame which columns have values from the list?

How do I rearrange values in different columns in an R dataframe

How can I merge results to a single dataframe after exctracting columns from different excel sheets?

Oracle 11g how I can group values from two different columns

In R, how can I match the first 3 characters of values from 2 different columns

Oracle SQL How can I separate values from a column in two different columns?

How can I conditionally sum values from different columns after aggregation?

How can I subtract values of each column of my dataframe from all other columns?

How do I create a new column for my dataframe whose values are maps made up of values from different columns?

How can I create a column in an actual dataframe by indexing another dataframe using the values in two columns from the actual dataframe

How can I insert a column into a dataframe if the column values come from a different file?

How can I count the amount of values in different columns in oracle plsql

How to use mutate from dplyr to create a series of columns defined and called by a vector specifying values for mutation?

How do I manipulate my dataframe to keep the columns with matching rownames?

How can I simplify adding columns with certain values to my dataframe?

How can I insert actual values in dataframe as columns in R?

Count values from different columns of a dataframe

Extract certain values from different columns in dataframe

How I can changes the values of multiple columns according to a condition of other columns in R using dplyr?

How can I separate a row character vector into unknown number of columns in R using dplyr?

How can I subtract specific values with different observations in the same Dataframe