我正在尝试按以下示例值进行分组,
纬度|经度| TotalGreenhouseGases |金额|分行|结束日期
-37.80144 | 144.95402 | 42965.9868 | 32549.99 |艺术与文化| 07/31/2013 12:00:00 AM
-37.80144 | 144.95402 | 43246.6716 | 32762.63 |艺术与文化| 08/30/2013 12:00:00 AM
-37.80144 | 144.95402 | 21374.1264 | 16192.52 |艺术与文化| 09/31/2013 12:00:00 AM
mapdata <- aggregate(cbind(TotalGreenhouseGases,Amount) ~ latitude+longitude,data = dt2,FUN=function(dt2) c(mn =sum(dt2), n=length(dt2) ) )
163 obs。结果创建了4个变量,现在使用plot.ly将其绘制在地图中我试图添加悬停文本,
mapdata$hover <- paste( mapdata$TotalGreenhouseGases, "CO2 Emission ",'<br>', "Resource Consumption ", mapdata$Amount)
但这会导致以下错误,
Error in `$<-.data.frame`(`*tmp*`, "hover", value = c("264.06428571 CO2 Emission <br> Resource Consumption 200", :
replacement has 326 rows, data has 163
任何人都可以让我知道我哪里出错了,或者如果它已经解决了,你能不能提供一个链接。
答案 0 :(得分:2)
我认为问题在于您创建mapdata
的方式最终会导致TotalGreenhouseGases
和Amount
的长度为2的列表。
> str(mapdata)
'data.frame': 1 obs. of 5 variables:
$ latitude : num -37.8
$ longitude : num 145
$ TotalGreenhouseGases: num [1, 1:2] 107587 3
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "mn" "n"
$ Amount : num [1, 1:2] 81505 3
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "mn" "n"
因此,如果您想在paste
函数中使用这些值的总和,则需要使用[1]索引,如果需要使用样本大小n
,则使用[2] ]。例如:
mapdata$hover <- paste( mapdata$TotalGreenhouseGases[1],
"CO2 Emission ",'<br>', "Resource Consumption ",
mapdata$Amount[1])
会给你
[1] "107586.7848 CO2 Emission <br> Resource Consumption 81505.14"