Home > Enterprise >  Group by and conditions in R
Group by and conditions in R

Time:10-05

I have a dataframe with similar names, courses, lesson and completion.

name <- c("John", "John", "John", "Sam", "Sam")
course <- c("Driving", "Driving", "Driving", "Cycling", "Cycling")
lesson <- c("Gears", "Parking", "Overtaking", "Balancing", "Turning")
completion <- c(1, 0, 1, 1, 1)
df_school <- data.frame(name, course, lesson, completion)

I want to change all completion to 0 if a student has not completed any lesson. The output should be:

name <- c("John", "John", "John", "Sam", "Sam")
course <- c("Driving", "Driving", "Driving", "Cycling", "Cycling")
lesson <- c("Gears", "Parking", "Overtaking", "Balancing", "Turning")
completion <- c(0, 0, 0, 1, 1)
df_completed <- data.frame(name, course, lesson, completion)

Thank you

CodePudding user response:

With dplyr:

library(dplyr)
df_school %>% group_by(name) %>%
  mutate(completion= (length(completion) == sum(completion)))

Output:

  name  course  lesson     completion
  <chr> <chr>   <chr>           <int>
1 John  Driving Gears               0
2 John  Driving Parking             0
3 John  Driving Overtaking          0
4 Sam   Cycling Balancing           1
5 Sam   Cycling Turning             1

CodePudding user response:

By dplyr

df_school %>%
  group_by(name) %>%
  mutate(completion =  all(completion == 1))

  name  course  lesson     completion
  <chr> <chr>   <chr>           <dbl>
1 John  Driving Gears               0
2 John  Driving Parking             0
3 John  Driving Overtaking          0
4 Sam   Cycling Balancing           1
5 Sam   Cycling Turning             1
  • Related