I am trying to subset a data frame by taking the integer values of 2 columns om my data frame
我试图通过在我的数据帧中取2列的整数值来对数据帧进行子集化
Subs1<-subset(DATA,DATA[,2][!is.na(DATA[,2])] & DATA[,3][!is.na(DATA[,3])])
but it gives me an error : longer object length is not a multiple of shorter object length.
但它给了我一个错误:较长的物体长度不是较短物体长度的倍数。
How can I construct a subset which is composed of NON NA values of column 2 AND column 3?
如何构建由第2列和第3列的非NA值组成的子集?
Thanks a lot?
非常感谢?
3 个解决方案
#1
8
Try this:
Subs1<-subset(DATA, (!is.na(DATA[,2])) & (!is.na(DATA[,3])))
The second parameter of subset
is a logical vector with same length of nrow(DATA)
, indicating whether to keep the corresponding row.
子集的第二个参数是具有相同nrow(DATA)长度的逻辑向量,指示是否保留相应的行。
#2
3
Here an example. a,b ,c are 3 vectors which a and b have a missing value. once they are created i use cbind in order to bind them in one matrix which afterwards you can transform to data frame.
这是一个例子。 a,b,c是3个向量,a和b具有缺失值。一旦它们被创建,我使用cbind将它们绑定在一个矩阵中,然后你可以转换为数据帧。
The final result is a dataframe where 2 out of 3 columns have a missing value. So we need to keep only the rows with complete cases.DATA[complete.cases(DATA), ]
is used in order to keep only these rows that have not missing values in every column. subset
object is these rows that have complete cases.
最终结果是一个数据框,其中3列中的2列具有缺失值。因此,我们只需要保留具有完整case.DATA [complete.cases(DATA),]的行,以便仅保留每列中没有缺失值的行。子集对象是具有完整案例的这些行。
a <- c(1,NA,2)
b <- c(NA,1,2)
c <- c(1,2,3)
DATA <- as.data.frame(cbind(a,b,c))
subset <- DATA[complete.cases(DATA), ]
#3
2
The na.omit functions can be an answer to you question
na.omit函数可以回答你的问题
Subs1 <- na.omit(DATA[2:3])
[https://stat.ethz.ch/R-manual/R-patched/library/stats/html/na.fail.html]
#1
8
Try this:
Subs1<-subset(DATA, (!is.na(DATA[,2])) & (!is.na(DATA[,3])))
The second parameter of subset
is a logical vector with same length of nrow(DATA)
, indicating whether to keep the corresponding row.
子集的第二个参数是具有相同nrow(DATA)长度的逻辑向量,指示是否保留相应的行。
#2
3
Here an example. a,b ,c are 3 vectors which a and b have a missing value. once they are created i use cbind in order to bind them in one matrix which afterwards you can transform to data frame.
这是一个例子。 a,b,c是3个向量,a和b具有缺失值。一旦它们被创建,我使用cbind将它们绑定在一个矩阵中,然后你可以转换为数据帧。
The final result is a dataframe where 2 out of 3 columns have a missing value. So we need to keep only the rows with complete cases.DATA[complete.cases(DATA), ]
is used in order to keep only these rows that have not missing values in every column. subset
object is these rows that have complete cases.
最终结果是一个数据框,其中3列中的2列具有缺失值。因此,我们只需要保留具有完整case.DATA [complete.cases(DATA),]的行,以便仅保留每列中没有缺失值的行。子集对象是具有完整案例的这些行。
a <- c(1,NA,2)
b <- c(NA,1,2)
c <- c(1,2,3)
DATA <- as.data.frame(cbind(a,b,c))
subset <- DATA[complete.cases(DATA), ]
#3
2
The na.omit functions can be an answer to you question
na.omit函数可以回答你的问题
Subs1 <- na.omit(DATA[2:3])
[https://stat.ethz.ch/R-manual/R-patched/library/stats/html/na.fail.html]