[英]How do I access a member of a list within a data.frame based on a column in the data.frame?
[英]How can I create a label/tooltip based on a list column of a data.frame
我希望在傳單中創建一個英國的選區地圖,當它懸停時提供選區名稱和詳細結果。
數據包含在包含2列的tibble中 :
我在下面列出了兩個選區的樣本
df <- structure(list(constituency = c("Knowsley", "Bristol West"),
result = list(structure(list(name = c("George Howarth", "James Spencer",
"Neil Miney", "Carl Cashman", "Steve Baines"), party = c("Labour",
"Conservative", "UKIP", "LD", "Green"), votes = c(47351L,
5137L, 1285L, 1189L, 521L), pc = c(85.34, 9.26, 2.32, 2.14,
0.94), order = 1:5), .Names = c("name", "party", "votes",
"pc", "order"), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(name = c("Thangam Debbonaire",
"Annabel Tall", "Molly Scott Cato", "Stephen Williams", "Jodian Rodgers"
), party = c("Labour", "Conservative", "Green", "LD", "Money Free Party"
), votes = c(47213L, 9877L, 9216L, 5201L, 101L), pc = c(65.93,
13.79, 12.87, 7.26, 0.14), order = 1:5), .Names = c("name",
"party", "votes", "pc", "order"), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("constituency", "result"))
如果我只是想要標簽中的選區,我會像這樣編碼,我可以應用於傳單輸出:
labels <- sprintf(
df$constituency
) %>% lapply(htmltools::HTML)
但我希望在結果細節中添加。
怎么回事?
labels = lapply(1:length(df$result), function(i) {
tmp = format(df$result[[i]])
tmp = tmp[3:length(tmp)]
tmp[1] = df$constituency[i]
htmltools::HTML(paste(tmp, collapse = "<br>"))
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.