1
votes

I have a tibble named "confidence_table". Does anyone know why if I try to add a new column using the mutate verb this does not work?

# A tibble: 12 x 3
# Groups:   Age [2]
  Age   Condition       Prop
<fctr>    <fctr>      <dbl>
   0       old      0.73993056
   1       old      0.75590278
   0       old      0.15069444
   1       old      0.13090278
   0       new      0.06388889
   1       new      0.04965278
   0       new      0.05902778
   1       new      0.05416667
   0      lure      0.23055556
   1      lure      0.23645833
   0      lure      0.13819444
   1      lure      0.12013889

I used this function from base r and it does work

confidence_table$Confidence <- as.factor(rep(c("HC", "LC"), times = 3, each = 2))

# A tibble: 12 x 4
# Groups:   Age [2]
 Age   Condition     Prop Confidence
<fctr>    <fctr>      <dbl>     <fctr>
  0       old      0.73993056     HC
  1       old      0.75590278     HC      
  0       old      0.15069444     LC
  1       old      0.13090278     LC
  0       new      0.06388889     HC
  1       new      0.04965278     HC
  0       new      0.05902778     LC
  1       new      0.05416667     LC
  0      lure      0.23055556     HC
  1      lure      0.23645833     HC
  0      lure      0.13819444     LC
  1      lure      0.12013889     LC

This is the expected output that works with base r code. However, if I use:

confidence_table <- confidence_table %>%
                    mutate(Confidence = rep(c("HC", "LC"), times = 3, each = 2))

it says: Error in mutate_impl(.data, dots) : Column Confidence must be length 6 (the group size) or one, not 12

What is wrong with it?

1

1 Answers

0
votes

In this case, the error message should actually help you figure out where the problem might be. Notice that 2 x 3 x 2 = 12.

confidence_table %>%
  mutate(Confidence = rep(c("HC", "LC"), times = 3, each = 2))
# Error in mutate_impl(.data, dots) : 
#   Column `Confidence` must be length 6 (the group size) or one, not 12

As pointed out in the comments, one way to solve this would be to ungroup first.

confidence_table %>%
  ungroup() %>%
  mutate(Confidence = rep(c("HC", "LC"), times = 3, each = 2))
# # A tibble: 12 x 4
#      Age Condition       Prop Confidence
#    <int>     <chr>      <dbl>      <chr>
#  1     0       old 0.73993056         HC
#  2     1       old 0.75590278         HC
#  3     0       old 0.15069444         LC
#  4     1       old 0.13090278         LC
#  5     0       new 0.06388889         HC
#  6     1       new 0.04965278         HC
#  7     0       new 0.05902778         LC
#  8     1       new 0.05416667         LC
#  9     0      lure 0.23055556         HC
# 10     1      lure 0.23645833         HC
# 11     0      lure 0.13819444         LC
# 12     1      lure 0.12013889         LC

You can also do it without ungrouping first though:

confidence_table %>% 
  mutate(Confidence = rep(c("HC", "LC"), times = 3)) # 2x3 = 6
# # A tibble: 12 x 4
# # Groups:   Age [2]
#      Age Condition       Prop Confidence
#    <int>     <chr>      <dbl>      <chr>
#  1     0       old 0.73993056         HC
#  2     1       old 0.75590278         HC
#  3     0       old 0.15069444         LC
#  4     1       old 0.13090278         LC
#  5     0       new 0.06388889         HC
#  6     1       new 0.04965278         HC
#  7     0       new 0.05902778         LC
#  8     1       new 0.05416667         LC
#  9     0      lure 0.23055556         HC
# 10     1      lure 0.23645833         HC
# 11     0      lure 0.13819444         LC
# 12     1      lure 0.12013889         LC

Another alternative would be to group by "Condition" instead--maybe something like:

confidence_table %>% 
  group_by(Condition) %>% 
  mutate(Confidence = c("HC", "LC")[cumsum(Age == 0)])

Sample data:

confidence_table <- structure(list(Age = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 
    0L, 1L), Condition = c("old", "old", "old", "old", "new", "new", 
    "new", "new", "lure", "lure", "lure", "lure"), Prop = c(0.73993056, 
    0.75590278, 0.15069444, 0.13090278, 0.06388889, 0.04965278, 0.05902778, 
    0.05416667, 0.23055556, 0.23645833, 0.13819444, 0.12013889)), .Names = c("Age", 
    "Condition", "Prop"), row.names = c(NA, -12L), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"), vars = "Age", drop = TRUE, indices = list(
        c(0L, 2L, 4L, 6L, 8L, 10L), c(1L, 3L, 5L, 7L, 9L, 11L)), group_sizes = c(6L, 
    6L), biggest_group_size = 6L, labels = structure(list(Age = 0:1), row.names = c(NA, 
    -2L), class = "data.frame", vars = "Age", drop = TRUE, .Names = "Age"))