mutate

How to use the name of the column I'm currently using in a mutate_at?

不问归期 提交于 2021-02-10 14:12:05
问题 I have a datatable with missing data and two columns supposed to help to replace those missing data which looks like : library(data.table) Data = data.table( "H1" = c(NaN,4,NaN), "H2" = c(5,NaN,NaN), "H3" = c(7,NaN,NaN), "Group" = c(1,2,1), "Factor" = c(2,3,4) ) H1 H2 H3 Group Factor 1: NaN 5 7 1 2 2: 4 NaN NaN 2 3 3: NaN NaN NaN 1 4 I would like to use a second dataframe for that Groups = data.table( "H1" = c(1,2,3), "H2" = c(4,5,6), "H3" = c(7,8,9), "Group" = c(1,2,3) ) H1 H2 H3 Group 1: 1

Access the column names in the `mutate_at` to use it for subseting a list

非 Y 不嫁゛ 提交于 2021-02-09 13:58:33
问题 I am trying to recode several variables but with different recode schemes. The recoding scheme is saved in a list where each element is a named vector of the form old = new . Each element is the recoding scheme for each variable in the data frame I am using the mutate_at function and the recode . I think that the problem is that I cannot extract the variable name to use it to get the correct recoding scheme from the list I tried deparse(substitute(.)) as in here and also this didn;t help Also

Access the column names in the `mutate_at` to use it for subseting a list

拟墨画扇 提交于 2021-02-09 13:57:29
问题 I am trying to recode several variables but with different recode schemes. The recoding scheme is saved in a list where each element is a named vector of the form old = new . Each element is the recoding scheme for each variable in the data frame I am using the mutate_at function and the recode . I think that the problem is that I cannot extract the variable name to use it to get the correct recoding scheme from the list I tried deparse(substitute(.)) as in here and also this didn;t help Also

R dplyr mutate_at accessing colnames

你离开我真会死。 提交于 2021-02-08 05:21:26
问题 How could one access the column name being processed by dplyr::mutate_at ? Let's say we would like to convert a column of a data frame into factors with levels stored in a separate list. df <- data.frame("C1"=c("A","B","C"), "C2"=c("D","E","F")) df C1 C2 1 A D 2 B E 3 C F lst <- list("C2"=c("F","E","D"), "C3"=c("G","H","I")) lst $C2 [1] "F" "E" "D" $C3 [1] "G" "H" "I" All of the following trigger error or replace all the column values by NA: df %>% mutate_at(vars(C2), function(x) factor(x,

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

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