Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 查找不带'的行;如果某一列中已经没有NA,则该列中不会有';I don’我没有_R_Rstudio - Fatal编程技术网

R 查找不带'的行;如果某一列中已经没有NA,则该列中不会有';I don’我没有

R 查找不带'的行;如果某一列中已经没有NA,则该列中不会有';I don’我没有,r,rstudio,R,Rstudio,我刚刚注意到,如果我的数据框中的一列不包含任何NA值(请参见下面的col2),并且我无意中尝试查找没有相应col2值作为NA的行,那么下面的代码将给我一个空输出 请参见下面的col1,因为它至少有一个NA值。 同样的方法不适用于col2 > col1 = c(1,1,1,1,NA) > col2 = c(2,2,2,2,2) > df = data.frame(col1,col2) > df col1 col2 1 1 2 2 1 2 3

我刚刚注意到,如果我的数据框中的一列不包含任何NA值(请参见下面的col2),并且我无意中尝试查找没有相应col2值作为NA的行,那么下面的代码将给我一个空输出

请参见下面的col1,因为它至少有一个NA值。 同样的方法不适用于col2

> col1 = c(1,1,1,1,NA)
> col2 = c(2,2,2,2,2)
> df = data.frame(col1,col2)
> df
  col1 col2
1    1    2
2    1    2
3    1    2
4    1    2
5   NA    2
> df[-which(is.na(df$col1)),]
  col1 col2
1    1    2
2    1    2
3    1    2
4    1    2
> df[-which(is.na(df$col2)),]
[1] col1 col2
<0 rows> (or 0-length row.names)

问题不仅限于NAs。如果索引向量为空,则会发生这种情况。希望返回整个向量,但实际上,
x[numeric(0)]
x
由长度为
0
的向量索引)返回一个空向量

例如,考虑以下内容:

> df[ c(-1), ] # Negative indexing
  col1 col2
2    1    2
3    1    2
4    1    2
5   NA    2
> df[ c(), ] # numeric(0)
[1] col1 col2
<0 rows> (or 0-length row.names)
> df[ c(1), ] # Positive indexing
  col1 col2
1    1    2
df[c(-1),]#负索引 col1 col2 2 1 2 3 1 2 4 1 2 5 NA 2 >df[c(),]#数字(0) [1] col1 col2 (或长度为0的行名称) >df[c(1),]#正索引 col1 col2 1 1 2 有关更一般的解释和解决方法,请参见中的第8.1.13节

> df[ c(-1), ] # Negative indexing
  col1 col2
2    1    2
3    1    2
4    1    2
5   NA    2
> df[ c(), ] # numeric(0)
[1] col1 col2
<0 rows> (or 0-length row.names)
> df[ c(1), ] # Positive indexing
  col1 col2
1    1    2