如何将嵌套列表中的数据框更改为列表
How do I change a dataframe in a nested list to a list
我有一个包含三列(类别、Sub.category 和活动)的数据框。
我需要一个嵌套列表,将这三个级别放入 shinyTree。
我正在尝试匹配 this .Rds file 的格式来创建一个 shinyTree。
我的完整 table 是 99 行,我在下面包含了 30 行,但可能需要以相同的方式完成 100 多行。
到目前为止我有
activity.list <- dlply(activities, .(Category), dlply, .(Sub.category), dlply, .(as.character(Activity)))
这几乎可以工作,但嵌套列表中的第 3 层仍然是一个数据框。
如何只保留“Activity”列作为一个字符? (匹配 texasCities 列表中的结构)
数据框:
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"
)```
这是一种可能的方法,使用 rrapply
包中的 rrapply()
到 unmelt
和 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]
我有一个包含三列(类别、Sub.category 和活动)的数据框。 我需要一个嵌套列表,将这三个级别放入 shinyTree。
我正在尝试匹配 this .Rds file 的格式来创建一个 shinyTree。
我的完整 table 是 99 行,我在下面包含了 30 行,但可能需要以相同的方式完成 100 多行。
到目前为止我有
activity.list <- dlply(activities, .(Category), dlply, .(Sub.category), dlply, .(as.character(Activity)))
这几乎可以工作,但嵌套列表中的第 3 层仍然是一个数据框。 如何只保留“Activity”列作为一个字符? (匹配 texasCities 列表中的结构)
数据框:
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"
)```
这是一种可能的方法,使用 rrapply
包中的 rrapply()
到 unmelt
和 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]