尝试根据循环创建数据帧

时间:2016-04-12 00:06:39

标签: r for-loop

我试图创建一个循环结果的数据框:

variation <- seq(0.10, 3, 0.5)
for (i in seq_along(variation)) {
  x <- iris %>% mutate(newLength = Sepal.Length + variation[i])
  newSum <- x %>% summarise(newSum = sum(newLength))
  oldSum <- iris %>% summarise(oldSum = sum(Sepal.Length))

  df <- cbind(variation[i], oldSum, newSum)
  z <- rbind(df)
  print(z)
}

我得到的输出是:

  variation[i] oldSum newSum
1          0.1  876.5  891.5
  variation[i] oldSum newSum
1          0.6  876.5  966.5
  variation[i] oldSum newSum
1          1.1  876.5 1041.5
  variation[i] oldSum newSum
1          1.6  876.5 1116.5
  variation[i] oldSum newSum
1          2.1  876.5 1191.5
  variation[i] oldSum newSum
1          2.6  876.5 1266.5

我想要的输出是:

 variation[i] oldSum newSum
         0.1  876.5  891.5
         0.6  876.5  966.5
         1.1  876.5 1041.5
         1.6  876.5 1116.5
         2.1  876.5 1191.5
         2.6  876.5 1266.5

我做错了什么?

2 个答案:

答案 0 :(得分:3)

rbind()将多行绑定在一起。如果你只给它一个df,它将只返回该数据帧。尝试使用rbind(z,df)将新DF附加到旧z。

variation <- seq(0.10, 3, 0.5)
for (i in seq_along(variation)) {
  x <- iris %>% 
    mutate(newLength = Sepal.Length + variation[i])
newSum <- x %>% 
    summarise(newSum = sum(newLength))
oldSum <- iris %>% 
  summarise(oldSum = sum(Sepal.Length))    
df <- cbind(variation[i], oldSum, newSum)
z <- rbind(z,df)
print(z)
}

请注意,z不会被清除,因此您可能希望在开始循环之前对其进行初始化。像z = NULL这样的东西可以确保它是空的。

答案 1 :(得分:3)

您应该尝试像outer这样的矢量化函数来完成分析的主要复杂部分:

data.frame(
  variation,
  oldSum=sum(iris$Sepal.Length),
  newSum=colSums(outer(iris$Sepal.Length, variation, FUN=`+`))
)

#  variation oldSum newSum
#1       0.1  876.5  891.5
#2       0.6  876.5  966.5
#3       1.1  876.5 1041.5
#4       1.6  876.5 1116.5
#5       2.1  876.5 1191.5
#6       2.6  876.5 1266.5

正如@Frank所说,你可以进一步简化/加快速度:

sum.sl <- sum(iris$Sepal.Length)
data.frame(
  variation,
  oldSum=sum.sl,
  newSum=sum.sl + length(iris$Sepal.Length)*variation
)