2017-03-01 63 views
3

我有這個data.frame減去行

DATA

df <- data.frame(id=c(rep("site1", 3), rep("site2", 8), rep("site3", 9), rep("site4", 15)), 
       major_rock = c("greywacke", "mudstone", "gravel", "greywacke", "gravel", "mudstone", "gravel", "mudstone", "mudstone", 
           "conglomerate", "gravel", "mudstone", "greywacke","conglomerate", "gravel", "gravel", "greywacke","gravel", 
           "greywacke", "gravel", "mudstone", "greywacke", "gravel", "gravel", "gravel", "conglomerate", "greywacke", 
           "coquina", "gravel", "gravel", "greywacke", "gravel", "mudstone","mudstone", "gravel"), 
       minor_rock = c("sandstone mudstone basalt chert limestone", "limestone", "sand silt clay", "sandstone mudstone basalt chert limestone", 
           "sand silt clay", "sandstone conglomerate coquina tephra", NA, "limestone", "mudstone sandstone coquina limestone", 
           "sandstone mudstone limestone", "sand loess silt", "sandstone conglomerate coquina tephra", "sandstone mudstone basalt chert limestone", 
           "sandstone mudstone limestone", "sand loess silt", "loess silt sand", "sandstone mudstone conglomerate chert limestone basalt", 
           "sand silt clay", "sandstone mudstone conglomerate", "loess sand silt", "sandstone conglomerate coquina tephra", "sandstone mudstone basalt chert limestone", 
           "sand loess silt", "sand silt clay", "loess silt sand", "sandstone mudstone limestone", "sandstone mudstone conglomerate chert limestone basalt", 
           "limestone", "loess sand silt", NA, "sandstone mudstone conglomerate", "sandstone siltstone mudstone limestone silt lignite", "limestone", 
           "mudstone sandstone coquina limestone", "mudstone tephra loess"), 
       area_ha = c(1066.68, 7.59, 3.41, 4434.76, 393.16, 361.69, 306.75, 124.93, 95.84, 9.3, 8.45, 4565.89, 2600.44, 2198.52,  
          2131.71, 2050.09, 1640.47, 657.09, 296.73, 178.12, 10403.53, 8389.2, 8304.08, 3853.36, 2476.36, 2451.25,  
          1640.47, 1023.02, 532.94, 385.68, 296.73, 132.45, 124.93, 109.12, 4.87)) 

在這裏面,有4個位點,其中2個是獨立site1site3 ;它們不包括任何上游的位點)並且2個是從屬的site2site4;它們包括上游站點)

我想創建一個新的data.frame,我們稱之爲df_indep。其中,我想所有網站是獨立這意味着減去由從屬站點任何上游位點(一個或多個),如下

站點1和site3將保持不變,因爲它們是獨立

站點2(獨立)=站點2 - 站點1

site4(獨立)= site4 - (站點2 + site3)

下面是df僅適用於major_rockminor_rock組合的大於15%(減去上游站點之前; site2site3

library(dplyr) 
head(df %>% group_by(id) %>% 
     mutate(area_percent = area_ha/sum(area_ha)*100) %>% 
     filter(area_percent>5),15) 


#  id major_rock            minor_rock area_ha area_percent 
# <fctr>  <fctr>             <fctr> <dbl>  <dbl> 
#1 site1 greywacke    sandstone mudstone basalt chert limestone 1066.68 98.979289 
#2 site2 greywacke    sandstone mudstone basalt chert limestone 4434.76 77.329604 
#3 site2  gravel           sand silt clay 393.16  6.855592 
#4 site2  mudstone     sandstone conglomerate coquina tephra 361.69  6.306845 
#5 site2  gravel              NA 306.75  5.348848 
#6 site3  mudstone     sandstone conglomerate coquina tephra 4565.89 27.978879 
#7 site3 greywacke    sandstone mudstone basalt chert limestone 2600.44 15.934986 
#8 site3 conglomerate       sandstone mudstone limestone 2198.52 13.472099 
#9 site3  gravel          sand loess silt 2131.71 13.062701 
#10 site3  gravel          loess silt sand 2050.09 12.562550 
#11 site3 greywacke sandstone mudstone conglomerate chert limestone basalt 1640.47 10.052479 
#12 site4  mudstone     sandstone conglomerate coquina tephra 10403.53 25.925869 
#13 site4 greywacke    sandstone mudstone basalt chert limestone 8389.20 20.906106 
#14 site4  gravel          sand loess silt 8304.08 20.693984 
#15 site4  gravel           sand silt clay 3853.36  9.602674 

這裏是

最終結果

我減去上游網站

#  id major_rock            minor_rock area_ha area_percent 
#1 site1 greywacke    sandstone mudstone basalt chert limestone 1066.68 98.979289 
#2 site2 greywacke    sandstone mudstone basalt chert limestone 3368.08 72.319849 
#3 site2  gravel           sand silt clay 389.75  8.368762 
#4 site2  mudstone     sandstone conglomerate coquina tephra 361.69  7.766254 
#5 site2  gravel              NA 306.75  6.586576 
#6 site3  mudstone     sandstone conglomerate coquina tephra 4565.89 27.978879 
#7 site3 greywacke    sandstone mudstone basalt chert limestone 2600.44 15.934986 
#8 site3 conglomerate       sandstone mudstone limestone 2198.52 13.472099 
#9 site3  gravel          sand loess silt 2131.71 13.062701 
#10 site3  gravel          loess silt sand 2050.09 12.562550 
#11 site3 greywacke sandstone mudstone conglomerate chert limestone basalt 1640.47 10.052479 
#12 site4  mudstone     sandstone conglomerate coquina tephra 5475.95 30.297305 
#13 site4 greywacke    sandstone mudstone basalt chert limestone 1354.00  7.491403 
#14 site4  gravel          sand loess silt 6163.92 34.103701 
#15 site4  gravel           sand silt clay 2803.11 15.509031 

我會明白如何做到這一點的任何建議後想在R.

UPDATE

是表示所有4個位點

enter image description here

下圖顯示site4(累計如df)和site1(獨立的),因爲我想在最終輸出後的地圖減去site2site3

enter image description here

下圖顯示了站點2(累積)和indepenendent enter image description here

關於網站dependcies是如何編碼@ rbierman的問題是相同的,請檢查以下。

#  id dependent dep_site1 dep_site2 dep_site3 
#1 site1  no  no  no  no 
#2 site1  no  no  no  no 
#3 site1  no  no  no  no 
#4 site2  yes  yes  no  no 
#5 site2  yes  yes  no  no 
#6 site2  yes  yes  no  no 
#7 site2  yes  yes  no  no 
#8 site2  yes  yes  no  no 
#9 site2  yes  yes  no  no 
#10 site2  yes  yes  no  no 
#11 site2  yes  yes  no  no 
#12 site3  no  no  no  no 
#13 site3  no  no  no  no 
#14 site3  no  no  no  no 
#15 site3  no  no  no  no 
#16 site3  no  no  no  no 
#17 site3  no  no  no  no 
#18 site3  no  no  no  no 
#19 site3  no  no  no  no 
#20 site3  no  no  no  no 
#21 site4  yes  yes  yes  yes 
#22 site4  yes  yes  yes  yes 
#23 site4  yes  yes  yes  yes 
#24 site4  yes  yes  yes  yes 
#25 site4  yes  yes  yes  yes 
#26 site4  yes  yes  yes  yes 
#27 site4  yes  yes  yes  yes 
#28 site4  yes  yes  yes  yes 
#29 site4  yes  yes  yes  yes 
#30 site4  yes  yes  yes  yes 
#31 site4  yes  yes  yes  yes 
#32 site4  yes  yes  yes  yes 
#33 site4  yes  yes  yes  yes 
#34 site4  yes  yes  yes  yes 
#35 site4  yes  yes  yes  yes 
+0

如果你已經有了你想要的最終結果,你的問題到底是什麼? – jsb

+0

@Samuel 我只使用Microsoft Excel獲得了4個站點的最終結果。我的數據包括大約150個站點。我剛剛在這個可重複的例子中提供了4個站點,以幫助任何回答問題的人瞭解我想要的輸出。所以,我的問題是「如何使用R獲得最終結果(通過Excel獲得)?」 – aelwan

+0

您提到減法,這個操作對您的數據有什麼影響?你能否解釋一下。我仍然困惑你需要什麼幫助。 – jsb

回答

1

這不算太壞,只是稍微改名和加入。

首先,我們需要依賴性以一個很好的兩列格式。您可以使用廣泛的依賴reshape2::melttidyr::gather您發佈讓他們長:

deps = data.frame(
    id = c("site2", "site4", "site4"), 
    dependency = c("site1", "site2", "site3"), 
    stringsAsFactors = FALSE 
) 
#  id dependency 
# 1 site2  site1 
# 2 site4  site2 
# 3 site4  site3 

使用dplyr的加入,我們也想character,而不是factor列以防萬一水平是不盡相同。

library(dplyr)  
df = mutate_at(df, .cols = c("id", "major_rock", "minor_rock"), .funs = funs(as.character)) 

首先我們創建了具有區域和id(編輯)明確依賴名稱的「依賴與措施」的數據幀,然後我們把它彙總到id水平,總結了相關的領域:

dep_w_measure = df %>% 
    select(dependency = id, major_rock, minor_rock, dep_area = area_ha) %>% 
    inner_join(deps) %>% 
    group_by(id, major_rock, minor_rock) %>% 
    summarize(dep_area = sum(dep_area)) 

然後,我們加入該原始數據,並減去關閉從屬區域(如果存在):

result = left_join(df, dep_w_measure, by = c("major_rock", "minor_rock", "id")) %>% 
    mutate(area_ind = area_ha - coalesce(dep_area, 0)) 
head(result) 
#  id major_rock        minor_rock area_ha dep_area area_ind 
# 1 site1 greywacke sandstone mudstone basalt chert limestone 1066.68  NA 1066.68 
# 2 site1 mudstone         limestone 7.59  NA  7.59 
# 3 site1  gravel       sand silt clay 3.41  NA  3.41 
# 4 site2 greywacke sandstone mudstone basalt chert limestone 4434.76 1066.68 3368.08 
# 5 site2  gravel       sand silt clay 393.16  3.41 389.75 
# 6 site2 mudstone  sandstone conglomerate coquina tephra 361.69  NA 361.69 

我離開dep_areaarea_ha列以「顯示我的工作」,您可以根據需要進行清理。獨立區域area_ind列與您想要的輸出中的area_ha匹配。

+0

感謝您的時間,幫助和這個不錯的方法。它適用於site2,但我認爲它不適用於site4。例如,我期望的輸出中的site4(與major_rock「mudstone」和minor_rock「sandstone conglomerate coquina tephra」等於5475.95 ha,因爲它是site4-(site2 + site3),但在你的答案中是(site4-site2)和(site4- site3)= 10041.84+ 5837.64 = 15879.48公頃,它甚至高於site4在df中的原始累積值,等於10403.53 ha – aelwan

+0

嗯,我認爲這個方法是可以適應的,我們需要在減法之前加一個步驟, 'id','major_rock'和'minor_rock',然後求和依賴區域,然後,這個依賴區域的* sum *應該從'area_ha'中減去,我認爲這應該工作,我沒有時間寫 – Gregor

+0

非常感謝您的時間和幫助 – aelwan