简体   繁体   English

根据data.frame的行值创建一个新变量

[英]Create a new variable based in row values of a data.frame

I am new to R and not very familiar with the language. 我是R语言的新手,对这种语言不是很熟悉。

I have a simple data frame in R. I'd like to add a blank column and then write some simple formulas to populate the new column for each row, based on the data in the columns of the respective row. 我在R中有一个简单的数据框。我想添加一个空白列,然后根据相应行的列中的数据编写一些简单的公式以为每一行填充新列。 Here is an example of the data frame: 这是数据帧的示例:

ID Age BP Chl DM 
1 57 120 5 Y
2 87 134 7.5 N
3 95 111 6 Y

I would then like to add a new column, which would be populated with a formula. 然后,我想添加一个新列,其中将填充一个公式。 For example, for a given ID: if Age 40-59 AND BP 100-120 AND Chl <4 AND DM "N" = "10". 例如,对于给定的ID:如果年龄40-59且BP 100-120且Chl <4且DM“ N” =“ 10”。

What package and function in R could I use to do this? 我可以使用R中的哪些程序包和函数来执行此操作? Many thanks. 非常感谢。

Imagine you have three conditions: 假设您有三个条件:

  1. Age > 50 & BP < 100 年龄> 50&BP <100
  2. Age > 50 & BP >= 100 年龄> 50&BP> = 100
  3. Age <=50 年龄<= 50

And you want to assing values v1 , v2 , v3 , respectively, and store them in a new variable. 您想分别赋值v1v2v3 ,并将它们存储在新变量中。

dat$newvar = NA # creates a new variable filled with NAs
ind1 = dat$Age > 50 & dat$BP < 100
ind2 = dat$Age > 50 & dat$BP >= 100
ind3 = dat$Age <=50

dat$newvar[ind1] = v1
dat$newvar[ind2] = v2
dat$newvar[ind3] = v3

You can use the same approach with as many conditions you want, just make sure the conditions are mutually excluyent. 您可以在需要的多个条件下使用相同的方法,只需确保条件互斥即可。 Rows not matching any condition will remain NA . 不符合任何条件的行将保留NA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM