我有一张数据文件,看起来像:
x <- tibble(
experiment_id = rep(c('1a','1b'),each=5),
keystroke = rep(c('a','SHIFT','b','SPACE','e'),2)
)我知道我可以使用str_c或str_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.我读过this answer并尝试使用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)
)发布于 2021-12-23 00:41:18
你几乎解决了你的问题。你可以用
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()函数即可。
https://stackoverflow.com/questions/70456489
复制相似问题