case-when

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

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

R apply multiple functions when large number of categories/types are present using case_when (R vectorization)

夙愿已清 提交于 2021-02-11 17:24:10
问题 Suppose I have a dataset of the following form: City=c(1,2,2,1) Business=c(2,1,1,2) ExpectedRevenue=c(35,20,15,19) zz=data.frame(City,Business,ExpectedRevenue) zz_new=do.call("rbind", replicate(zz, n=30, simplify = FALSE)) My actual dataset contains about 200K rows. Furthermore, it contains information for over 100 cities. Suppose, for each city (which I also call "Type"), I have the following functions which need to be applied: #Writing the custom functions for the categories here Type1

ORA-00913: too many values while using case when

不羁的心 提交于 2021-01-28 12:09:23
问题 I have a requirement such that, if one condition is true i should execute on query Q1 if that condition fails, i should execute another query Q2. This queries result is the records of search performed by the user. I am using case when statement for if condition, as Q1 and Q2 have more than one column to retrieve, I am getting ORA-00913: too many values . I came to know that case when cannot execute queries with more columns in retrieving data. Can anyone suggest how to achieve this type

Making tidyeval function inside case_when

不羁岁月 提交于 2020-07-21 03:06:48
问题 I have a data set that I like to impute one value among others based on probability distribution of those values. Let make some reproducible example first library(tidyverse) library(janitor) dummy1 <- runif(5000, 0, 1) dummy11 <- case_when( dummy1 < 0.776 ~ 1, dummy1 < 0.776 + 0.124 ~ 2, TRUE ~ 5) df1 <- tibble(q1 = dummy11) here is the output: df1 %>% tabyl(q1) q1 n percent 1 3888 0.7776 2 605 0.1210 5 507 0.1014 I used mutate and sample to share value= 5 among value 1 and 2 like this: df1 %