简体   繁体   English

通过R中的数据框进行基本循环计算

[英]Basic looping calculation through the dataframe in R

I have this structure: 我有这个结构:

census <- structure(list(date1 = c(1993L, 1993L, 1993L), dbh1 = c(NA, 40.7, 
67), liana1 = c(NA, 2, 3), date2 = c(1994L, 1994L, 1994L), dbh2 =
c(NA, 
41.3, NA), date3 = c(1995L, 1995L, 1995L), dbh3 = c(NA_real_,  NA_real_, NA_real_), date4 = c(1996L, 1996L, 1996L), dbh4 =
c(NA_real_,  NA_real_, NA_real_), date5 = c(1998L, 1998L, 1998L), dbh5
= c(NA_real_,  NA_real_, NA_real_), date6 = c(2000L, 2000L, 2000L), dbh6 = c(NA_real_,  NA_real_, NA_real_), date7 = c(2003L, 2003L,
2003L), dbh7 = c(NA_real_,  NA_real_, NA_real_), liana7 = c(NA_real_,
NA_real_, NA_real_), 
    date8 = c(2006L, 2006L, 2006L), dbh8 = c(20.1, NA, NA), date9 = c(2009L, 
     2009L, 2009L), dbh9 = c(24.2, NA, NA), liana9 = structure(c(4L, 
     1L, 1L), .Label = c("", ",", "+", "1", "2", "3"), class = "factor"), 
     death = c(NA, 1995L, 1994L), wd = c(0.6185, 0.6185, 0.6185
     )), .Names = c("date1", "dbh1", "liana1", "date2", "dbh2",  "date3", "dbh3", "date4", "dbh4", "date5", "dbh5", "date6", "dbh6", 
 "date7", "dbh7", "liana7", "date8", "dbh8", "date9", "dbh9", 
 "liana9", "death", "wd"), row.names = c(NA, 3L), class = "data.frame")

And I want to get each "dbh" (1 to 9) column and apply an equation those values. 我想获取每个“ dbh”(1到9)列,并应用这些值的方程式。 Then I was trying to add those results into new columns in my dataframe (adding new 9 columns). 然后,我试图将这些结果添加到数据框中的新列中(添加新的9列)。 For that I came up with this loop: 为此,我想到了这个循环:

 dbh =c("dbh1","dbh2","dbh3","dbh4","dbh5","dbh6","dbh7","dbh8","dbh9")



for (i in 1:9) {
     census[,31+i] <- census$wd * exp(-1.499 + 2.148 * log(census$dbh[i]) +
     0.207*(log(census$dbh[i]))^2 - 0.0281*(log(census$dbh[i]))^3) / 1000                                 
 }

I am starting to learn how to loop, so I am not even sure if that would work, but the error that I get is: 我开始学习如何循环,所以我什至不确定那是否行得通,但是我得到的错误是:

Error in log(census$dbh[i]) :    non-numeric argument to mathematical
function

Any ideas how to fix this? 任何想法如何解决这一问题? Thanks in advance! 提前致谢!

2 things: 2件事:

1) You need to replace census$dbh[i] with census[dbh[i]] in your calculation. 1)您需要在计算中将census $ dbh [i]替换为census [dbh [i]]。 I don't fully understand why this matters, but it does. 我不完全理解为什么这很重要,但是确实如此。

2) After that, you should check where the results are going. 2)之后,您应该检查结果去向。 In your example they go to columns 32-40, but there are only 23 columns in the data.frame so R gives an error because it does not like the blank columns this will create. 在您的示例中,它们转到第32-40列,但是data.frame中只有23列,因此R给出了错误,因为它不喜欢将要创建的空白列。 For the example you need census[,23+i] for the results of the for loop to work. 对于该示例,您需要census [,23 + i]才能使for循环的结果起作用。

Use this instead: 使用此代替:

l <- log(census[,grepl("^dbh",names(census))])
Result <- census$wd * exp(-1.499 + 2.148*l + 0.207*l^2 - 0.0281*l^3) / 1000

Result: 结果:

      dbh1    dbh2 dbh3 dbh4 dbh5 dbh6 dbh7      dbh8      dbh9
1       NA      NA   NA   NA   NA   NA   NA 0.2626273 0.4272698
2 1.626766 1.68795   NA   NA   NA   NA   NA        NA        NA
3 5.558074      NA   NA   NA   NA   NA   NA        NA        NA

If you want to add this to the original dataframe, use this: 如果要将其添加到原始数据框中,请使用以下命令:

names(Result) <- paste("transformed", names(Result))
cbind(census, Result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM