我有重复的纬度/长度的数据行,我想要包含一个包含所有分组列数据的标签。
# Make sample dataframe
long <- c(147.5, 147.5, 147.5, 147, 147)
lat <- c(-36.5, -36.5, -36.5, -36, -36)
label <- c(1, 1, 2, 1, 2)
markers <- data.frame(lat,long,label)
# Combine labels based on lat long
markers <- markers %>%
group_by(lat, long) %>%
summarize(concat_label = toString(label))
# Markers with all of the labels
leaflet() %>%
addTiles() %>%
addMarkers(lng=markers$long, lat= markers$lat,
popup= markers$concat_label
)
是否有使用换行符而不是逗号的toString版本?我尝试使用paste和paste0,但无法使其工作。
答案 0 :(得分:1)
您的标签变量可以是HTML字符串,因此您可以使用<br/>
标记使标签相互堆叠。例如:
library(leaflet)
long <- c(147.768, 147.768, 147.768,147.768, 147.768, 147.768)
lat <- c(-36.852, -36.852, -36.852,-36.852, -36.852, -36.852)
label <- paste(sep = "<br/>",'long label1', 'long label2', 'long label3','long label4', 'long label5', 'long label6')
markers <- data.frame(lat,long,label)
leaflet() %>%
addTiles() %>%
addMarkers(lng=markers$long, lat= markers$lat,
popup=markers$label,
)
您还可以使用相同的方法将变量提供给标签 - 逐行连接。
答案 1 :(得分:0)
# Make sample dataframe
long <- c(147.5, 147.5, 147.5, 147, 147)
lat <- c(-36.5, -36.5, -36.5, -36, -36)
label <- c(1, 1, 2, 1, 2)
markers <- data.frame(lat,long,label)
# Aggregate method
markers <- aggregate(label ~ lat + long, markers, paste, collapse = "<br/>")
# Markers with all of the labels
leaflet() %>%
addTiles() %>%
addMarkers(lng=markers$long, lat= markers$lat,
popup= markers$label
)
这个问题有答案: Collapse / concatenate / aggregate a column to a single comma separated string within each group