2017-05-03 85 views
3

我有一个包含许多变量的数据框。这里是我到目前为止缩短版:将NA分配给列表中的值

n_20010_0_0 <- c(1,2,3,4) 
n_20010_0_1 <- c(0, -2, NA, 4) 
n_20010_0_2 <- c(3, 0, -7, 2) 

x <- data.frame (n_20010_0_0, n_20010_0_1, n_20010_0_2) 

我创建的返回是否有一个1变量列表中的一个新变量:

MotherIllness0 <- paste("n_20010_0_", 0:2, sep = "") 
x$MotherCAD_0_0 <- apply(x, 1, function(x) as.integer(any(x[MotherIllness0] == 1, na.rm = TRUE))) 

我想保持NAs为0,但我也想重新编码它,以便如果有-7,新的值是NA。 这是我试过和它不工作:

x$MotherCAD_0_0[MotherIllness0 == -7] <- NA 
+1

你的意思是“如果x中的列包含-7,那么在MotherCAD_0_0中的结果应该是NA?” –

+0

你的问题并不难。只有你能清楚地表达自己。 – Masoud

+1

你可以做'x $ MotherCAD_0_0 [rowSums(x [,MotherIllness0] == - 7,na.rm = T)> = 1] = NA'。 – Lamia

回答

2

你不需要在你的apply功能来定义MotherIllness0,参数1需要的照顾。

下面是一行代码,可以执行所需的两件事。

MotherIllness0 <- paste("n_20010_0_", 0:2, sep = "")  
x$MotherCAD_0_0<- apply(x[,MotherIllness0], 1, function(x) ifelse(any(x==-7), NA,  
                as.integer(any(x==1, na.rm=T)))) 

我以为1s和-7s的行都应该有NA作为新变量。如果没有,那么这应该工作:

x$MotherCAD_0_0<- apply(x[,MotherIllness0], 1, function(x) ifelse(any(x==1, na.rm=T), 1, 
               ifelse(any(x==-7), NA, 0))) 

请注意,对于上面的例子,这两行应产生相同的结果。

+1

'apply(MARGIN = 1)'实际上每次都在处理每一行,而'MotherIllness0'则是从每一行中选择列。据推测,这里显示的列数多于此处显示的列数,但OP只希望对其中的一部分执行此操作。 –

+0

@MattParker touche!这个例子并不清楚。我正在编辑相应的代码。 –

+0

@YannisVassiliadis谢谢你,这很好! – user7777508

1

这里的另一种方式做到这一点,而无需使用任何if-else逻辑:

# Here's your dataset, with a row including both 1 and -7 added: 
x <- data.frame (n_20010_0_0 = c(1, 2, 3, 4, 1), 
       n_20010_0_1 = c(0, -2, NA, 4, 0) , 
       n_20010_0_2 = c(3, 0, -7, 2, -7) 
) 

# Your original function: 
MotherIllness0 <- paste("n_20010_0_", 0:2, sep = "") 

x$MotherCAD_0_0 <- apply(x, MARGIN = 1, FUN = function(x) { 
    as.integer(
     any(x[MotherIllness0] == 1, na.rm = TRUE) 
    ) 
}) 

# A simplified version 
x$test <- apply(x, MARGIN = 1, FUN = function(row) { 

    as.integer(
     any(row[MotherIllness0] == 1, na.rm = TRUE) & 
     !any(row[MotherIllness0] == -7, na.rm = TRUE) 
    ) 

}) 

有两点要注意:的xfunction(x)匿名函数的名称可以是任何东西,你可以节省通过称它为什么,你自己有很多混乱(我将它命名为row以上)。

实际上您不太需要将结果列转换为整数 - 逻辑列更容易解释,而且它们与0-1列几乎相同(例如,TRUE + FALSE等于1)。