简体   繁体   English

基于多列创建一列 - 在 R 中

[英]Create a column based on multiple columns- in R

I am familiar with statistical packages including R but I kind of stopped working in R in between and recently started using it again.我熟悉包括 R 在内的统计包,但我有点停止在 R 之间工作,最近又开始使用它。

I have simple questions and any help would be greatly appreciated it.我有简单的问题,任何帮助将不胜感激。

My question is: I want to create a variable by fulfilling in a condition based on multiple columns/variables.我的问题是:我想通过满足基于多列/变量的条件来创建变量。 For instance, a new variable to code as "1" if there is no missing values in any one of the columns from 1 to 100 and to code as 0 if otherwise.例如,如果从 1 到 100 的任何一列中没有缺失值,则将新变量编码为“1”,否则编码为 0。

How could I possibly solve this in R or possible code suggestions?我怎么可能在 R 或可能的代码建议中解决这个问题?

Many thanks in advance.提前谢谢了。

Kind regards, Mahesh亲切的问候,马赫什

我们也可以在没有ifelse情况下做到这ifelse

df$new.column <- with(df, +(is.na(a) & is.na(b)))

This is an example.这是一个例子。

df <- data.frame(a = c(1, 2, 3, 4, 5, NA, NA), b=c(6, 7, 8, NA, 9, 10, NA))
df$new.column <- ifelse(is.na(df$a) & is.na(df$b), 1, 0)
df

HTH!哼!

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

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