dplyr

How to use mutate_at() with two sets of variables, in R

*爱你&永不变心* 提交于 2021-02-07 20:13:48
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

How to use mutate_at() with two sets of variables, in R

痞子三分冷 提交于 2021-02-07 20:01:30
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

How to use mutate_at() with two sets of variables, in R

五迷三道 提交于 2021-02-07 20:00:54
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

How to use mutate_at() with two sets of variables, in R

旧街凉风 提交于 2021-02-07 20:00:51
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

R - use group_by() and mutate() in dplyr to apply function that returns a vector the length of groups

烂漫一生 提交于 2021-02-07 16:17:07
问题 Take the following example data: set.seed(1) foo <- data.frame(x=rnorm(10, 0, 10), y=rnorm(10, 0, 10), fac = c(rep("A", 5), rep("B", 5))) I want to split the dataframe "foo" by the variable "fac" into A's and B's, apply a function (mahalanobis distance) that returns a vector of the length of each subgroup, and then mutate the output back on to the original dataframe. For example: auto.mahalanobis <- function(x) { temp <- x[, c("x", "y")] return(mahalanobis(temp, center = colMeans(temp, na.rm

summarise logical dataframe with dplyr

坚强是说给别人听的谎言 提交于 2021-02-07 16:12:43
问题 I'm trying to summarise a dataframe using two variables - I basically want to break down variable 1 by variable 2 in order to plot the results in a 100% stacked bar chart. I have multiple columns of type logical, which can be split between two main categories that will be used to create the breakdown. I have tried to use gather from dplyr to transform the dataframe to longform, however the output is not what I expect. topics_by_variable <- function (dataset, variable_1, variable_2) { #select

summarise logical dataframe with dplyr

旧街凉风 提交于 2021-02-07 16:10:36
问题 I'm trying to summarise a dataframe using two variables - I basically want to break down variable 1 by variable 2 in order to plot the results in a 100% stacked bar chart. I have multiple columns of type logical, which can be split between two main categories that will be used to create the breakdown. I have tried to use gather from dplyr to transform the dataframe to longform, however the output is not what I expect. topics_by_variable <- function (dataset, variable_1, variable_2) { #select

summarise logical dataframe with dplyr

只愿长相守 提交于 2021-02-07 16:09:20
问题 I'm trying to summarise a dataframe using two variables - I basically want to break down variable 1 by variable 2 in order to plot the results in a 100% stacked bar chart. I have multiple columns of type logical, which can be split between two main categories that will be used to create the breakdown. I have tried to use gather from dplyr to transform the dataframe to longform, however the output is not what I expect. topics_by_variable <- function (dataset, variable_1, variable_2) { #select

R - use group_by() and mutate() in dplyr to apply function that returns a vector the length of groups

给你一囗甜甜゛ 提交于 2021-02-07 16:05:43
问题 Take the following example data: set.seed(1) foo <- data.frame(x=rnorm(10, 0, 10), y=rnorm(10, 0, 10), fac = c(rep("A", 5), rep("B", 5))) I want to split the dataframe "foo" by the variable "fac" into A's and B's, apply a function (mahalanobis distance) that returns a vector of the length of each subgroup, and then mutate the output back on to the original dataframe. For example: auto.mahalanobis <- function(x) { temp <- x[, c("x", "y")] return(mahalanobis(temp, center = colMeans(temp, na.rm

How to correctly use group_by() and summarise() in a For loop in R

百般思念 提交于 2021-02-07 13:34:55
问题 I'm trying to calculate some summary information to help me check for outliers in different groups in a dataset. I can get the sort of output I want using dplyr::group_by() and dplyr::summarise() - a dataframe with summary information for each group for a given variable. Something like this: Sepal.Length_outlier_check <- iris %>% dplyr::group_by(Species) %>% dplyr::summarise(min = min(Sepal.Length, na.rm = TRUE), max = max(Sepal.Length, na.rm = TRUE), median = median(Sepal.Length, na.rm =