R 替换有x行,数据有y-paste()函数
我试图根据以下样本值进行分组 纬度|经度|温室气体总量|数量|分支机构|结束日期R 替换有x行,数据有y-paste()函数,r,ggplot2,R,Ggplot2,我试图根据以下样本值进行分组 纬度|经度|温室气体总量|数量|分支机构|结束日期 -37.80144 | 144.95402 | 42965.9868 | 32549.99 |艺术与文化| 2013年7月31日上午12:00:00 -37.80144 | 144.95402 | 43246.6716 | 32762.63 |艺术与文化| 2013年8月30日上午12:00:00 -37.80144 | 144.95402 | 21374.1264 | 16192.52 |艺术与文化| 2013年
-37.80144 | 144.95402 | 42965.9868 | 32549.99 |艺术与文化| 2013年7月31日上午12:00:00
-37.80144 | 144.95402 | 43246.6716 | 32762.63 |艺术与文化| 2013年8月30日上午12:00:00
-37.80144 | 144.95402 | 21374.1264 | 16192.52 |艺术与文化| 2013年9月31日上午12:00:00
mapdata <- aggregate(cbind(TotalGreenhouseGases,Amount) ~ latitude+longitude,data = dt2,FUN=function(dt2) c(mn =sum(dt2), n=length(dt2) ) )
mapdata我认为问题在于,您创建mapdata
的方式最终导致totalgreenhousegas
和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"
因此,如果要在粘贴
函数中使用这些值的总和,则需要使用[1]索引,如果需要使用样本大小n
则需要使用[2]。例如:
mapdata$hover <- paste( mapdata$TotalGreenhouseGases[1],
"CO2 Emission ",'<br>', "Resource Consumption ",
mapdata$Amount[1])
mapdata$hover您能提供'str(mapdata)'的输出吗?我得到以下'data.frame':163 obs。共4个变量:$latitude:num-37.8-37.8-37.8-37.8-37.8…$经度:数字145…$温室气体总量:num[1:163,1:2]264 1616 30480 0 3186….-attr(,“dimnames”)=2个….$的列表:空…$:chr“mn”n“$Amount:num[1:163,1:2]200 1223 23085 4659 2410…”attr(,“dimnames”)=2个….$的列表:空…$:就我所能看到的而言,整个绿色。。。Amount列的维度为2,因此粘贴调用的输出为2*163。您是否尝试将粘贴输出分配给一个新变量并检查其长度?@FlorianSchunke:是的,我尝试使用不同的变量,但仍然得到相同的结果(请参阅问题下的第三条注释;)是的,我只是在发布答案后看到的。。。你跑得更快!
> 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"
mapdata$hover <- paste( mapdata$TotalGreenhouseGases[1],
"CO2 Emission ",'<br>', "Resource Consumption ",
mapdata$Amount[1])
[1] "107586.7848 CO2 Emission <br> Resource Consumption 81505.14"