2014-07-08 71 views
-2

我想創建一個函數,它將使用輸入變量創建一個新列並根據下標值計算所述列。在下面的示例中,我想創建一個名爲'forest_closed_start_h_1'的列,它是在start_class_01 =='forest_closed'等於以下公式時計算出來的:(start_class_01_perc * 0.01)*(ha_affect)。基於輸入變量創建和引用列的R函數

編輯:我應該提到我想要一個函數(或者甚至更好,也許是一個循環),因爲我必須計算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") 
+0

你好,你好像有'[''非常笨拙的位置。也許你可能想要一點一點地研究語言的語法? –

+0

由於語法錯誤,您發佈的代碼不會生成有效的函數。你初始化'dat $ a',但是你沒有初始化'dat $ b'或'dat $ c',但你在你的問題中使用它們。也許你只是指'b'和'c'。 – MrFlick

+0

它看起來像我正在試圖讓你自己的版本的現有功能。你見過'tapply','aggregate',包'plyr'和'dplyr'嗎? – AndrewMacDonald

回答

0

您可以subset subsetd數​​據(只得到封閉的森林),然後作出一個新列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 

如果你想0,其中森林類等於「forest_closed」,如上然後由邏輯向量乘您使用子集,你可以簡單地計算出你的新列:

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