[英]How to replace values in the columns of a dataframe based on the values in the other column in R?
I have a dataframe containing the safety data for 100 patients. 我有一个数据框,其中包含100位患者的安全性数据。 There are different safety factors for each patient with the size of that specific factor.
每个患者的安全系数各不相同,具体取决于安全系数。
v1_d0_urt_redness v1_d0_urt_redness_size v1_d1_urt_redness v1_d1_urt_redness_size ...
P1 1 20
P2 1 NA
P3 0 NA
.
.
.
Here redness=1
means there was redness and redness=0
means there was no redness, and therefore the redness_size
was not reported. 在此,
redness=1
表示存在红色, redness=0
表示没有红色,因此未报告redness_size
。
In order to find what proportion of the data is missing I need to code the data as follows: 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)
to have this coded for d0,d1,.. and to repeat this process for the other variables like hardness, swelling and etc. Any ideas how one could implement this in R? 为了找到丢失的数据比例,我需要对数据进行如下编码:
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,..,并对其他变量(例如硬度,溶胀等)重复此过程。 ?
If I understand well what you are trying to do and assuming your dataframe is called df
, you can change values of the column redness_size
by doing this: 如果我很了解您要尝试做的事情,并假设您的数据帧称为
df
,则可以通过执行以下操作更改列redness_size
的值:
df[df[,endsWith(colnames(df),"_redness")] == 1 & is.na(df[,endsWith(colnames(df),"redness_size")]),endsWith(colnames(df),"redness_size")] <- NA
df[df[,endsWith(colnames(df),"_redness")] == 1, endsWith(colnames(df),"redness_size")] <- 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.