R函数,它根据输入变量创建和引用列

时间:2014-07-08 20:21:23

标签: r function paste

我正在尝试创建一个函数,该函数将使用输入变量创建新列并根据下标值计算所述列。在下面的示例中,我想创建一个名为“forest_closed_start_h_1”的列,该列在start_class_01 =='forest_closed'等于此公式时计算:(start_class_01_perc * 0.01)*(ha_affect)。

编辑:我应该提到我想要一个函数(甚至更好,可能是一个循环),因为我必须计算相同类型数据的50个不同迭代。

这是我编写的函数,但我无法获取函数变量来填充'a','b'和'c'。我也无法获得创建新列的功能。

class_calc <- function(start_end,number,veg){
  a <- [paste (veg,start_end,'h',number,sep='_')] #create new variable (a) equal to forest_closed_start_h_1
  b <- [paste0(start_end,'_class_',number)] #create new variable (b) equal to start_class_01
  c <- [paste0(start_end,'_class_',number,'_perc')] #create new variable (c) equal to start_class_01_perc
  dat$a <- 0 #create new column from variable a, which is forest_closed_start_h_01
  dat$a[dat$b==veg]<-(dat$c[dat$b==veg]*0.01)*(dat$ha_affect[dat$b==veg]) #calculate values for a, where start_class_01==forest_closed
}

class_calc(start_end='start',number='01',veg='forest_closed')

以下是我的数据的一个子集:

structure(list(start_class_01 = c("forest_closed", "forest_closed", 
"forest_open", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "herbaceous", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_semi_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed", 
"forest_closed", "forest_closed", "forest_closed", "forest_closed"
), start_class_01_perc = c(100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 70, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100), ha_affect = c(3.87, 1.134, 1.44, 1.8, 2.43, 
40.752, 22.95, 9.432, 1.89, 1.53, 2.25, 1.08, 8.946, 3.42, 3.15, 
4.32, 5.04, 1.62, 1.17, 2.16, 2.34, 25.56, 3.51, 2.07, 3.51, 
100.17, 15.66, 2.7, 36.27, 18.36, 4.41, 23.31, 1.944, 9.18, 1.62, 
5.76, 17.37, 7.56, 1.512, 81.36, 7.2, 61.02, 21.69, 1.62, 1.26, 
5.4, 0.288, 1.08, 7.74, 1.17)), .Names = c("start_class_01", 
"start_class_01_perc", "ha_affect"), row.names = c(NA, 50L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

您可以使用subset对数据进行子集化(仅获取封闭的林),然后使用transform创建新列。

dat1 <- subset(dat,dat$start_class_01 == "forest_closed")

dat1_new <- transform(dat1,forest_closed_start_h_1 = (start_class_01_perc * 0.01) * (ha_affect))

head(dat1_new)
  start_class_01 start_class_01_perc ha_affect forest_closed_start_h_1
1  forest_closed                 100     3.870                   3.870
2  forest_closed                 100     1.134                   1.134
4  forest_closed                 100     1.800                   1.800
5  forest_closed                 100     2.430                   2.430
6  forest_closed                 100    40.752                  40.752
7  forest_closed                 100    22.950                  22.950

如果你想要森林类等于“forest_closed”的0,你可以简单地计算你的新列,然后乘以你用来分配的逻辑向量:

transform(dat,forest_closed_start_h_1 = (start_class_01_perc * 0.01) * (ha_affect) * (start_class_01 == "forest_closed"))

head(dat_new)
  start_class_01 start_class_01_perc ha_affect forest_closed_start_h_1
1  forest_closed                 100     3.870                   3.870
2  forest_closed                 100     1.134                   1.134
3    forest_open                 100     1.440                   0.000
4  forest_closed                 100     1.800                   1.800
5  forest_closed                 100     2.430                   2.430
6  forest_closed                 100    40.752                  40.752