【问题标题】:Creating a Data frame from a for loop in R从 R 中的 for 循环创建数据框
【发布时间】:2019-08-12 18:00:53
【问题描述】:

我正在尝试从 For 循环中创建一个数据框,但是在我找到的所有建议中,我只获得了循环的最后一个值,而不是整个结果。

我尝试在循环之前创建一个空列表,一个空的data.frame,矩阵,并在循环中定义了这样的元素,在循环之后再次调用它,但它只取循环的最后一个值。

random_growth <- rnorm(n=10000, mean=0.11, sd=0.3560766)
fcf0 <- 3619
 for (i in random_growth){
  fcf1= fcf0 *(1+i)
  fcf2= fcf1*(1+i)
  fcf3= fcf2*(1+i)
  fcf4= fcf3*(1+i)
  fcf5= fcf4*(1+i)
  fcf6= fcf5*(1+i)
  ffcfs = c(fcf1,fcf2,fcf3,fcf4,fcf5,fcf6)
  print(rbind(ffcfs))
    }

我想要一个 6 列和 10.000 行的数据框。我知道我的循环不是进行此类计算的最佳方法,但我没有找到另一种方法。拜托,如果你能帮助我,我会很高兴。

我找到了一种创建数据框的方法,但我尝试在循环内创建不同的 if-else 语句,因为 random_growth 上的某些值可能是负数,如果我执行直接公式,它将不会被应用。所以我想出了这个改变,但不知何故计算是错误的:

random_growth <- rnorm(n=10000, mean=0.11, sd=0.3560766)
fcf0 <- 3619
ffcfs <- data.frame()
  for (i in random_growth){
    if (i>0){
  fcf1= fcf0*(1+i)
  fcf2= fcf1*(1+i)
  fcf3= fcf2*(1+i)
  fcf4= fcf3*(1+i)
  fcf5= fcf4*(1+i)
  fcf6= fcf5*(1+i)
    }
  else{
  fcf1= if ((fcf0*i)>0){fcf0*(1+i)} else {fcf0-(fcf0*i)}
  fcf2= if ((fcf1*i)>0){fcf1*(1+i)} else {fcf1-(fcf1*i)}
  fcf3= if ((fcf2*i)>0){fcf2*(1+i)} else {fcf2-(fcf2*i)}
  fcf4= if ((fcf3*i)>0){fcf3*(1+i)} else {fcf3-(fcf3*i)}
  fcf5= if ((fcf4*i)>0){fcf4*(1+i)} else {fcf4-(fcf4*i)}
  fcf6= if ((fcf5*i)>0){fcf5*(1+i)} else {fcf5-(fcf5*i)}
  }
  row_i = c(fcf1,fcf2,fcf3,fcf4,fcf5,fcf6)
  ffcfs = rbind(ffcfs,row_i)
    }

您能帮我找出问题所在吗? 非常感谢!!

【问题讨论】:

    标签: r loops dataframe for-loop


    【解决方案1】:

    您正在运行的循环不断声明或更新ffcfs 向量。

    试试这个:

    random_growth <- rnorm(n=10000, mean=0.11, sd=0.3560766)
    fcf0 <- 3619
    ffcfs = data.frame()
    
    for (i in random_growth){
      fcf1 = fcf0 *(1+i)
      fcf2 = fcf1*(1+i)
      fcf3 = fcf2*(1+i)
      fcf4= fcf3*(1+i)
      fcf5 = fcf4*(1+i)
      fcf6 = fcf5*(1+i)
    
      row_i = c(fcf1,fcf2,fcf3,fcf4,fcf5,fcf6)
    
      ffcfs = rbind(ffcfs,row_i)
    
    }
    

    这样,第 i 行将添加到您的数据框中,而无需为每次迭代创建新的数据框。希望对您有所帮助!

    【讨论】:

    • 正如你之前帮助过我的那样,我想知道你是否可以看看这个问题的新编辑部分,关于同一循环中的 if-else 语句。非常感谢!
    【解决方案2】:

    为此不需要循环。这是使用lapply的简洁方式-

    set.seed(2)
    random_growth <- rnorm(n=10000, mean=0.11, sd=0.3560766)
    
    df <- data.frame(
      setNames(
        lapply(1:6, function(x) {
          3619*(1 + random_growth)^x
        }),
        c("fcf1","fcf2","fcf3","fcf4","fcf5","fcf6")
      )
    )
    
    head(df)
    
          fcf1      fcf2      fcf3       fcf4       fcf5       fcf6
    1 2861.289  2262.220  1788.578  1414.1033  1118.0321   883.9494
    2 4255.294  5003.462  5883.173  6917.5554  8133.8033  9563.8924
    3 6063.253 10158.341 17019.229 28513.9244 47772.0740 80037.0733
    4 2560.441  1811.511  1281.644   906.7625   641.5342   453.8852
    5 3913.674  4232.342  4576.957  4949.6326  5352.6526  5788.4882
    6 4187.732  4845.842  5607.374  6488.5831  7508.2754  8688.2141
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      相关资源
      最近更新 更多