根据条件交换分组数据中的字符串或值

问题描述 投票:0回答:1

以下数据框按 id 变量分组。对于变量 X、Y 和 Z 上的每个 id,当且仅当特定 id 在除第一行之外的行中具有“yes”时,我希望将第一行上的“no”替换为“yes”。

id <- c(1,1,1,2,2,3,3)
X <- c("yes", "no", "no", "no", "no", "no", "no")
Y <- c("no", "no", "yes", "no", "yes", "no", "no")
Z <- c("no", "yes", "no", "no", "no", "no", "no")
df <- data.frame(id, X, Y, Z)

预期是:

id   X   Y   Z
 1 yes yes yes
 1  no  no  no
 1  no  no  no
 2  no yes  no
 2  no  no  no
 3  no  no  no
 3  no  no  no

我尝试使用 ifelse 函数,但由于分组而遇到了困难。我想在这里请求帮助。谢谢!

r dataframe dplyr swap
1个回答
0
投票

这是一种方法:

library(dplyr)

id <- c(1,1,1,2,2,3,3)
X <- c("yes", "no", "no", "no", "no", "no", "no")
Y <- c("no", "no", "yes", "no", "yes", "no", "no")
Z <- c("no", "yes", "no", "no", "no", "no", "no")
df <- data.frame(id, X, Y, Z)

df <- mutate(group_by(df, id),
             rownum = 1:n(),
             X = ifelse(rownum == 1 & length(X) > 1 & any(X[2:length(X)] == "yes"), 
                           "yes", X),
             Y = ifelse(rownum == 1 & length(Y) > 1 & any(Y[2:length(Y)] == "yes"),
                           "yes", Y),
             Z = ifelse(rownum == 1 & length(Z) > 1 & any(Z[2:length(Z)] == "yes"),
                           "yes", Z))
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.