我有一个有三列(类别、Sub.category和Acitivty)的数据框架。我需要一个嵌套列表,并将这三个级别放到shinyTree中。
我试图匹配这个.Rds文件的格式来创建一个shinyTree。
我的完整表格是99行,我已经包含了下面的30行,但是可能需要以同样的方式在100+上完成。
到目前为止我有
activity.list <- dlply(activities, .(Category), dlply, .(Sub.category), dlply, .(as.character(Activity)))这几乎是可行的,但是嵌套列表中的第3层仍然是数据格式。如何将“活动”列保留为字符?(匹配texasCities列表中的结构)
Dataframe:
activities <- tibble::tribble(
~Category, ~Sub.category, ~Activity,
"Recreational fishing", "Boat", "Cray pot",
"Recreational fishing", "Boat", "Hand/rod & line",
"Recreational fishing", "Boat", "Cray loop",
"Recreational fishing", "Boat", "Drop net",
"Recreational fishing", "Boat", "Spear",
"Recreational fishing", "Boat", "Other",
"Recreational fishing", "Jetty", "Hand/rod & line",
"Recreational fishing", "Jetty", "Cray loop",
"Recreational fishing", "Jetty", "Drop net",
"Recreational fishing", "Jetty", "Other",
"Recreational fishing", "Beach fishing", "Hand/rod & line",
"Recreational fishing", "Beach fishing", "Cray loop",
"Recreational fishing", "Beach fishing", "Drop net",
"Recreational fishing", "Beach fishing", "Bait pump",
"Recreational fishing", "Beach fishing", "Spear",
"Recreational fishing", "Beach fishing", "Other",
"Recreational fishing", "Rock fishing", "Hand/rod & line",
"Recreational fishing", "Rock fishing", "Cray loop",
"Recreational fishing", "Rock fishing", "Drop net",
"Recreational fishing", "Rock fishing", "Spear",
"Recreational fishing", "Rock fishing", "Other",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Hand/rod & line",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Cray loop",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Spear",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Drop net",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Other",
"Recreational - other", "Motorised water sports", "Jet Skiing",
"Recreational - other", "Motorised water sports", "Boating",
"Recreational - other", "Motorised water sports", "Water skiing/Knee boarding/Wake boarding",
"Recreational - other", "Motorised water sports", "Other",
"Recreational - other", "Non-motorised water sports", "Sailing",
"Recreational - other", "Non-motorised water sports", "Surfing",
"Recreational - other", "Non-motorised water sports", "Paddle board",
"Recreational - other", "Non-motorised water sports", "Kayak/canoe/Surf ski/Rowing",
"Recreational - other", "Diving/Snorkelling", "Boat based",
"Recreational - other", "Diving/Snorkelling", "Shore based",
"Recreational - other", "Off road vehicle", "4WD",
"Recreational - other", "Off road vehicle", "Quad bike/ATV",
"Recreational - other", "Off road vehicle", "Dirt bike",
"Recreational - other", "Off road vehicle", "Land yacht",
"Recreational - other", "Off road vehicle", "Other",
"Recreational - other", "Shore based recreation", "Horse riding",
"Recreational - other", "Shore based recreation", "Walking/running",
"Recreational - other", "Shore based recreation", "Beach combing",
"Recreational - other", "Shore based recreation", "Dog Walking",
"Recreational - other", "Shore based recreation", "Photography",
"Recreational - other", "Shore based recreation", "Other",
"Recreational - other", "Other", "Whale watching",
"Recreational - other", "Other", "Bird watching",
"Recreational - other", "Other", "Seal / sea lion watching",
"Recreational - other", "Other", "Other",
"Commercial fishing", "Roes/Greenlip/Brownlip Abalone Fishery", "Hand collection (dive)",
"Commercial fishing", "South Coast Crustacean Managed Fishery (do we need to include the zones?)", "Pot fishery",
"Commercial fishing", "South Coast Estuarine Management Fishery", "Gill Net",
"Commercial fishing", "South Coast Estuarine Management Fishery", "Haul Net",
"Commercial fishing", "South Coast Salmon Managed Fishery", "Beach seine",
"Commercial fishing", "(closed fishery)", NA,
"Commercial fishing", "South Coast Purse Seine Managed Fishery", "Purse seine net",
"Commercial fishing", "Joint Authority Southern Demersal Gillnet and Demersal Longline Fishery", "Demersal gillnets",
"Commercial fishing", "Marine Aquarium Fish Managed Fishery (statewide)", NA,
"Commercial fishing", "Specimen Shell Managed Fishery (statewide)", NA,
"Commercial fishing", "Octopus Interim Managed Fishery", "Trigger trap",
"Commercial fishing", "Octopus Interim Managed Fishery", "Unbaited (shelter) pot",
"Commercial fishing", "Octopus Interim Managed Fishery", "Lobster pot (bycatch)",
"Commercial fishing", "South Coast Trawl Fishery", "Otter trawl",
"Commercial fishing", "South Coast Line and Fish Trap Managed Fishery", "Line",
"Commercial fishing", "South Coast Line and Fish Trap Managed Fishery", "Fish trap",
"Commercial fishing", "South Coast Nearshore Net Managed Fishery", "Net",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Scenic site",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Swimming",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Wildlife viewing",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Maritime heritage site",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Other",
"Commercial Tour Operator", "Charter Fishing", "Fishing site",
"Commercial Tour Operator", "Anchorages and landings", "Anchorage",
"Commercial Tour Operator", "Anchorages and landings", "Beach landings",
"Commercial Tour Operator", "Anchorages and landings", "Moorings",
"Commercial Tour Operator", "Anchorages and landings", "Fuel drop-offs",
"Commercial Tour Operator", "Anchorages and landings", "Helicopter landing site",
"Commercial Tour Operator", "Anchorages and landings", "Other",
"Commercial - other", "Film/photography", NA,
"Commercial - other", "Ferry operator", NA,
"Commercial - other", "Harbour/Marina", NA,
"Commercial - other", "Shipping", NA,
"Commercial - other", "Other", NA,
"Environmental", "Education", "School based monitoring",
"Environmental", "Education", "Other",
"Environmental", "Science/Research", "Intertidal monitoring",
"Environmental", "Science/Research", "Subtidal monitoring",
"Environmental", "Science/Research", "Experiment/Tagging",
"Environmental", "Science/Research", "BRUVS site",
"Environmental", "Science/Research", "Marine debris survey",
"Environmental", "Science/Research", "Other",
"Environmental", "Volunteer on ground works", "Community monitoring",
"Environmental", "Volunteer on ground works", "Weed control",
"Environmental", "Volunteer on ground works", "Beach clean up",
"Environmental", "Volunteer on ground works", "Marine debris survey",
"Environmental", "Volunteer on ground works", "Other",
"Environmental", "Other", "Other"
)```发布于 2021-11-03 13:15:44
下面是使用rrapply()在rrapply-package中将data.frame转化为嵌套列表的一种可能方法:
library(rrapply)
lst <- rrapply(activities, how = "unmelt")
str(lst, list.len = 3)
#> List of 6
#> $ Recreational fishing :List of 26
#> ..$ Boat : chr "Cray pot"
#> ..$ Boat : chr "Hand/rod & line"
#> ..$ Boat : chr "Cray loop"
#> .. [list output truncated]
#> $ Recreational - other :List of 25
#> ..$ Motorised water sports : chr "Jet Skiing"
#> ..$ Motorised water sports : chr "Boating"
#> ..$ Motorised water sports : chr "Water skiing/Knee boarding/Wake boarding"
#> .. [list output truncated]
#> $ Commercial fishing :List of 17
#> ..$ Roes/Greenlip/Brownlip Abalone Fishery : chr "Hand collection (dive)"
#> ..$ South Coast Crustacean Managed Fishery (do we need to include the zones?): chr "Pot fishery"
#> ..$ South Coast Estuarine Management Fishery : chr "Gill Net"
#> .. [list output truncated]
#> [list output truncated]texasCities示例包含叶节点上的所有空字符串(""),为了模仿这一点,我们可以向初始的activities data.frame中添加一个额外的(空字符串)列:
lst1 <- rrapply(cbind(activities, newcol = ""), how = "unmelt")
str(lst1, list.len = 2)
#> List of 6
#> $ Recreational fishing :List of 5
#> ..$ Boat :List of 6
#> .. ..$ Cray pot : chr ""
#> .. ..$ Hand/rod & line: chr ""
#> .. .. [list output truncated]
#> ..$ Jetty :List of 4
#> .. ..$ Hand/rod & line: chr ""
#> .. ..$ Cray loop : chr ""
#> .. .. [list output truncated]
#> .. [list output truncated]
#> $ Recreational - other :List of 6
#> ..$ Motorised water sports :List of 4
#> .. ..$ Jet Skiing : chr ""
#> .. ..$ Boating : chr ""
#> .. .. [list output truncated]
#> ..$ Non-motorised water sports:List of 4
#> .. ..$ Sailing : chr ""
#> .. ..$ Surfing : chr ""
#> .. .. [list output truncated]
#> .. [list output truncated]
#> [list output truncated]https://stackoverflow.com/questions/69804732
复制相似问题