替换 data.frame 中的特殊的值

替换空值:

 

foo <- data.frame("day"= c(1, 3, 5, 7), "od" = c(0.1, "#N/A", 0.4, 0.8))

NAs <- foo == "#N/A"

## by replace method

is.na(foo)[NAs] <- TRUE

## or directly

foo[NAs] <- NA

 

替换负值为0:

 

方法一:

df[df < 0] = 0

 

方法二:

df <- data.frame(a=rnorm(1000),b=rnorm(1000))

m <- as.matrix(df)

m[m<0] <- 0

df <- as.data.frame(m)

 

REF:

http://stackoverflow.com/questions/2767219/r-how-to-replace-elements-of-a-data-frame

http://stackoverflow.com/questions/12835942/fast-replacing-values-in-dataframe-in-r

http://stackoverflow.com/questions/19503266/r-replace-all-particular-values-in-a-data-frame

 

posted @ 2016-06-03 20:42  emanlee  阅读(5690)  评论(0编辑  收藏  举报