gpt4 book ai didi

r - 如何根据左相邻列中的值替换多列中的值

转载 作者:行者123 更新时间:2023-12-02 19:03:55 27 4
gpt4 key购买 nike

我有与此类似的数据(尽管数据集更大):

  correct.trial1 RT.trial1 correct.trial2 RT.trial2 correct.trial3 RT.trial3
1 1 473 0 337 1 426
2 1 496 1 407 1 421
3 1 368 0 405 1 470
4 0 333 1 475 0 473
5 0 435 0 312 1 402

我们可以用这个来制作这个示例:

set.seed(12)
df <- data.frame(correct.trial1 = sample(0:1, 5, replace=T),
RT.trial1 = sample(300:500, 5, replace=T),
correct.trial2 = sample(0:1, 5, replace=T),
RT.trial2 = sample(300:500, 5, replace=T),
correct.trial3 = sample(0:1, 5, replace=T),
RT.trial3 = sample(300:500, 5, replace=T))

当相邻(左侧)列 starts_with("Correct.Trial"时,我想将值 starts_with("RT.Trial") 替换为 NA ) 的值为 0。当然,我可以一次执行一个,例如:

library(dplyr)
df %>%
mutate(RT.trial1 = ifelse(correct.trial1==1, RT.trial1, NA),
RT.trial2 = ifelse(correct.trial2==1, RT.trial2, NA),
RT.trial3 = ifelse(correct.trial3==1, RT.trial3, NA))

所以它看起来像这样:

  correct.trial1 RT.trial1 correct.trial2 RT.trial2 correct.trial3 RT.trial3
1 1 473 0 NA 1 426
2 1 496 1 407 1 421
3 1 368 0 NA 1 470
4 0 NA 1 475 0 NA
5 0 NA 0 NA 1 402

但是对于数千列来说这是不切实际的。

问题

如何同时对所有列执行此操作? (注意:我更喜欢 dplyr 解决方案,并且使用 across 比使用 mutate_at 更好。)

尝试

不确定,但基于此 related post ,它(也许)看起来像这样:

df %>%
mutate_at(vars(starts_with("RT.trial")),
~ifelse(vars(starts_with("correct.trial"))==0, NA, .x))

最佳答案

我们可以 reshape 为“长”格式,然后进行转换

library(dplyr)
library(tidyr)
df %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn, names_to = c(".value", "grp"),
names_sep="\\.") %>%
mutate(RT = case_when(as.logical(correct) ~ RT)) %>%
pivot_wider(names_from = grp, values_from = c(correct, RT),
names_sep = ".") %>%
select(names(df))

-输出

# A tibble: 5 x 6
# correct.trial1 RT.trial1 correct.trial2 RT.trial2 correct.trial3 RT.trial3
# <int> <int> <int> <int> <int> <int>
#1 0 NA 0 NA 0 NA
#2 1 394 1 458 0 NA
#3 0 NA 1 337 0 NA
#4 1 479 0 NA 0 NA
#5 0 NA 0 NA 0 NA

base R中,这可以通过更简单的方式完成

i1 <- grepl('correct', names(df))
df[!i1] <- (NA^!df[i1]) * df[!i1]

数据

df <- structure(list(correct.trial1 = c(0L, 1L, 0L, 1L, 0L), RT.trial1 = c(417L, 
394L, 345L, 479L, 368L), correct.trial2 = c(0L, 1L, 1L, 0L, 0L
), RT.trial2 = c(382L, 458L, 337L, 406L, 306L), correct.trial3 = c(0L,
0L, 0L, 0L, 0L), RT.trial3 = c(469L, 364L, 361L, 359L, 309L)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5"))

关于r - 如何根据左相邻列中的值替换多列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65314640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com