dplyr

Why does dplyr error in this nested if_else, when logical condition means output should not be evaluated?

家住魔仙堡 提交于 2021-02-19 02:17:03
问题 I have a nested if_else statement inside mutate . In my example data frame: tmp_df2 <- data.frame(a = c(1,1,2), b = c(T,F,T), c = c(1,2,3)) a b c 1 1 TRUE 1 2 1 FALSE 2 3 2 TRUE 3 I wish to group by a and then perform operations based on whether a group has one or two rows. I would have thought this nested if_else would suffice: tmp_df2 %>% group_by(a) %>% mutate(tmp_check = n() == 1) %>% mutate(d = if_else(tmp_check, # check for number of entries in group 0, if_else(b, sum(c)/c[b == T], sum

Why does dplyr error in this nested if_else, when logical condition means output should not be evaluated?

拜拜、爱过 提交于 2021-02-19 02:15:20
问题 I have a nested if_else statement inside mutate . In my example data frame: tmp_df2 <- data.frame(a = c(1,1,2), b = c(T,F,T), c = c(1,2,3)) a b c 1 1 TRUE 1 2 1 FALSE 2 3 2 TRUE 3 I wish to group by a and then perform operations based on whether a group has one or two rows. I would have thought this nested if_else would suffice: tmp_df2 %>% group_by(a) %>% mutate(tmp_check = n() == 1) %>% mutate(d = if_else(tmp_check, # check for number of entries in group 0, if_else(b, sum(c)/c[b == T], sum

Can not use dynamic variable names with dplyr in r

寵の児 提交于 2021-02-18 18:57:27
问题 I see the answers on this website, but it can not solve my problem. What I want is that use dynamic variable names both on LHS and RHS within summarize . This is a simple example to show what I have tried: why I use paste0('carb') not use carb directly is that on the position( paste0('carb') ) is a dynamic variable like this paste0('temp', n) and n is a series of numbers in my real situation. library(dplyr) sumay1 <- mtcars %>% group_by(cyl) %>% summarise(!!paste0('carb', 100) := mean(paste0(

Is there more efficient or concise way to use tidyr::gather to make my data look 'tidy'?

前提是你 提交于 2021-02-18 16:59:43
问题 I am new to using tidyverse. I want to see if I am being as efficient/concise as possible using the functions in this package. I suspect I am not. My original data has the key sym as part of each column name. day a_x b_x a_y b_y 1 1 -0.56047565 1.2240818 -1.0678237 0.42646422 2 2 -0.23017749 0.3598138 -0.2179749 -0.29507148 ... I would like to make the data look tidy, like so: day sym x y 1 1 a 0.118 0.702 2 2 a -0.947 -0.262 ... 11 1 b 1.44 0.788 12 2 b 0.452 0.769 Here is my code that does

Is there more efficient or concise way to use tidyr::gather to make my data look 'tidy'?

爷,独闯天下 提交于 2021-02-18 16:59:12
问题 I am new to using tidyverse. I want to see if I am being as efficient/concise as possible using the functions in this package. I suspect I am not. My original data has the key sym as part of each column name. day a_x b_x a_y b_y 1 1 -0.56047565 1.2240818 -1.0678237 0.42646422 2 2 -0.23017749 0.3598138 -0.2179749 -0.29507148 ... I would like to make the data look tidy, like so: day sym x y 1 1 a 0.118 0.702 2 2 a -0.947 -0.262 ... 11 1 b 1.44 0.788 12 2 b 0.452 0.769 Here is my code that does

R: Calculating cumulative number of unique entries

情到浓时终转凉″ 提交于 2021-02-18 15:57:37
问题 I have a data frame from several experiments. I am looking to calculate cumulative number of unique values obtained after each successive experiment. For example, consider: test <- data.frame(exp = c( rep("exp1" , 4) , rep("exp2" , 4), rep("exp3" , 4) , rep("exp4" , 5) ) , entries = c("abcd","efgh","ijkl","mnop", "qrst" , "uvwx" , "abcd","efgh","ijkl" , "qrst" , "uvwx", "yzab" , "yzab" , "cdef" , "mnop" , "uvwx" , "ghij")) > test exp entries 1 exp1 abcd 2 exp1 efgh 3 exp1 ijkl 4 exp1 mnop 5

R: Calculating cumulative number of unique entries

a 夏天 提交于 2021-02-18 15:57:08
问题 I have a data frame from several experiments. I am looking to calculate cumulative number of unique values obtained after each successive experiment. For example, consider: test <- data.frame(exp = c( rep("exp1" , 4) , rep("exp2" , 4), rep("exp3" , 4) , rep("exp4" , 5) ) , entries = c("abcd","efgh","ijkl","mnop", "qrst" , "uvwx" , "abcd","efgh","ijkl" , "qrst" , "uvwx", "yzab" , "yzab" , "cdef" , "mnop" , "uvwx" , "ghij")) > test exp entries 1 exp1 abcd 2 exp1 efgh 3 exp1 ijkl 4 exp1 mnop 5

combine two looping structures to obtain a matrix output

怎甘沉沦 提交于 2021-02-18 08:38:49
问题 I'm using two closely related formulas in R. I was wondering if it might be possible to combine B1 and B2 to get my desired matrix output shown below? z <- "group y1 y2 1 1 2 3 2 1 3 4 3 1 5 4 4 1 2 5 5 2 4 8 6 2 5 6 7 2 6 7 8 3 7 6 9 3 8 7 10 3 10 8 11 3 9 5 12 3 7 6" dat <- read.table(text = z, header = T) (B1 = Reduce("+", group_split(dat, group, .keep = FALSE) %>% map(~ nrow(.)*(colMeans(.)-colMeans(dat[-1]))^2))) # y1 y2 #61.86667 19.05000 (B2 = Reduce("+",group_split(dat, group, .keep =

R: Recoding variables using recode, mutate and case_when

喜欢而已 提交于 2021-02-18 03:23:17
问题 I want to recode the following values < 4 = -1, 4 = 0, > 4 = 1 for the following variables defined by core.vars in the dataset, and still keep the rest of the variables in the data frame. temp.df <- as.tibble (mtcars) other.vars <- c('hp', 'drat', 'wt') core.vars <- c('mpg', 'cyl', 'disp') temp.df <- rownames_to_column (temp.df, var ="cars_id") temp.df <- temp.df %>% mutate_if (is.integer, as.numeric) I have tried a number of ways to implement this. Using case_when , mutate , recode but with

R: Recoding variables using recode, mutate and case_when

强颜欢笑 提交于 2021-02-18 03:21:40
问题 I want to recode the following values < 4 = -1, 4 = 0, > 4 = 1 for the following variables defined by core.vars in the dataset, and still keep the rest of the variables in the data frame. temp.df <- as.tibble (mtcars) other.vars <- c('hp', 'drat', 'wt') core.vars <- c('mpg', 'cyl', 'disp') temp.df <- rownames_to_column (temp.df, var ="cars_id") temp.df <- temp.df %>% mutate_if (is.integer, as.numeric) I have tried a number of ways to implement this. Using case_when , mutate , recode but with