hyat
hyat

Reputation: 1057

How to replace values in one line by NA in R?

I have a text file:

df=read.table("E:\\value.txt", sep="\t")
df
  k    jh   fg          
1  208  0.15 0.17  
2  304  0.00 0.08

Now I want to replace unwanted values in k with NA

df$k[df$k >= 500 ]=NA

There is no problem with that but I would like to replace all corresponding values (in the same line in jh and fg ) by NA as well!

Upvotes: 3

Views: 330

Answers (1)

IRTFM
IRTFM

Reputation: 263342

It's hard to imagine this wasn't illustrated in some prior answer (but I did search):

is.na(df1[df1$k >300, ] ) <- TRUE

The fact that this was acceptable code for a dataframe object and "[.data.frame" suggests to me that it might also work for the more complex case where the column classes were more varied. There are specific is.na<- methods for specific classes:

> methods(`is.na<-`)
[1] is.na<-.default  is.na<-.discrete is.na<-.factor  

Upvotes: 7

Related Questions