John Thomas
John Thomas

Reputation: 1105

Using mutate() returns error about can't modify because it's a grouping variable

Error in mutate_impl(.data, dots) : 
  Column `month` can't be modified because it's a grouping variable

I would like to plot with one of the axis's being month of the year, however I would like it to go from August to July. I would also like "week" to display 1-5 instead of 5-1.....but it is not letting me do so when I try the following code:

Admit_Weekly1 <- Admit_Weekly %>% 
+   filter(Retention_Status == 1) %>% 
+   mutate(week = factor(week,levels(week)[1,2,3,4,5])) %>% 
+   mutate(month = factor(month,levels(month)["August","September","October","November",
+                                             "December","January","February","March",
+                                             "April", "May","June","July"]))

Error in mutate_impl(.data, dots) : Column week can't be modified because it's a grouping variable

Upvotes: 4

Views: 9965

Answers (2)

Corina Roca
Corina Roca

Reputation: 546

In my case I realized I was using dplyr 0.8.5 instead of dplyr 1.0.7. Once I did the update of the package (o reinstall it) it worked without adding the ungroup() %>% line.

Upvotes: 1

Prof Ga&#235;lle
Prof Ga&#235;lle

Reputation: 66

I ran into the same problem and solved it using the ungroup():

df <- df %>%
ungroup(var) %>%
mutate(var = factor(var,levels = c(1,2,3),
                    labels = c("label1","label2","label3") ) )


Upvotes: 5

Related Questions