【发布时间】:2022-01-24 02:48:22
【问题描述】:
我有一个如下所示的数据框:
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)
)
【问题讨论】:
-
你的预期输出是什么?
-
对不起,我应该澄清一下。我需要将每个单词映射回原始 df,所以我知道哪个击键是哪个单词的一部分。