我有这个数据帧(test
),它具有100个地址+计数+ place_names
:
structure(list(Address = c("1200 BROOKS AV, Rochester", "35 GLENDALE PK, Rochester",
"LAKE AV/W RIDGE RD, Rochester", "271 GREECE RIDGE CENTER DR, Greece",
"DEWEY AV/W RIDGE RD, Rochester", "LONG POND RD/W RIDGE RD, Greece",
"E HENRIETTA RD/JEFFERSON RD, Henrietta", "PENFIELD RD/RT 250, Penfield",
"2150 CHILI AV, Gates", "DRIVING PARK AV/LAKE AV, Rochester",
"WB RT 490 AT RT 390, Gates", "1490 HUDSON AV, Rochester", "NB RT 590 AT EMPIRE BL, Irondequoit",
"SB RT 590 AT EMPIRE BL, Irondequoit", "3349 MONROE AV, Pittsford",
"2833 W RIDGE RD, Greece", "LYELL AV/MT READ BL, Rochester",
"W HENRIETTA RD/JEFFERSON RD, Henrietta", "WB RT 490 AT RT 590, Brighton",
"LONG POND RD/RIDGEWAY AV, Greece", "525 TITUS AV, Irondequoit",
"LATTA RD/LONG POND RD, Greece", "00 RT 104, Webster", "LEXINGTON AV/MT READ BL, Rochester",
"SB RT 590 AT BROWNCROFT BL, Rochester", "1750 EAST AV, Rochester",
"WB RT 490 AT MT READ BL, Rochester", "HUDSON AV/E RIDGE RD, Irondequoit",
"1200 MARKETPLACE DR, Henrietta", "59 GLEN RD, Brighton", "1555 LONG POND RD, Greece",
"1425 PORTLAND AV, Rochester", "900 HOLT RD, Webster", "ON W RIDGE RD AT RT 390, Greece",
"NB RT 390 AT CHILI AV, Gates", "CLOVER ST/MONROE AV, Brighton",
"CULVER RD/E RIDGE RD, Irondequoit", "EB RT 490 AT RT 390, Gates",
"00 RT 104, Irondequoit", "ELMWOOD AV/MT HOPE AV, Rochester",
"3333 W HENRIETTA RD, Henrietta", "ON E HENRIETTA RD AT RT 390, Brighton",
"2301 LYELL AV, Gates", "SB RT 590 AT MONROE AV, Brighton", "1341 WESTFALL RD, Brighton",
"3800 DEWEY AV, Greece", "EB RT 104 AT BAY BRIDGE, Irondequoit",
"NB RT 390 AT RT 490, Gates", "NB RT 590 AT RT 490, Rochester",
"SB RT 590 AT RT 490, Rochester", "EB RT 490 AT RT 590, Rochester",
"NB RT 390 AT SCOTTSVILLE RD, Chili", "FETZNER RD/W RIDGE RD, Greece",
"EB RT 490 AT MT READ BL, Rochester", "NB RT 590 AT MONROE AV, Brighton",
"3195 MONROE AV, Pittsford", "NB RT 590 AT BROWNCROFT BL, Brighton",
"SB RT 390 AT RT 490, Gates", "ON N GOODMAN ST AT RT 104, Irondequoit",
"BRIGHTON HENRTA T L RD/W HENRIETTA RD,", "SB RT 390 AT BROOKS AV, Gates",
"SB RT 390 AT LYELL AV, Gates", "1000 E HENRIETTA RD, Brighton",
"LAKE AV/RIDGEWAY AV, Rochester", "JEFFERSON RD/S WINTON RD, Henrietta",
"EB RT 490 AT S GOODMAN ST, Rochester", "601 ELMWOOD AV, Rochester",
"MAIDEN LA/MT READ BL, Greece", "NB RT 390 AT LYELL AV, Gates",
"630 N CLINTON AV, Rochester", "EB RT 490 AT CULVER RD, Rochester",
"MT READ BL/RIDGEWAY AV, Rochester", "EB RT 104 AT N GOODMAN ST, Irondequoit",
"ST PAUL ST/UPPER FALLS BL, Rochester", "NB RT 390 AT W RIDGE RD, Greece",
"EMPIRE BL/PLANK RD, Penfield", "WB RT 104 AT BAY BRIDGE, Webster",
"DEWEY AV/STONE RD, Greece", "NB RT 390 AT LEXINGTON AV, Gates",
"SB RT 390 AT CHILI AV, Gates", "EB RT 104 AT BAY RD, Webster",
"CLIFFORD AV/PORTLAND AV, Rochester", "NB RT 390 AT BROOKS AV, Gates",
"N CLINTON AV/UPPER FALLS BL, Rochester", "WB RT 490 AT BUSHNELL BASIN EXIT, Perinton",
"SB RT 390 AT LEXINGTON AV, Gates", "WB RT 490 AT RT 531, Gates",
"CLOVER ST/W JEFFERSON RD, Pittsford", "JEFFERSON RD/E RIVER RD, Henrietta",
"SB RT 390 AT SCOTTSVILLE RD, Chili", "LINDEN AV/RT 441, Pittsford",
"AYRAULT RD/TURK HILL RD, Perinton", "1000 RIDGE RD, Webster",
"WB RT 104 AT N GOODMAN ST, Irondequoit", "1955 EMPIRE BL, Penfield",
"NB RT 390 AT VINTAGE LA, Greece", "EB RT 490 AT INNER LP, Rochester",
"W HENRIETTA RD/LEHIGH STATION RD, Henrietta", "FIVE MILE LINE RD/RIDGE RD, Webster",
"N GOODMAN ST/E MAIN ST, Rochester"), Count = c(5579L, 1440L,
1141L, 1059L, 1015L, 924L, 851L, 836L, 825L, 808L, 798L, 785L,
749L, 748L, 744L, 741L, 734L, 708L, 704L, 699L, 676L, 672L, 670L,
670L, 668L, 667L, 663L, 661L, 643L, 642L, 641L, 629L, 621L, 619L,
617L, 615L, 611L, 610L, 606L, 605L, 603L, 594L, 585L, 582L, 573L,
571L, 564L, 557L, 555L, 554L, 553L, 553L, 552L, 551L, 551L, 550L,
548L, 540L, 533L, 532L, 531L, 524L, 523L, 523L, 522L, 518L, 517L,
512L, 505L, 503L, 497L, 492L, 491L, 490L, 488L, 481L, 478L, 476L,
475L, 475L, 473L, 470L, 470L, 467L, 465L, 461L, 460L, 459L, 459L,
457L, 456L, 450L, 447L, 446L, 445L, 442L, 437L, 436L, 435L, 434L
), place_names = list(structure(list(name = c("Rochester", "Dunkin'"
)), class = "data.frame", row.names = 1:2), structure(list(name = c("Rochester",
"Edgerton")), class = "data.frame", row.names = 1:2), structure(list(
name = c("55 W Ridge Rd", "Rochester")), class = "data.frame", row.names = 1:2),
structure(list(name = c("Greece", "Bath & Body Works", "Metro by T-Mobile",
"Hickory Farms")), class = "data.frame", row.names = c(NA,
4L)), structure(list(name = c("West Ridge Road", "Rochester"
)), class = "data.frame", row.names = 1:2), structure(list(
name = c("2563-2537 W Ridge Rd", "Greece")), class = "data.frame", row.names = 1:2),
structure(list(name = "East Henrietta Road"), class = "data.frame", row.names = 1L),
structure(list(name = c("Penfield Road", "Events by Jen")), class = "data.frame", row.names = 1:2),
structure(list(name = c("Gates", "Jackson Hewitt Tax Service",
"Dunkin'", "Regal Nails Salon & Spa", "Walmart Bakery", "TNT Fireworks"
)), class = "data.frame", row.names = c(NA, 6L)), structure(list(
name = c("Lake Avenue", "Rochester")), class = "data.frame", row.names = 1:2),
"No Match", structure(list(name = c("Rochester", "Walmart Supercenter",
"Jackson Hewitt Tax Service", "West Irondequoit")), class = "data.frame", row.names = c(NA,
4L)), "No Match", "No Match", structure(list(name = c("Barnes & Noble",
"Trader Joe's", "Michaels", "Block Advisors", "Orvis", "Pittsford Plaza",
"Matrix Security Services, Inc.", "Next Revolution Strategies L.L.C.",
"Michael Nelkin - Citizens Bank, Home Mortgages", "uBreakiFix (Coming Soon)",
"Davide Turco: Allstate Insurance", "Elite Tailor Inc.")), class = "data.frame", row.names = c(NA,
12L)), structure(list(name = c("Greece", "Citizens Bank",
"Four Seasons Spirits & Wine", "Hearing Help", "Leap N' Laugh",
"Spirit Halloween", "Campbell Services, LLC", "Wegmans Bakery",
"HomeGoods", "Wegmans Sub Shop")), class = "data.frame", row.names = c(NA,
10L)), structure(list(name = c("1274-1308 Lyell Ave", "Rochester"
)), class = "data.frame", row.names = 1:2), structure(list(
name = "Dunn Tire"), class = "data.frame", row.names = 1L),
structure(list(name = c("New York 590", "Irondequoit")), class = "data.frame", row.names = 1:2),
structure(list(name = c("1620-1600 Long Pond Rd", "Long Pond & Ridgeway"
)), class = "data.frame", row.names = 1:2), structure(list(
name = c("Irondequoit", "Big Lots", "Little Caesars Pizza",
"KeyBank", "Dollar General", "Wegmans Bakery", "higi",
"Wegmans Sub Shop", "Wegmans Catering", "West Irondequoit"
)), class = "data.frame", row.names = c(NA, 10L)), structure(list(
name = "3162-3164 NY-18"), class = "data.frame", row.names = 1L),
structure(list(name = "Empire Visionworks"), class = "data.frame", row.names = 1L),
structure(list(name = c("Mount Read Boulevard", "Rochester"
)), class = "data.frame", row.names = 1:2), "No Match", structure(list(
name = c("Rochester", "Helen M. Malahosky, RPH", "Cardtronics",
"Paul Taranto, RPH", "Bridget M. Barvian, RPH", "John Crisafulli, RPH",
"Barbone Katie E", "Kristin M. Veley, PharmD", "Wegmans Bakery",
"Wegmans Pizza", "Dedes Natasha A", "Wegmans Sub Shop",
"Wegmans Catering", "East Avenue")), class = "data.frame", row.names = c(NA,
14L)), "No Match", structure(list(name = c("661-673 Ridge Rd",
"Irondequoit")), class = "data.frame", row.names = 1:2),
structure(list(name = c("Jackson Hewitt Tax Service", "Walmart Grocery Pickup and Delivery"
)), class = "data.frame", row.names = 1:2), structure(list(
name = "Brighton"), class = "data.frame", row.names = 1L),
structure(list(name = c("Timothy R. McCormick Transitional Care Center at Park Ridge Living Center",
"Unity Hospital - Emergency Center", "Unity Cardiology Group",
"Golisano Restorative Neurology & Rehabilitation Center",
"L. M. Salmen Surgical Center", "Unity Hospital - Intensive Care Unit",
"Andrew J. Kirch Dialysis Center - Park Ridge", "Charles J. August Joint Replacement Center",
"Unity Hospital", "Lipson Cancer Institute - Unity Hospital",
"Imaging Center - Unity Hospital", "The Sands-Constellation Heart Institute - Unity Hospital",
"Women's Continence Center Of Greater Rochester - West Side"
)), class = "data.frame", row.names = c(NA, 13L)), structure(list(
name = c("Rochester", "Laboratory Service Center - Rochester General Hospital",
"Imelda C MacDonald, MD", "Hyun Jin Yoo, MD", "Hassan Jamil, DO",
"James Patrick Cullinan, DO", "Jane Laura Kjoller, MD",
"Group 14621")), class = "data.frame", row.names = c(NA,
8L)), structure(list(name = c("Wegmans Bakery", "Wegmans Sub Shop",
"Wegmans Catering")), class = "data.frame", row.names = c(NA,
3L)), "No Match", "No Match", structure(list(name = c("2876-2866 Monroe Ave",
"Brighton")), class = "data.frame", row.names = 1:2), structure(list(
name = c("2098-2080 Ridge Rd", "Rochester")), class = "data.frame", row.names = 1:2),
structure(list(name = "New York 390"), class = "data.frame", row.names = 1L),
structure(list(name = c("Irondequoit - Wayne County Expressway",
"Irondequoit Bay Bridge", "Bay Bridge")), class = "data.frame", row.names = c(NA,
3L)), structure(list(name = c("Mount Hope Avenue", "Rochester"
)), class = "data.frame", row.names = 1:2), structure(list(
name = c("JOANN Fabrics and Crafts", "Red Wing", "Dollar Tree",
"Payless ShoeSource", "South Town Plaza", "Lina Esther Vega, DDS",
"Lisa Kandella, DMD", "Bank of America Mortgage")), class = "data.frame", row.names = c(NA,
8L)), structure(list(name = c("Rochester", "DiBella's Subs",
"Strong")), class = "data.frame", row.names = c(NA, 3L)),
structure(list(name = c("Gates-North Gates", "Wegmans Pizza",
"Wegmans Bakery", "Wegmans Sub Shop", "Holiday inn", "Gates"
)), class = "data.frame", row.names = c(NA, 6L)), "No Match",
structure(list(name = "Brighton"), class = "data.frame", row.names = 1L),
structure(list(name = c("minuteKEY", "Quick-Tag")), class = "data.frame", row.names = 1:2),
structure(list(name = c("Irondequoit - Wayne County Expressway",
"Irondequoit Bay Bridge", "Bay Bridge")), class = "data.frame", row.names = c(NA,
3L)), "No Match", "No Match", structure(list(name = c("New York 590",
"Rochester")), class = "data.frame", row.names = 1:2), "No Match",
structure(list(name = c("Chili", "Scottsville-Rt 390 Entrance NB"
)), class = "data.frame", row.names = 1:2), structure(list(
name = c("1932 W Ridge Rd", "Greece")), class = "data.frame", row.names = 1:2),
structure(list(name = c("Mount Read Boulevard", "Jewelry and Coin Exchange",
"Rochester")), class = "data.frame", row.names = c(NA, 3L
)), "No Match", structure(list(name = c("Cardtronics ATM",
"Kara Cilano, RPh", "Emilia Kuczkowski, RPH", "John Coultry, RPH",
"Marisa Marchese, Pharm. D.", "Wegmans Sub Shop", "Wegmans Catering",
"Wegmans Bakery")), class = "data.frame", row.names = c(NA,
8L)), "No Match", "No Match", structure(list(name = c("Irondequoit",
"IHOP", "East Irondequoit")), class = "data.frame", row.names = c(NA,
3L)), structure(list(name = c("6984-6924 NY-15", "2 Brooktree Terrace Macedon"
)), class = "data.frame", row.names = 1:2), structure(list(
name = "New York 390"), class = "data.frame", row.names = 1L),
"No Match", structure(list(name = c("Brighton", "Pioneer Hall",
"Alexander Hall", "Samuel J. Stabins Physical Education Complex",
"CSEA Unit 7402", "M&T Bank ATM")), class = "data.frame", row.names = c(NA,
6L)), structure(list(name = c("1470-1492 Lake Ave", "Rochester"
)), class = "data.frame", row.names = 1:2), structure(list(
name = "1585-1599 W Jefferson Rd"), class = "data.frame", row.names = 1L),
"No Match", structure(list(name = c("Rochester", "UR Medicine Oncology – David Korones, MD",
"UR Medicine Oncology – Michelle Shayne, MD", "UR Medicine Oncology – Rabih Salloum, MD",
"UR Medicine Oncology – Matthew Miller, MD", "UR Medicine Oncology – Randeep Kashyap, MD",
"UR Medicine Oncology – Chunkit Fung, MD", "UR Medicine Oncology – Kristen O'Dwyer, MD",
"UR Medicine Oncology – Alissa Huston, MD", "UR Medicine Oncology – Paul van der Sloot, MD",
"UR Medicine Oncology – Fergal Fleming, MBBCh", "UR Medicine Oncology – Mark Orloff, MD",
"UR Medicine Oncology – Michael Becker, MD", "UR Medicine Oncology – Jane Liesveld, MD",
"UR Medicine Heart & Vascular – Peter Knight, MD", "UR Medicine Heart & Vascular – Eugene Storozynsky, MD",
"UR Medicine Heart & Vascular – Liz Powley, NP", "UR Medicine Heart & Vascular – Joseph Delehanty, MD",
"UR Medicine Heart & Vascular – Spencer Rosero, MD", "Strong"
)), class = "data.frame", row.names = c(NA, 20L)), structure(list(
name = "3559-3545 County Rd 150"), class = "data.frame", row.names = 1L),
"No Match", structure(list(name = c("Rochester", "Police Department-Patrol Section Office",
"Upper Falls")), class = "data.frame", row.names = c(NA,
3L)), "No Match", structure(list(name = "1431-1301 County Rd 111"), class = "data.frame", row.names = 1L),
"No Match", structure(list(name = c("631-607 St Paul St",
"Rochester")), class = "data.frame", row.names = 1:2), "No Match",
structure(list(name = "1634-1650 Empire Blvd"), class = "data.frame", row.names = 1L),
structure(list(name = c("Irondequoit - Wayne County Expressway",
"Irondequoit Bay Bridge", "Bay Bridge")), class = "data.frame", row.names = c(NA,
3L)), structure(list(name = "2954-2958 NY-18"), class = "data.frame", row.names = 1L),
"No Match", "No Match", "No Match", structure(list(name = c("1176-1156 Clifford Ave",
"Rochester")), class = "data.frame", row.names = 1:2), structure(list(
name = "New York 390"), class = "data.frame", row.names = 1L),
structure(list(name = c("663-643 N Clinton Ave", "Rochester"
)), class = "data.frame", row.names = 1:2), "No Match", structure(list(
name = c("1735-1761 Massachusetts Ave", "Marihuana Office Delivery",
"Lexington")), class = "data.frame", row.names = c(NA,
3L)), "No Match", structure(list(name = "2813-2849 NY-65"), class = "data.frame", row.names = 1L),
structure(list(name = c("West Jefferson Road", "Brighton"
)), class = "data.frame", row.names = 1:2), structure(list(
name = c("Chili", "Scottsville-Rt 390 Exit SB")), class = "data.frame", row.names = 1:2),
structure(list(name = "Route 441"), class = "data.frame", row.names = 1L),
structure(list(name = "Turk Hill Road"), class = "data.frame", row.names = 1L),
"No Match", "No Match", structure(list(name = c("Wegmans Bakery",
"Jill Graham, RPh", "Christine Ling, RPH", "Eve Blake, LCSW",
"Maria Fatiga, PharmD", "Wegmans Sub Shop", "Julie Taylor, RPH",
"Shannon Kozarits, RPH", "Wegmans Catering")), class = "data.frame", row.names = c(NA,
9L)), structure(list(name = "New York 390"), class = "data.frame", row.names = 1L),
"No Match", structure(list(name = c("West Henrietta Road",
"Henrietta")), class = "data.frame", row.names = 1:2), structure(list(
name = "830-828 Ridge Rd"), class = "data.frame", row.names = 1L),
structure(list(name = c("1049-1065 E Main St", "Rochester"
)), class = "data.frame", row.names = 1:2))), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -100L))
在place_names
列中,我们有带有name
列的数据帧和几个表示Addresses
名称的值。如何拆卸此name
列,以使place_names
列的第一行为Rochester, Dunkin
,第二行为Rochester, Edgerton
,第三行为55 W Ridge Rd, Rochester
行等...
答案 0 :(得分:1)
怎么样?
假设data.frame称为d
d$pn <- sapply(d$place_names, function(x) paste(x[[1]], collapse = ', '))
答案 1 :(得分:0)
一个选项是flatten
'place_names'和paste
一起按“地址”分组之后
library(tidyverse)
test %>%
group_by(Address) %>%
summarise(name = toString(flatten_chr(flatten(place_names))))
# A tibble: 100 x 2
# Address name
# <chr> <chr>
# 1 00 RT 104, Irondequoit Irondequoit - Wayne County Expressway, Irondequoit Bay Bridge, Bay Bridge
# 2 00 RT 104, Webster Empire Visionworks
# 3 1000 E HENRIETTA RD, Brig… Brighton, Pioneer Hall, Alexander Hall, Samuel J. Stabins Physical Education Complex, CSEA Unit 7402, M&T…
# 4 1000 RIDGE RD, Webster No Match
# 5 1200 BROOKS AV, Rochester Rochester, Dunkin'
# 6 1200 MARKETPLACE DR, Henr… Jackson Hewitt Tax Service, Walmart Grocery Pickup and Delivery
# 7 1341 WESTFALL RD, Brighton Brighton
# 8 1425 PORTLAND AV, Rochest… Rochester, Laboratory Service Center - Rochester General Hospital, Imelda C MacDonald, MD, Hyun Jin Yoo, …
# 9 1490 HUDSON AV, Rochester Rochester, Walmart Supercenter, Jackson Hewitt Tax Service, West Irondequoit
#10 1555 LONG POND RD, Greece Timothy R. McCormick Transitional Care Center at Park Ridge Living Center, Unity Hospital - Emergency Cen…
# … with 90 more rows
答案 2 :(得分:0)
library(tidyverse)
test %>%
mutate(place_names = map(place_names, unlist)) %>% # update to a list of character values
unnest() %>% # unnest that columns
group_by(Address) %>% # for each address
summarise(name = paste0(place_names, collapse = ", ")) # combine names
# # A tibble: 100 x 2
# Address name
# <chr> <chr>
# 1 00 RT 104, Irondequoit Irondequoit - Wayne County Expressway, Irondequoit Bay Bridge, Bay Bridge
# 2 00 RT 104, Webster Empire Visionworks
# 3 1000 E HENRIETTA RD, Bri~ Brighton, Pioneer Hall, Alexander Hall, Samuel J. Stabins Physical Education Complex,~
# 4 1000 RIDGE RD, Webster No Match
# 5 1200 BROOKS AV, Rochester Rochester, Dunkin'
# 6 1200 MARKETPLACE DR, Hen~ Jackson Hewitt Tax Service, Walmart Grocery Pickup and Delivery
# 7 1341 WESTFALL RD, Bright~ Brighton
# 8 1425 PORTLAND AV, Roches~ Rochester, Laboratory Service Center - Rochester General Hospital, Imelda C MacDonald~
# 9 1490 HUDSON AV, Rochester Rochester, Walmart Supercenter, Jackson Hewitt Tax Service, West Irondequoit
# 10 1555 LONG POND RD, Greece Timothy R. McCormick Transitional Care Center at Park Ridge Living Center, Unity Hosp~
# # ... with 90 more rows