我有一个包含一列(深度,z)的数据框,其中我试图根据常规深度值找出累积深度值的差异。我想创建一个包含3列的新数据框:条件值,其各自的累积深度值,以及连续累积深度之间存在差异的第三列,例如:
z1<-c(1.2, 1.5, 0.8, 0.7, 1.6, 1.9, 1.1, 0.6, 1.3, 1.0)
z<-data.frame(z1)
crit1<-c(0.5,1,1.5,2)
# A loop comes to mind,
for(i in c(0.5,1,1.5,2)){
print( sum(subset(z1,z1<=i)))
} # But I get an error, because I cannot use integers
Error in FUN(X[[1L]], ...) :
only defined on a data frame with all numeric variables
尝试使用cumsum
cumsum(z1)[seq(0.5,2,by=0.5)] # Which doesn't work either
我想得到一张这样的表:
Crit Cumulative Difference
0.5 0 0
1 3.1 3.1
1.5 8.2 5.1
答案 0 :(得分:2)
此处不要使用for
循环,因此您应该使用sapply
,因为您存储了结果。
y <- sapply(crit1,function(x)sum(z1[z1<=x]))
d <- c(0,diff(y))
data.frame(Crit = crit1, Cumulative =y, Difference=d)
# Crit Cumulative Difference
# 1 0.5 0.0 0.0
# 2 1.0 3.1 3.1
# 3 1.5 8.2 5.1
# 4 2.0 11.7 3.5
答案 1 :(得分:1)
你可以尝试
Difference <- setNames(c(0,tapply(z1,cut(z1, breaks=crit1,labels=F),FUN=sum)),NULL)
data.frame(Crit=crit1, Cumulative=cumsum(Difference), Difference)
# Crit Cumulative Difference
#1 0.5 0.0 0.0
#2 1.0 3.1 3.1
#3 1.5 8.2 5.1
#4 2.0 11.7 3.5