Shreyas
Shreyas

Reputation: 78

Group by a variable in R and then run dplyr case_when

I want to make a new variable for every group in my dataset I'm using this code:

 data <- data  %>%
  group_by(Set_Number , Set_score) %>%
  mutate(test = case_when(
    lag(data$Serves_In_Fault) == "Serve In" ~ "Second Serve" ,
    lag(data$Serves_In_Fault) == "Fault" ~ "First Serve"
  )
)

although, this gives me an error

Error: Column test must be length 93 (the group size) or one, not 164

I want a value for every row in the dataset not for every group. Please help.

Upvotes: 0

Views: 497

Answers (1)

user2332849
user2332849

Reputation: 1450

The problem is, you're qualifying Servers_In_Fault with data$ in front of it. That way, you're accessing a new copy of the entire structure of data, not the data structure that's being processed. You're already within data, so you don't need to do it. When you do a group_by(), you're dealing subgroups of the data frame, one at a time. So when you try to process a subgroup of size 93, and you throw at it a new copy of data, with its full 164 rows, sizes don't match. Just remove the data$ within the pipes and you'll be fine.

Upvotes: 1

Related Questions