循环操作数据帧;要更换的项目数不是更换长度的倍数

时间:2015-10-28 10:15:50

标签: r

我正试图用R操纵循环中的数据帧 这个框架有3个变量的100个观测值。

#Lemmings in order
lemmings <- ds[order(ds$weights, ds$lifetimes),]

for(i in 100) {
 #Amount of Sightings on that specific moment
 s <- sum( lemmings[0:i,]$sightings )

 l <- s / lemmings$lifetimes
 w <- s / lemmings$weights

 #Change values; this will show warnings!
 lemmings$l[i] <- l
 lemmings$w[i] <- w

 print(l)
 print(w)
}
print(lemmings)

当我尝试操纵框架时,添加$ l和$ w 我收到了这个警告:

Warning messages:
1: In lemmings$l[i] <- l :
number of items to replace is not a multiple of replacement length
2: In lemmings$w[i] <- w :
number of items to replace is not a multiple of replacement length

数据值$ l,$ w仍然与之前相同(生命周期,权重),见一行:

sightings   weights lifetimes        l           w
33.55230  0.579702         4   4.0000    0.579702

如何删除警告消息,并通过计算更改变量?

1 个答案:

答案 0 :(得分:0)

for(i in 100) {不应该是for(i in 1:100) {