将列表转换为带条件的字符串

Convert list to string with conditions

我的数据框如下所示:

x <- tibble(
  experiment_id = rep(c('1a','1b'),each=5),
  keystroke = rep(c('a','SHIFT','b','SPACE','e'),2)
)

我知道我可以使用 str_cstr_flatten 将一个列表连接成一个字符串,并且只保留如下某些值:

> y <- c('b','a','SPACE','d')
> y[y %in% letters]
[1] "b" "a" "d"

但是当我在分组管道中尝试同样的事情时:

x_out <- x %>%
  group_by(experiment_id) %>%
  mutate(
    grp = cumsum(lag(keystroke=='SPACE',default=0))) %>% 
    group_by(grp, .add=TRUE) %>%
      mutate(within_keystrokes = list(keystroke),
             within_word = within_keystrokes[within_keystrokes %in% letters]
             ) %>% 
  ungroup()

我收到错误:

Error: Problem with `mutate()` input `within_word`.
x Input `within_word` can't be recycled to size 2.
ℹ Input `within_word` is `within_keystrokes[within_keystrokes %in% letters]`.
ℹ Input `within_word` must be size 2 or 1, not 0.
ℹ The error occurred in group 1: experiment_id = "1a", grp = 0.

我阅读了 并尝试使用 ifelse 但仍然 运行 出错。

对我做错了什么有任何见解吗?

编辑:预期输出 很抱歉没有包括这个。我希望最终的 df 看起来像:

    x <- tibble(
      experiment_id = rep(c('1a','1b'),each=5),
      keystroke = rep(c('a','SHIFT','b','SPACE','e'),2),
      within_keystrokes = list(list('a','SHIFT','b','SPACE'), 
                          list('a','SHIFT','b','SPACE'), 
                          list('a','SHIFT','b','SPACE'), 
                          list('a','SHIFT','b','SPACE'),
                          'e',
                          list('a','SHIFT','b','SPACE'), 
                          list('a','SHIFT','b','SPACE'), 
                          list('a','SHIFT','b','SPACE'), 
                          list('a','SHIFT','b','SPACE'),
                          'e'),
      within_word = rep(list('ab','ab','ab','ab','e'),2)
)

你几乎解决了你的问题。你可以使用

library(dplyr)
library(stringr)

x %>%
  group_by(experiment_id, grp = cumsum(lag(keystroke == "SPACE", default = 0))) %>% 
  mutate(
    within_keystrokes = list(keystroke),
    within_word = list(str_c(keystroke[keystroke %in% letters], collapse = ""))
    )

得到

# A tibble: 10 x 4
   experiment_id keystroke within_keystrokes within_word
   <chr>         <chr>     <list>            <list>     
 1 1a            a         <list [4]>        <chr [1]>  
 2 1a            SHIFT     <list [4]>        <chr [1]>  
 3 1a            b         <list [4]>        <chr [1]>  
 4 1a            SPACE     <list [4]>        <chr [1]>  
 5 1a            e         <chr [1]>         <chr [1]>  
 6 1b            a         <list [4]>        <chr [1]>  
 7 1b            SHIFT     <list [4]>        <chr [1]>  
 8 1b            b         <list [4]>        <chr [1]>  
 9 1b            SPACE     <list [4]>        <chr [1]>  
10 1b            e         <chr [1]>         <chr [1]> 

如果你不想让within_word成为一个列表,只需删除list()函数即可。