Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete rows in multiple columns by unique number?

Tags:

r

Given data like this

C1<-c(3,-999.000,4,4,5)
C2<-c(3,7,3,4,5)
C3<-c(5,4,3,6,-999.000)
DF<-data.frame(ID=c("A","B","C","D","E"),C1=C1,C2=C2,C3=C3)

How do I go about removing the -999.000 data in all of the columns

I know this works per column

DF2<-DF[!(DF$C1==-999.000 | DF$C2==-999.000 | DF$C3==-999.000),]

But I'd like to avoid referencing each column. I am thinking there is an easy way to reference all of the columns in a particular data frame aka:

DF3<-DF[!(DF[,]==-999.000),]

or

DF3<-DF[!(DF[,(2:4)]==-999.000),]

but obviously these do not work

And out of curiosity, bonus points if you can me why I need that last comma before the ending square bracket as in:

==-999.000),]
like image 211
Vinterwoo Avatar asked Dec 01 '22 22:12

Vinterwoo


1 Answers

The following may work

DF[!apply(DF==-999,1,sum),]

or if you can have multiple -999 on a row

DF[!(apply(DF==-999,1,sum)>0),]

or

DF[!apply(DF==-999,1,any),]
like image 134
shhhhimhuntingrabbits Avatar answered Jan 08 '23 04:01

shhhhimhuntingrabbits