删除R中数据文件的空行 [英] Removing empty rows of a data file in R

查看:63
本文介绍了删除R中数据文件的空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含空行的数据集.我想删除它们:

I have a dataset with empty rows. I would like to remove them:

myData<-myData[-which(apply(myData,1,function(x)all(is.na(x)))),]

它工作正常.但现在我想在我的数据中添加一列并初始化第一个值:

It works OK. But now I would like to add a column in my data and initialize the first value:

myData$newCol[1] <- -999

Error in `$<-.data.frame`(`*tmp*`, "newCol", value = -999) : 
  replacement has 1 rows, data has 0

不幸的是它不起作用,我真的不明白为什么,我无法解决这个问题.当我使用以下方法一次删除一行时它起作用了:

Unfortunately it doesn't work and I don't really understand why and I can't solve this. It worked when I removed one line at a time using:

TgData = TgData[2:nrow(TgData),]

或任何类似的东西.

当我只使用前 13.000 行时它也有效.

It also works when I used only the first 13.000 rows.

但它不适用于我有 32.000 行的实际数据.

But it doesn't work with my actual data, with 32.000 rows.

我做错了什么?这对我来说似乎没有意义.

What did I do wrong? It seems to make no sense to me.

推荐答案

我假设您想删除全部为 NA 的行.然后,您可以执行以下操作:

I assume you want to remove rows that are all NAs. Then, you can do the following :

data <- rbind(c(1,2,3), c(1, NA, 4), c(4,6,7), c(NA, NA, NA), c(4, 8, NA)) # sample data
data
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1   NA    4
[3,]    4    6    7
[4,]   NA   NA   NA
[5,]    4    8   NA

data[rowSums(is.na(data)) != ncol(data),]
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1   NA    4
[3,]    4    6    7
[4,]    4    8   NA

如果要删除至少有一个 NA 的行,只需更改条件:

If you want to remove rows that have at least one NA, just change the condition :

data[rowSums(is.na(data)) == 0,]
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    6    7

这篇关于删除R中数据文件的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆