vendredi 30 novembre 2018

Transferring value from another column in ifelse within dplyr::mutate

I have dataframe dd (dput at bottom of question):

# A tibble: 6 x 2
# Groups:   Date [5]
  Date     keeper
  <chr>    <lgl> 
1 1/1/2018 TRUE  
2 2/1/2018 TRUE  
3 3/1/2018 FALSE 
4 4/1/2018 FALSE 
5 3/1/2018 TRUE  
6 5/1/2018 TRUE 

Note it is already grouped by Date. I'm trying to create another column that will turn "keeper" to TRUE if there's only one row in the group, and otherwise keep the value of keeper. That seemed fairly simple, but when I tried this, I got the following result:

dd %>% mutate(moose=ifelse(n()==1,TRUE,keeper))
# A tibble: 6 x 3
# Groups:   Date [5]
  Date     keeper moose
  <chr>    <lgl>  <lgl>
1 1/1/2018 TRUE   TRUE 
2 2/1/2018 TRUE   TRUE 
3 3/1/2018 FALSE  FALSE
4 4/1/2018 FALSE  TRUE 
5 3/1/2018 TRUE   FALSE
6 5/1/2018 TRUE   TRUE 

Note that rows 3 and 5 have the same Date, so they should have just retained what's in keeper for the new column -- but they both got turned to FALSE. What am I missing?

Here's the dput for the dataframe:

dd<-structure(list(Date = c("1/1/2018", "2/1/2018", "3/1/2018", "4/1/2018", 
"3/1/2018", "5/1/2018"), keeper = c(TRUE, TRUE, FALSE, FALSE, 
TRUE, TRUE)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), vars = "Date", drop = TRUE, indices = list(
    0L, 1L, c(2L, 4L), 3L, 5L), group_sizes = c(1L, 1L, 2L, 1L, 
1L), biggest_group_size = 2L, labels = structure(list(Date = c("1/1/2018", 
"2/1/2018", "3/1/2018", "4/1/2018", "5/1/2018")), class = "data.frame", row.names = c(NA, 
-5L), vars = "Date", drop = TRUE, indices = list(0L, 1L, 2L, 
    4L, 3L, 5L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    Date = c("1/1/2018", "2/1/2018", "3/1/2018", "3/1/2018", 
    "4/1/2018", "5/1/2018"), keeper = c(TRUE, TRUE, FALSE, TRUE, 
    FALSE, TRUE)), class = "data.frame", row.names = c(NA, -6L
), vars = c("Date", "keeper"), drop = TRUE, .Names = c("Date", 
"keeper")), .Names = "Date"), .Names = c("Date", "keeper"))

ADDENDUM:

As I continue to play with this dataframe, I discovered that if I first create a column n using add_count, and refer to that column in my ifelse instead of to n(), I get the result I'm looking for. What's causing this? Why isn't n() giving me the same result?

Aucun commentaire:

Enregistrer un commentaire