如何按条件复制行并替换R中的内容

时间:2018-11-26 11:07:24

标签: r duplicates

我想通过测试条件然后更改变量的内容来复制数据框的行。

我的原始数据框是这样的:

df <- data.frame(id = c("x", "y", "w"), decision = c("partial", "refusal", "total"),
             code = c("AAA20", "AAA61", "AAA77"), `2nd_decision` = c("total", "partial", NA),
             `2nd_code` = c("BBB50", "BBB89", NA), varx = c("a", "v", "p"))



id   decision    code   2nd_decision  2nd_code  varx
 x    partial   AAA20    total          BBB50    a
 y    refusal   AAA61    partial        BBB89    v
 w    total     AAA77                            p

我想每次测试2nd_decision是“ partial”还是“ total”,如果是,请复制该行并将变量“ decision”和“ code”的内容替换为“ 2nd_decision”和“ 2nd_code” ;另外,我也不想再显示“ 2nd_decision”和“ 2nd_code”的内容,而将数据帧的其余部分保持原样,就像这样:

id   decision    code   2nd_decision  2nd_code  varx 
 x    partial   AAA20    total          BBB50    a
 y    refusal   AAA61    partial        BBB89    v
 w    total     AAA77                            p
 x    total     BBB50                            a
 y    partial   BBB89                            v

提前谢谢

2 个答案:

答案 0 :(得分:0)

这是您想要的吗?

df <- data.frame(id = c("x", "y", "w"), decision = c("partial", "refusal", "total"),
                 code = c("AAA20", "AAA61", "AAA77"), `2nd_decision` = c("total", "partial", NA),
                 `2nd_code` = c("BBB50", "BBB89", NA), varx = c("a", "v", "p"))

add_rows <- unique(df[, c("id", "X2nd_decision", "X2nd_code", "varx")])
colnames(add_rows) <- c("id", "decision", "code", "varx")
add_rows <- add_rows[!is.na(add_rows$decision), ]

library(plyr)
df_final <- rbind.fill(df, add_rows)
df_final

答案 1 :(得分:0)

您可以将mutateifelse语句结合使用。

让我们首先重新创建您的数据。

df <- data.frame(id = c("x", "y", "w", "x", "y"),
                 decision = c("partial", "refusal", "total", "total", "partial"),
                 code = c("AAA20", "AAA61", "AAA77", "BBB50", "BBB89"),
                 decision2 = c("total", "partial", NA, NA, NA),
                 varx = c("a", "v", "p", "a", "v"))

这里是测试第二个决定并删除不需要的变量的代码。

library(tidyverse)

dfnew <- df %>% 
  mutate(code = ifelse(decision2 == "total", "BBB50",
                ifelse(decision2 == "partial", "BBB89", NA))) %>% 
  select(-decision2)