Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I try to delete the rows with NA values on a specific column("Ground_Tru". This is my attempt so far;

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...

however the "Ground_Tru" field has many NA values thanks in advance for any help,

like image 351
vizpi Avatar asked Sep 10 '25 10:09

vizpi


2 Answers

Use complete.cases:

all_data <- all_data[complete.cases(all_data[, 'Ground_Tru'])]
like image 193
rankthefirst Avatar answered Sep 13 '25 01:09

rankthefirst


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

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.

like image 33
vizpi Avatar answered Sep 12 '25 23:09

vizpi