简体   繁体   中英

I have a data frame with 10 columns and thousand of rows. I want to replace 4th column (value<=0.05) with NA. How can I do this using R script

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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