dplyr

R group by multiple columns and mean value per each group based on different column

女生的网名这么多〃 提交于 2021-02-15 07:51:44
问题 data set exist data with age, gender, state, income, group . Group represents the group that each user belongs to: group gender state age income 1 3 Female CA 33 $75,000 - $99,999 2 3 Male MA 41 $50,000 - $74,999 3 3 Male KY 32 $35,000 - $49,999 4 2 Female CA 23 $35,000 - $49,999 5 3 Male KY 25 $50,000 - $74,999 6 3 Male MA 21 $75,000 - $99,999 7 3 Female CA 33 $75,000 - $99,999 8 3 Male MA 41 $50,000 - $74,999 9 3 Male KY 32 $35,000 - $49,999 10 2 Female CA 23 $35,000 - $49,999 11 3 Male KY

How to filter rows based on the previous row and keep previous row using dplyr?

无人久伴 提交于 2021-02-15 07:50:40
问题 I am trying to subset rows of a data set using a condition that's based on the previous row, whilst keeping the previous row in the subsetted data. This is essentially the same as the question here, but I am looking for a dplyr approach: Select specific rows based on previous row value (in the same column) I have taken the dplyr approach applied in the comments to that answer, but I am unable to figure out the last step of retaining the previous row. I can get the rows that support the

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

dplyr select is claiming that I have extra arguments that are not there

寵の児 提交于 2021-02-11 18:22:41
问题 data <- tibble(x = 1:5, y = 6:10) data %>% select(x) returns Error: `...` is not empty. We detected these problematic arguments: * `logical` These dots only exist to allow future extensions and should be empty. Did you misspecify an argument? I can not figure out what is causing this error. I've tried reinstalling dplyr, restarting R, restarting the computer. Any suggestions would be appreciated. Running slang::last_error() gives <error/rlib_error_dots_nonempty> `...` is not empty. We

variable use in dplyr and ggplot

喜你入骨 提交于 2021-02-11 16:38:35
问题 I'm trying to sort out functional programming with dplyr/ggplot. In my first couple of weeks of R I mostly went by trial and error following snippets found on the web, but I'm trying to understand this better so it comes more natural. I'm playing around with mtcars as an example: library(tidyverse) data <- mtcars data$carb <- as.factor(data$carb) My sample code w/o using variables looks like this: data %>% filter(carb != 4) %>% ggplot() + geom_point(aes(x = mpg, y = hp, color = carb)) +

variable use in dplyr and ggplot

拜拜、爱过 提交于 2021-02-11 16:37:54
问题 I'm trying to sort out functional programming with dplyr/ggplot. In my first couple of weeks of R I mostly went by trial and error following snippets found on the web, but I'm trying to understand this better so it comes more natural. I'm playing around with mtcars as an example: library(tidyverse) data <- mtcars data$carb <- as.factor(data$carb) My sample code w/o using variables looks like this: data %>% filter(carb != 4) %>% ggplot() + geom_point(aes(x = mpg, y = hp, color = carb)) +

variable use in dplyr and ggplot

大憨熊 提交于 2021-02-11 16:36:53
问题 I'm trying to sort out functional programming with dplyr/ggplot. In my first couple of weeks of R I mostly went by trial and error following snippets found on the web, but I'm trying to understand this better so it comes more natural. I'm playing around with mtcars as an example: library(tidyverse) data <- mtcars data$carb <- as.factor(data$carb) My sample code w/o using variables looks like this: data %>% filter(carb != 4) %>% ggplot() + geom_point(aes(x = mpg, y = hp, color = carb)) +

R's padr package claiming the “datetime variable does not vary” when it does vary

≯℡__Kan透↙ 提交于 2021-02-11 15:38:07
问题 library(tidyverse) library(lubridate) library(padr) df #> # A tibble: 828 x 5 #> Scar_Id Code Type Value YrMo #> <chr> <chr> <chr> <date> <date> #> 1 0070-179 AA Start_Date 2020-04-22 2020-04-01 #> 2 0070-179 AA Closure_Date 2020-05-23 2020-05-01 #> 3 1139-179 AA Start_Date 2020-04-23 2020-04-01 #> 4 1139-179 AA Closure_Date 2020-05-23 2020-05-01 #> 5 262-179 AA Start_Date 2019-08-29 2019-08-01 #> 6 262-179 AA Closure_Date 2020-05-23 2020-05-01 #> 7 270-179 AA Start_Date 2019-08-29 2019-08-01