R: Last non-NA value among column sets

前端 未结 3 609
情话喂你
情话喂你 2021-01-21 17:23

I am looking for a solution to the problem below that would be supported in pipes.

I have data that looks like this:

tibble(
  column_set_1_1 = c(1, 2,         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-21 18:16

    Here is a solution that I came up with that works with pipes:

    df_foo %>% 
      gather(key = Key, value = Value, -ID) %>% 
      mutate(set = str_extract(Key, "column_set_[0-9]")) %>% 
      mutate(number = str_extract(Key, "(?<=column_set_[0-9]_)[0-9]+")) %>% 
      group_by(ID, set) %>% 
      dplyr::filter(!is.na(Value)) %>%
      arrange(number) %>% 
      slice(n()) %>% 
      select(-number, -Key) %>% 
      spread(key = set, value = Value)
    

    I don't like the fact that I have to arrange and then slice out the last row -- seems inelegant to me. Any improvements welcome.

提交回复
热议问题