说我下载了一个名为econ_dmg.csv
的数据集:
fileUrl <- "https://raw.githubusercontent.com/MichaelSodeke/DataSets/main/econ_dmg.csv"
download.file(fileUrl, destfile="econ_dmg.csv", method = "curl", mode="wb")
df <- read_csv("econ_dmg.csv")
当前顺序是降序排列(按max.net.loss
),我想在使用group_split
之后保留该顺序:
> df
# A tibble: 91 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AM WATERSPOUT 5000
2 IL FLASH FLOOD 5000
3 NC THUNDERSTORM WIND 5000
4 PR LANDSLIDE 4800
5 TN TORNADO 4410
6 OK THUNDERSTORM WIND 3500
7 CT THUNDERSTORM WIND 3200
8 IN FLOOD 3000
9 KY FLASH FLOOD 3000
10 CA LANDSLIDE 1584
# ... with 81 more rows
然后我将元素按这样分组:
df2 <- df %>% group_by(state, max.net.loss)
group_by
保留顺序:
> df2
# A tibble: 91 x 3
# Groups: state, max.net.loss [72]
state evtype max.net.loss
<fct> <fct> <dbl>
1 AM WATERSPOUT 5000
2 IL FLASH FLOOD 5000
3 NC THUNDERSTORM WIND 5000
4 PR LANDSLIDE 4800
5 TN TORNADO 4410
6 OK THUNDERSTORM WIND 3500
7 CT THUNDERSTORM WIND 3200
8 IN FLOOD 3000
9 KY FLASH FLOOD 3000
10 CA LANDSLIDE 1584
# ... with 81 more rows
接下来,我将数据框转换为具有以下内容的列表:
df3 <- group_split(df2)
但是,group_split
不遵守排序,因此按state
变量而不是max.net.loss
进行排序:
> df3
<list_of<
tbl_df<
state : factor<00fd4>
evtype : factor<70f44>
max.net.loss: double
>
>[72]>
[[1]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AK COASTAL FLOOD 1000
[[2]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AL TORNADO 960
[[3]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AM WATERSPOUT 5000
[[4]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AN MARINE THUNDERSTORM WIND 100
[[5]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AR TORNADO 950
[[6]]
# A tibble: 2 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AS HURRICANE 500
2 AS TROPICAL STORM 500
[[7]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 AZ HAIL 900
[[8]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 CA LANDSLIDE 1584
[[9]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 CO HIGH WIND 979
[[10]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 CT THUNDERSTORM WIND 3200
[[11]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 DC TSTM WIND 600
[[12]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 DE LIGHTNING 800
[[13]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 FL FLOOD 935
[[14]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 GA ICE STORM 952.
[[15]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 GM MARINE TSTM WIND 100
[[16]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 GU STORM SURGE/TIDE 900
[[17]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 HI WILDFIRE 950
[[18]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 IA ICE STORM 989
[[19]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 ID FLASH FLOOD 800
[[20]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 IL FLASH FLOOD 5000
[[21]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 IN FLOOD 3000
[[22]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 KS WILDFIRE 1000
[[23]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 KY FLASH FLOOD 3000
[[24]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LA ICE STORM 900
[[25]]
# A tibble: 3 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LC MARINE HAIL 0
2 LC MARINE THUNDERSTORM WIND 0
3 LC MARINE TSTM WIND 0
[[26]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LE MARINE THUNDERSTORM WIND 25
[[27]]
# A tibble: 4 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LH MARINE HAIL 0
2 LH MARINE THUNDERSTORM WIND 0
3 LH MARINE TSTM WIND 0
4 LH WATERSPOUT 0
[[28]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LM MARINE TSTM WIND 500
[[29]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LO MARINE TSTM WIND 25
[[30]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 LS MARINE TSTM WIND 400
[[31]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MA HIGH WIND 900
[[32]]
# A tibble: 2 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MD FLASH FLOOD 900
2 MD TROPICAL STORM 900
[[33]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 ME FLOOD 954
[[34]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MH HIGH SURF 5
[[35]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MI FLASH FLOOD 970
[[36]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MN FLOOD 996
[[37]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MO FLOOD 940
[[38]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MS FLOOD 955
[[39]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 MT HAIL 900
[[40]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NC THUNDERSTORM WIND 5000
[[41]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 ND FLOOD 975
[[42]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NE FLOOD 897
[[43]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NH FLOOD 945
[[44]]
# A tibble: 2 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NJ COASTAL FLOOD 900
2 NJ FLOOD 900
[[45]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NM THUNDERSTORM WIND 800
[[46]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NV FLOOD 973
[[47]]
# A tibble: 2 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 NY FLOOD 975
2 NY TSTM WIND 975
[[48]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 OH HIGH WIND 995
[[49]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 OK THUNDERSTORM WIND 3500
[[50]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 OR FLOOD 688
[[51]]
# A tibble: 5 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 PA FLASH FLOOD 900
2 PA HEAVY SNOW 900
3 PA THUNDERSTORM WINDS 900
4 PA TORNADO 900
5 PA WINTER STORM 900
[[52]]
# A tibble: 3 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 PH MARINE STRONG WIND 0
2 PH MARINE TSTM WIND 0
3 PH WATERSPOUT 0
[[53]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 PK MARINE HIGH WIND 15
[[54]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 PM WATERSPOUT 0
[[55]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 PR LANDSLIDE 4800
[[56]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 PZ MARINE STRONG WIND 25
[[57]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 RI TSTM WIND 750
[[58]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 SC TORNADO 1000
[[59]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 SD TORNADO 900
[[60]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 SL MARINE TSTM WIND 15
[[61]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 ST STRONG WINDS 0
[[62]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 TN TORNADO 4410
[[63]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 TX TSTM WIND 990
[[64]]
# A tibble: 2 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 UT FLASH FLOOD 900
2 UT WINTER STORM 900
[[65]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 VA FLASH FLOOD 900
[[66]]
# A tibble: 2 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 VI HEAVY RAIN 500
2 VI HIGH WINDS 500
[[67]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 VT FLASH FLOOD 800
[[68]]
# A tibble: 3 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 WA FLOOD 900
2 WA LIGHTNING 900
3 WA WILD/FOREST FIRE 900
[[69]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 WI FLOOD 975
[[70]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 WV FLASH FLOOD 950
[[71]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 WY HAIL 800
[[72]]
# A tibble: 1 x 3
state evtype max.net.loss
<fct> <fct> <dbl>
1 XX MARINE THUNDERSTORM WIND 0
请帮助我修复。非常感谢!
答案 0 :(得分:1)
如果要保留顺序,可以将state
的{{1}}列转换为factor
,因为它们默认出现在数据中,并按字母顺序排序。
levels