-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behavior
Milestone
Description
I've encountered an issue trying to select the maximum value of a column with groupings and mutating that column with NAs present.
x <- rep(c("Bob", "Jane"), each = 36)
y <- rep(rep(c("A", "B", "C"), each = 12), 2)
day <- rep(rep(1:12, 3), 2)
values <- rep(rep(c(10, 11, 30, 12, 13, 14, 15, 16, 17, 18, 19, 20), 3), 2)
df <- data.frame(x = x, y = y, day = day, values = values)
df$values[1:12] <- NA
df_works <- df %>%
group_by(x, y) %>%
mutate(max.sum = day[which.max(values)[1]]) %>%
group_by(y) %>%
mutate(adjusted_values = ifelse(day < max.sum, 30, values))This works. The regrouping seems to be required though...
df_logic_fail <- df %>%
group_by(x, y) %>%
mutate(max.sum = day[which.max(values)[1]]) %>%
mutate(adjusted_values = ifelse(day < max.sum, 30, values))Error: incompatible types, expecting a logical vectorEven though after the first mutate the data frame still appears to be grouped...
df_still_grouped <- df %>%
group_by(x, y) %>%
mutate(max.sum = day[which.max(values)[1]])Source: local data frame [72 x 5]
Groups: x, y
x y day values max.sum
1 Bob A 1 NA NA
2 Bob A 2 NA NA
3 Bob A 3 NA NA
4 Bob A 4 NA NA
5 Bob A 5 NA NA
6 Bob A 6 NA NA
7 Bob A 7 NA NA
8 Bob A 8 NA NA
9 Bob A 9 NA NA
10 Bob A 10 NA NA
.. ... . ... ... ...What also doesn't work is if your only grouping by one column
x2 <- rep(c("Bob", "Jane"), each = 12)
day2 <- rep(1:12, 2)
values2 <- rep(c(10, 11, 30, 12, 13, 14, 15, 16, 17, 18, 19, 20), 2)
df2 <- data.frame(x = x2, day = day2, values = values2)
df2$values[1:12] <- NA
df_test2 <- df2 %>%
group_by(x) %>%
mutate(max.sum = day[which.max(values)[1]]) %>%
group_by(x) %>%
mutate(adjusted_values = ifelse(day < max.sum, 30, values))Error: incompatible types, expecting a logical vectorReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behavior