1
df <- read.csv(url("https://www.dropbox.com/s/uaivja22czx2pe8/df_stats_question.csv?raw=1"))
創建不同層次的EVT
#for example "0-15", "15-30", "30-60", ">60"
library(dplyr)
df <- df %>%
mutate(EVT_mod = ifelse (EVT <= 15, "0-15",
ifelse(EVT <= 30, "15-30",
ifelse(EVT <= 60, "30-60", ">60"))))
我要做些什麼?
每個區域(Zone1
到Zone5
),我想要得到的param1
和param2
不同組合的總區域面積的百分比,這percent_area
的每個級別中EVT_mod
例分佈輸出
#I want the output to be as below
#ID param1 param2 percent_area 0-15 15-30 30-60 >60
#zone1 High High 10 2 3 4 1
#zone1 High Medium 5 0.5 2 0.5 2
#zone1 High Low 15 3 4 5 3
#zone1 Medium High 9 3 2 3 1
#zone1 Medium Medium 11 2 3 4 2
#zone1 Medium Low 8 0.7 0.3 3 4
#zone1 Low High 7 0.9 1.1 3 2
#zone1 Low Medium 23 8 7 5 3
#zone1 Low Low 12 7 2 1 2
我幹了什麼?
#I got the percent of area for each zone like below
df1 <- df %>%
dplyr::select(ID, param1, param2, area) %>%
dplyr::arrange(ID, param1, param2) %>%
dplyr::group_by(ID, param1, param2) %>%
dplyr::summarise(area = sum(area)) %>%
dplyr::group_by(ID) %>%
dplyr::mutate(percent_area = area/sum(area) * 100)
head(df1)
# ID param1 param2 area percent_area
# <fctr> <fctr> <fctr> <dbl> <dbl>
#1 Zone1 High High 1247.26891 1.60636374
#2 Zone1 High Low 4725.57502 6.08609125
#3 Zone1 High Medium 10.06087 0.01295744
#4 Zone1 Low High 1432.38859 1.84478029
#5 Zone1 Medium High 44907.15570 57.83614608
#6 Zone1 Medium Low 22036.19702 28.38052622
問題
任何建議如何獲得percent_area
每個EVT_mod
水平的分佈可以理解的?
非常感謝您的時間和幫助。 – aelwan