I have a data frame with 10 columns and thousand of rows. I want to give conditional format(if value<=0.05) then replace cell with NA. How can I do this using R?
Asked
Active
Viewed 108 times
0
-
3What have you tried so far? SO isn't a free code writing service. Please show that you've made some effort. – Reticulated Spline May 24 '15 at 16:51
2 Answers
2
You can try
is.na(df[,4]) <- df[,4]<=0.05
Or a faster option is
df[,4] <- NA^(df[,4]<=0.05)*df[,4]
If we use data.table
, the :=
would be more efficient
library(data.table)
setDT(df)[V4<=0.05, V4:=NA] #assuming that the 4th column name is 'V4'

akrun
- 874,273
- 37
- 540
- 662
-
Thank you very much.Actually I used subset command to filter the values greater than the threshold (0.05) value. dd <- read.csv("10min_S1~S3.csv") DF <- subset(dd, v4 >= 0.05) But it make error when I tried to plot the subset data frame (DF). Therefore, I tried to set NA for v4 >= 0.05. – Thilak May 25 '15 at 11:52
-
2
@akrun has a brilliant solution. Just adding more for reference.
df[,4][df[,4]<=0.05] <- NA
Or for speed
replace(df[,4], which(df[,4]<=0.05), NA)

Pierre L
- 28,203
- 6
- 47
- 69
-
Though, my solution looks good, your take would be faster and even more faster might be to use `replace` with `which` – akrun May 24 '15 at 17:09
-
A few days ago we did one like that. I'll add it. http://stackoverflow.com/questions/30329374/replace-0-in-matrix-with-na/30330054#30330054 – Pierre L May 24 '15 at 17:12