我有个小问题需要你帮忙。我有以下数据框架:
set.seed(1000)
test = data.frame(a = sample(10, replace=T), b = sample(10, replace=T), c=rep(NA, 10))
> test
a b c
1 1 6 NA
2 2 4 NA
3 6 3 NA
4 6 9 NA
5 1 5 NA
6 4 3 NA
7 5 1 NA
8 3 7 NA
9 5 10 NA
10 4 2 NA并执行diff()函数来计算每列内连续行之间的差异。
test2 = abs(apply(test, 2, diff))
> test2
a b c
[1,] 1 2 NA
[2,] 4 1 NA
[3,] 0 6 NA
[4,] 5 4 NA
[5,] 3 2 NA
[6,] 1 2 NA
[7,] 2 6 NA
[8,] 2 3 NA
[9,] 1 8 NA我想用NA值替换'test‘中的那些元素,比如,test2中的差异大于/等于4。例如,我希望test3,1 3,1成为NA,因为它在test22中的差异是>= 4。
发布于 2014-07-08 15:15:17
test2 <- abs(apply(test,2,function(x) c(NA, diff(x))))更新
根据新的资料:
test[!is.na(test2) & test2 >4] <- NA
test
# a b c
# 1 4 4 NA
# 2 8 8 NA
# 3 NA 4 NA
# 4 NA NA NA
# 5 6 8 NA
# 6 NA NA NA
# 7 NA 5 NA
# 8 6 7 NA
# 9 3 NA NA
# 10 3 NA NAhttps://stackoverflow.com/questions/24635358
复制相似问题