简体   繁体   English

在 R 中的 data.table 中删除 NA

[英]Remove NA in a data.table in R

I'm trying to do in R something apparently very easy (sorry but i'm very newbie with data.tables) but I don't manage to get the right solution.我正在尝试在 R 中做一些显然非常容易的事情(抱歉,我对 data.tables 非常陌生)但我没有设法获得正确的解决方案。 I try to delete the rows with NA values on a specific column("Ground_Tru". This is my attempt so far;我尝试删除特定列上具有 NA 值的行(“Ground_Tru”。这是我迄今为止的尝试;

all_data <- fread ("all_vbles.txt",header=TRUE, na.strings=c("NA","N/A",""))
na.omit (all_data, cols="Ground_Tru")

I get the message我收到消息

Empty data.table (0 rows) of 75 cols: OID_,IN_FID,Polygon_ID,DIST_highw,DIST_railw,DIST_port... 75 列的空数据表(0 行):OID_,IN_FID,Polygon_ID,DIST_highw,DIST_railw,DIST_port...

however the "Ground_Tru" field has many NA values thanks in advance for any help,然而,“Ground_Tru”字段有许多 NA 值,在此先感谢您的帮助,

At the end I managed to solve the problem.最后我设法解决了这个问题。 Apparently there are some issues with R reading column names using the data.table library so I followed one of the suggestions provided here: read.table doesn't read in column names显然,R 使用 data.table 库读取列名存在一些问题,因此我遵循了此处提供的建议之一: read.table 不读取列名

so the code became like this:所以代码变成了这样:

library(data.table)

read.table("all_vbles.txt",header=T,nrow=1,sep=",",dec=".",quote="")
all_data <- fread ("all_vbles.txt",header=FALSE, skip=1, ,sep="auto", na.strings=c("NA","N/A","")) 
setnames (all_data,header)
test_data <- na.omit (all_data, "Ground_Tru") 

which seemed to work fine.这似乎工作正常。

使用complete.cases

all_data <- all_data[complete.cases(all_data[, 'Ground_Tru'])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM