用for循环逐列填充数据 [英] Fill data frame by column with for loop

查看:356
本文介绍了用for循环逐列填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含11列15行的空数据框,然后将其命名为这些列.

I created an empty data frame with 11 columns and 15 rows and subsequently named the columns.

L_df <- data.frame(matrix(ncol = 11, nrow = 15))
names(L_df) <- paste0("L_por", 0:10)

w <- c(0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6,  2.8, 3)
wu <- 0
L <- 333.7
pm <- c(2600, 2574, 2548, 2522, 2496, 2470, 2444, 2418, 2392, 2366,  2340)

数据框如下:

head(L_df)
  L_por0 L_por1 L_por2 L_por3 L_por4 L_por5 L_por6 L_por7 L_por8 L_por9 L_por10
1     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA      NA
2     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA      NA
3     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA      NA
4     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA      NA
5     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA      NA
6     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA      NA

现在,我想根据公式按列填充数据框.我试图用嵌套的for循环来表达这一点:

Now, I would like to fill the data frame by column, based on a formula. I tried to express this with a nested for loop:

 for (i in 1:ncol(L_df)) {
  pm_tmp <- pm[i]
  col_tmp <- colnames(L_df)[i]
  for (j in 1:nrow(L_df)) {
    w_tmp <- w[j]
    L_por_tmp <- pm_tmp*L*((w_tmp-wu)/100)
    col_tmp[j] <- L_por_tmp
  }
}

对于每个,我对长度为11的预定义向量pm进行迭代.对于每个 row ,对长度为15的预定义向量w进行迭代. (重复每列).

For each column, I iterate over a predefined vector pm of length 11. For each row, I iterate over a predefined vector w of length 15 (repeats each column).

示例:首先,在第一列中选​​择pm[1].其次,为第一列中的每一行选择w[i].将公式存储在L_por_tmp中,并用它填充从row1到row15的第一列.整个过程应从第二列(带有pm[2])重新开始,每一行以w[i]开始,依此类推. wuL在公式中是固定的.

Example: First, select pm[1] for the first column. Second, select w[i] for each row in the first column. Store the formula in L_por_tmp and use it to fill the first column from row1 to row15. The whole procedure should start all over again for the second column (with pm[2]) with w[i] for each row and so on. wu and L are fixed in the formula.

R无错误地执行代码.当我检查tmp值时,它们是正确的.但是,数据帧保持为空. L_df没有填充.我想循环解决此问题,但是如果您有其他解决方案,我很高兴听到他们的声音!我的印象是,这样做可能会更流畅.干杯!

R executes the code without an error. When I check the tmp values, they are correct. However, the data frame remains empty. L_df does not get filled. I would like solve this with a loop but if you have other solutions, I am happy to hear them! I get the impression there might be a smoother way of doing this. Cheers!

推荐答案

解决方案

L_df <- data.frame(sapply(pm, function(x) x * L * ((w - wu) / 100)))
names(L_df) <- c("L_por0", "L_por1", "L_por2", "L_por3", "L_por4", "L_por5",
                 "L_por6", "L_por7", "L_por8", "L_por9", "L_por10")
L_df
 L_por0    L_por1    L_por2    L_por3    L_por4    L_por5    L_por6    L_por7
1   1735.24  1717.888  1700.535  1683.183  1665.830  1648.478  1631.126  1613.773
2   3470.48  3435.775  3401.070  3366.366  3331.661  3296.956  3262.251  3227.546
3   5205.72  5153.663  5101.606  5049.548  4997.491  4945.434  4893.377  4841.320
4   6940.96  6871.550  6802.141  6732.731  6663.322  6593.912  6524.502  6455.093
5   8676.20  8589.438  8502.676  8415.914  8329.152  8242.390  8155.628  8068.866
6  10411.44 10307.326 10203.211 10099.097  9994.982  9890.868  9786.754  9682.639
7  12146.68 12025.213 11903.746 11782.280 11660.813 11539.346 11417.879 11296.412
8  13881.92 13743.101 13604.282 13465.462 13326.643 13187.824 13049.005 12910.186
9  15617.16 15460.988 15304.817 15148.645 14992.474 14836.302 14680.130 14523.959
10 17352.40 17178.876 17005.352 16831.828 16658.304 16484.780 16311.256 16137.732
11 19087.64 18896.764 18705.887 18515.011 18324.134 18133.258 17942.382 17751.505
12 20822.88 20614.651 20406.422 20198.194 19989.965 19781.736 19573.507 19365.278
13 22558.12 22332.539 22106.958 21881.376 21655.795 21430.214 21204.633 20979.052
14 24293.36 24050.426 23807.493 23564.559 23321.626 23078.692 22835.758 22592.825
15 26028.60 25768.314 25508.028 25247.742 24987.456 24727.170 24466.884 24206.598
      L_por8    L_por9   L_por10
1   1596.421  1579.068  1561.716
2   3192.842  3158.137  3123.432
3   4789.262  4737.205  4685.148
4   6385.683  6316.274  6246.864
5   7982.104  7895.342  7808.580
6   9578.525  9474.410  9370.296
7  11174.946 11053.479 10932.012
8  12771.366 12632.547 12493.728
9  14367.787 14211.616 14055.444
10 15964.208 15790.684 15617.160
11 17560.629 17369.752 17178.876
12 19157.050 18948.821 18740.592
13 20753.470 20527.889 20302.308
14 22349.891 22106.958 21864.024
15 23946.312 23686.026 23425.740

说明

sapply()函数可用于以更惯用的方式遍历向量,以进行R编程.我们遍历pm并使用您的公式一次,因为R是 vectorized ;每次它创建一个长度为15的向量(所以11个长度为15的向量)时,当我们将其包装在data.frame()中时,它都会返回所需的数据帧,并添加列名.

Explanation

The sapply() function can be used to iterate over vectors in a more idiomatic way for R programming. We iterate over pm and use your formula once since R is vectorised; each time it creates a vector of length 15 (so 11 vectors of length 15), and when we wrap it in data.frame() returns the data frame you want and we add in the column names.

注意:使用apply()族函数将函数应用于向量的每个元素与使用for循环进行迭代有一些不同的含义.就您而言,我认为sapply()更容易理解.有关何时需要循环或何时类似应用会更好的更多信息,请参见例如这个讨论来自Hadley Wickham的Advanced R书.

NOTE: Applying functions to every element of a vector using an apply() family function has some different implications than iterating using for loops. In your case, I think sapply() is easier and more understandable. For more information on when you need a loop or when something like apply is better, see for example this discussion from Hadley Wickham's Advanced R book.

这篇关于用for循环逐列填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆