如何根据R中另一列中的值替换数据框各列中的值?

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

我有一个数据框,其中包含100位患者的安全性数据。每个患者的安全系数各不相同,具体取决于安全系数。

   redness_d0 size_of_redness_d0 hardness_d0 size_of_hardness_d0 redness_d1 size_of_redness_d1 ...
P1  1              20             
P2  1              NA
P3  0              NA
.
.
.

这里redness=1表示发红,redness=0表示没有发红,因此未报告redness_size。为了找到丢失的数据比例,我需要对数据进行如下编码:if (the column containing redness=1 & the column containing redness_size=NA) then (the column containing redness_size<-NA) else if (the column containing redness=0 then the column containing redness_size<-0)将其编码为d0,d1,..,并对其他变量(如硬度,膨胀等)重复此过程。任何想法如何在R中实现?

r if-statement
1个回答
1
投票

如果我很了解您要尝试做的并假设您的数据帧称为df,则可以通过执行以下操作来更改列redness_size的值:

df[df[,grep("redness_d",colnames(df))] == 1 & is.na(df[,grep("redness_size_d",colnames(df))],grep("redness_size_d",colnames(df))] <- NA
df[df[,grep("redness_d",colnames(df))] == 0,grep("redness_size_d",colnames(df))] <- 0
© www.soinside.com 2019 - 2024. All rights reserved.