R函数,该函数基于输入变量创建和引用列

R函数,该函数基于输入变量创建和引用列,r,function,paste,R,Function,Paste,我试图创建一个函数,该函数将使用输入变量创建一个新列,并根据订阅值计算所述列。在下面的示例中,我想创建一个名为“forest\u closed\u start\u h\u 1”的列,该列在start\u class\u 01==“forest\u closed”等于以下公式时计算:start\u class\u 01\u perc*0.01*ha\u impact 编辑:我应该提到我想要一个函数或者更好的,可能是一个循环,因为我必须计算相同类型数据的50个不同迭代 这是我编写的函数,但我无法获得

我试图创建一个函数,该函数将使用输入变量创建一个新列,并根据订阅值计算所述列。在下面的示例中,我想创建一个名为“forest\u closed\u start\u h\u 1”的列,该列在start\u class\u 01==“forest\u closed”等于以下公式时计算:start\u class\u 01\u perc*0.01*ha\u impact

编辑:我应该提到我想要一个函数或者更好的,可能是一个循环,因为我必须计算相同类型数据的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')

您可以使用subset对数据进行子划分,以仅获取封闭林,然后使用transform创建一个新列


你好,你好像有[非常尴尬的位置。也许你想稍微温习一下语言语法?由于语法错误,你发布的代码没有生成有效的函数。你初始化了dat$a,但没有任何地方初始化dat$b或dat$c,但你在问题中使用了它们。也许你只是指b和c。在我看来,你是在试图使你的现有函数的自有版本。您看过tapply、aggregate以及包plyr和dplyr吗?@MrFlick,您是对的,该函数在其当前形式下不起作用,但我希望它能提供一个其他人可以使用的框架。关于dat$b和dat$c,我试图引用dataframe中的特定列,但不希望o将它们硬连接到函数中。相反,我认为我可以通过将函数输入的部分粘贴在一起来定义特定列。在提供的示例中,我希望dat$b定义为start_class_01,它出现在数据帧中,dat$c定义为start_class_01_perc,同样也出现在数据帧中。希望这能有所帮助。
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")
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
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