我有一个数据表,其中包含列[ID,状态,纬度,经度,价格0BR,价格1BR,价格2BR,价格3BR]。目前,我正在按单张地图中的纬度/经度映射ID,并希望按ID为0BR,1BR,2BR,3BR的ID进行过滤。值得注意的是,对于任何Price列,ID可能具有空值。
按现状,我可以使过滤器出现,但实际上不能过滤任何数据。我相信我需要让我的代码调出ID,但是我不确定如何。
df <- read.csv(file="MPLS_Saturation_Map.csv", header=TRUE, sep=",")
getColor <- function(df) {
sapply(df$Status, function(Status) {if(Status == '80% Undersaturated') {"red"} else if(Status == '50% Undersaturated') {"orange"} else {"green"}
})}
icons <- awesomeIcons(
icon = 'home',
iconColor = 'black',
library = 'ion',
markerColor = getColor(df)
)
popup <- paste(
"<strong>Rental ID: </strong>", df$Rental.ID,
"<br><strong>Total Units: </strong>", df$Units,
"<br><strong>Availability %: </strong>", df$Availability,
"<br><strong>Relative Price 0BR: </strong>", df$X0BR,
"<br><strong>Relative Price 1BR: </strong>", df$X1BR,
"<br><strong>Relative Price 2BR: </strong>", df$X2BR,
"<br><strong>Relative Price 3BR: </strong>", df$X3BR,
"<br><strong>Link: </strong>", df$Slug
)
pal <- colorFactor(c("orange", "red", "chartreuse3"),df$Status)
leaflet(df) %>%
addTiles() %>%
addLayersControl(
overlayGroups = c(if(length(df$X0BR)){"Studio"}, if(length(df$X1BR)){"1BR"}, if(length(df$X2BR)){"2BR"}, if(length(df$X3BR)){"3BR"}),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(pal = pal, title = '2018 Saturation Status', values = df$Status, opacity = 2, position = 'bottomright') %>%
addAwesomeMarkers(~Lon, ~Lat, icon = icons, popup = popup)
最初的结果显示了过滤器中的每个价格,但后来我添加了> if(length(df $ X0BR)){“ Studio”} <等,以获得正确的过滤器标签(“ Studio”,“ 1BR “,” 2BR“,” 3BR“)。现在,我只需要根据ID在df $ X0BR,df $ X1BR,df $ X2BR和df $ X3BR中是否具有价格来过滤数据集。