suppose that we have the following data frame:
假设我们有以下数据框架:
> dataset1
x
1 1
2 2
3 3
4 NA
5 5
I want to come up with a R command that computes the row index of the 1-column data frame that contains the value of 'NA'. More specifically, in above dataset1 example, such command would return 4 - because the 'NA' appears in the 4th row of the data frame. How can I make this happen? thank you!
我想提出一个R命令,它计算包含“NA”值的1列数据帧的行索引。更具体地说,在上面的dataset1示例中,这样的命令将返回4——因为“NA”出现在数据帧的第4行。我怎样才能做到这一点呢?谢谢你!
2 个解决方案
#1
21
As suggested by Ben Bolker, you can use both which
and is.na
as in:
根据Ben Bolker的建议,你可以同时使用which和is。na在:
> which(is.na(dataset1), arr.ind=TRUE)
row col
4 4 1 # NA is in row 4 and column 1
#2
0
create newdataset1
which is a table formed after deleting rows with missing column values from dataset1
use -which(is.na)
创建newdataset1,它是在使用—(is.na)从dataset1中删除具有缺失列值的行之后形成的表。
newdataset1<-dataset1[-which(is.na(dataset1$x)),]
#1
21
As suggested by Ben Bolker, you can use both which
and is.na
as in:
根据Ben Bolker的建议,你可以同时使用which和is。na在:
> which(is.na(dataset1), arr.ind=TRUE)
row col
4 4 1 # NA is in row 4 and column 1
#2
0
create newdataset1
which is a table formed after deleting rows with missing column values from dataset1
use -which(is.na)
创建newdataset1,它是在使用—(is.na)从dataset1中删除具有缺失列值的行之后形成的表。
newdataset1<-dataset1[-which(is.na(dataset1$x)),]