Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of other columns as arguments to function in summarize_if()

Tags:

r

dplyr

This works great (see it as a solution for using list() instead of vars() here):

mtcars %>% 
  group_by(cyl) %>% 
  summarize_at(vars(disp, hp), list(~weighted.mean(., wt)))

However, in a very similar situation using summarize_if(), it does not work:

mtcars %>% 
  group_by(cyl) %>% 
  summarize_if(is.numeric, list(~weighted.mean(., wt)))

Error in weighted.mean.default(., wt) : 
  'x' and 'w' must have the same length

Why?

like image 211
Kevin Avatar asked May 14 '20 18:05

Kevin


People also ask

How do I apply a function across a column in R?

apply() lets you perform a function across a data frame's rows or columns. In the arguments, you specify what you want as follows: apply(X = data. frame, MARGIN = 1, FUN = function.

How do I reference multiple columns in R?

R – Get Multiple Columns of Matrix To get multiple columns of matrix, specify the column numbers as a vector preceded by a comma, in square brackets, after the matrix variable name. This expression returns the required columns as a matrix.

How does Group_by work in R?

The group_by() function in R is from dplyr package that is used to group rows by column values in the DataFrame, It is similar to GROUP BY clause in SQL. R dplyr groupby is used to collect identical data into groups on DataFrame and perform aggregate functions on the grouped data.


1 Answers

I believe this has to do with what you are naming this new variable. This works:

mtcars %>% 
     group_by(cyl) %>% 
     summarize_if(is.numeric, list(tmp = ~weighted.mean(., wt)))

See the naming section here and issues that have been noted here for more details.

like image 68
CzechInk Avatar answered Oct 06 '22 20:10

CzechInk