mutate

How to mutate_at multiple columns on a condition on each value?

送分小仙女□ 提交于 2021-02-19 05:55:28
问题 I have a dataframe of over 1 million rows, and a column for each hour in the day. I want to mutate each value in those columns, but that modifition depends of the sign of the value. How can I efficiently do that ? I could do a gather on those hourly values (then spread), but gather seems to be pretty slow on big dataframes. I could also just do the same mutate on all 24 columns, but it does not seems like a great solution when mutate_at looks to be able to do exactly that. I'll probably have

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

Using switch statement within dplyr's mutate

五迷三道 提交于 2021-02-16 20:04:13
问题 I would like to use a switch statement within dplyr's mutate. I have a simple function that performs some operations and assigns alternative values via switch, for example: convert_am <- function(x) { x <- as.character(x) switch(x, "0" = FALSE, "1" = TRUE, NA) } This works as desired when applied to scalars: >> convert_am(1) [1] TRUE >> convert_am(2) [1] NA >> convert_am(0) [1] FALSE I would like to arrive at equivalent results via mutate call: mtcars %>% mutate(am = convert_am(am)) This

Create new variable by multiple conditions via mutate case_when

允我心安 提交于 2021-02-15 06:21:08
问题 Hi want to create a new variable/column (WHRcat) by 2 variables (WHR and sexe) under a certain condition wth dyplr, mutate and case_when. Data: WHR sexe WHRcat (new variable) 1.5 1 2.8 2 0.2 2 0.3 1 1.1 1 My code: test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1, (WHR < 1.02 & sexe = 1) ~ 2, (WHR >= 0.85 & sexe = 2) ~ 3, (WHR < 0.85 & sexe = 2) ~ 4, TRUE ~ 0)) Though doesnt work. Error: > test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1, + (WHR < 1.02

Create new variable by multiple conditions via mutate case_when

ε祈祈猫儿з 提交于 2021-02-15 06:14:09
问题 Hi want to create a new variable/column (WHRcat) by 2 variables (WHR and sexe) under a certain condition wth dyplr, mutate and case_when. Data: WHR sexe WHRcat (new variable) 1.5 1 2.8 2 0.2 2 0.3 1 1.1 1 My code: test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1, (WHR < 1.02 & sexe = 1) ~ 2, (WHR >= 0.85 & sexe = 2) ~ 3, (WHR < 0.85 & sexe = 2) ~ 4, TRUE ~ 0)) Though doesnt work. Error: > test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1, + (WHR < 1.02

Create new variable by multiple conditions via mutate case_when

£可爱£侵袭症+ 提交于 2021-02-15 06:12:48
问题 Hi want to create a new variable/column (WHRcat) by 2 variables (WHR and sexe) under a certain condition wth dyplr, mutate and case_when. Data: WHR sexe WHRcat (new variable) 1.5 1 2.8 2 0.2 2 0.3 1 1.1 1 My code: test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1, (WHR < 1.02 & sexe = 1) ~ 2, (WHR >= 0.85 & sexe = 2) ~ 3, (WHR < 0.85 & sexe = 2) ~ 4, TRUE ~ 0)) Though doesnt work. Error: > test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1, + (WHR < 1.02

Replace values in a column with specific row value from same column using loop

落爺英雄遲暮 提交于 2021-02-11 13:50:09
问题 I have data obtained from a survey that lists the recipient's name and whether or not they selected a specific county in the state. The survey structure outputs an off for any county not selected and an for the selected county. The state has about 100 counties so there end up being a lot of columns that really correspond to the same question. What I am looking to do is replace any cells with on with the county name and any cells with off with a blank. From there I can basically unite many

Why won't pipe operator let me combine successive mutations?

心已入冬 提交于 2021-02-11 12:44:47
问题 I want to mutate columns from this... ...into... When I did the following... villastats<-villastats%>% mutate(HG = ifelse(HomeTeam == "Aston Villa", villastats$FTHG, ifelse(HomeTeam != "Aston Villa", 0, 0))) villastats<-villastats%>% mutate(AG = ifelse(AwayTeam == "Aston Villa", villastats$FTAG, ifelse(AwayTeam != "Aston Villa", 0, 0))) villastats<-villastats%>% mutate(THG=cumsum(villastats$HG)) villastats<-villastats%>% mutate(TAG=cumsum(villastats$AG)) villastats<-villastats%>% mutate(Tot

Vlookup and Count String Occurrences in Separate Table R to new Column

…衆ロ難τιáo~ 提交于 2021-02-10 19:53:25
问题 I have two data frames. Below are samples but should be easily reproducible for illustration. df1 <- data.frame(School = c("Omaha South", "Omaha Central", "Grand Island"), Enrollment = c(2166, 2051, 1982)) df2 <- data.frame('Away Score' = c(25, 57, 76), 'Away Team' = c("Omaha South", "Omaha Central", "Grand Island"), 'Away Score' = c(52, 88, 69), 'Away Team' = c("Omaha Central", "Grand Island", "Omaha South"), Date = c("1/11/2020", "1/12/2020", "1/13/2020"), Winner = c("Omaha Central", "Grand