简体   繁体   English

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

[英]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.

相关问题 根据其他数据框中列的值选择 R 数据框中的列 - Selecting columns in R dataframe based on values of column in other dataframe 如何根据 R dataframe 中的列将 NA 值替换为不同的值? - How to replace NA values with different values based on column in R dataframe? R:如何使用来自利用其他多列的条件的值替换 dataframe 列中的 NA? - R: How do I replace NAs in a dataframe column with values from conditions leveraging other multiple columns? 如何根据其他列R中的值对一列中的值求和? - How to sum values in one column based on values in other columns R? 如何根据R中的其他列将NA替换为先前的列值加上一个分组? - How to replace NAs with previous column values plus one by group based on other columns in R? 如何根据R中的/另一列替换几列的值? - How to replace values of several columns based on/ another column in R? 根据 R 中其他列的值创建列 - Creating column based on values of other columns in R 如何根据R中其他列中的值添加计数列 - How to add a counting column based on values in other columns in R 如何根据R中另外两列的分组来标准化列中的值? - How to standardize values in a column based on grouping by two other columns in R? 根据R中的条件,删除多列并替换数据框的列值 - Remove multiple columns and replace values of columns of dataframe based on condition in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM