R 绘制世界银行数据:条件具有长度>;1.

R 绘制世界银行数据:条件具有长度>;1.,r,google-visualization,panel-data,R,Google Visualization,Panel Data,我试图使用googleVis R软件包绘制一些世界银行数据,但它给了我以下错误: In if (class(x[[.x]]) == "Date") as.character(x[[.x]]) else x[[.x]] : the condition has length > 1 and only the first element will be used 我使用了以下代码: # load the packages library(WDI) # package for importing

我试图使用googleVis R软件包绘制一些世界银行数据,但它给了我以下错误:

In if (class(x[[.x]]) == "Date") as.character(x[[.x]]) else x[[.x]] :
the condition has length > 1 and only the first element will be used
我使用了以下代码:

# load the packages
library(WDI) # package for importing World Bank data
library(plm) # package for working with panel data
library(googleVis) # the googleVis package

# import the data using the WDI package
LONG <- WDI(country=c("AGO","BEN","BWA","BFA","BDI"), indicator=c("SP.DYN.CBRT.IN",
"SP.DYN.TFRT.IN", "SP.POP.TOTL", "NY.GDP.PCAP.KN"), start=2005, end=2009, 
extra=FALSE)

# transform to panel format and encode the year variable as numeric
PANEL <- pdata.frame(LONG, c("country","year"))
PANEL$year <- as.numeric(as.character(PANEL$year))

# plot in a MotionChart using googleVis
MC <- gvisMotionChart(PANEL, idvar="country", timevar="year")
plot(MC)
#加载包
图书馆(WDI)#世界银行数据导入包
库(plm)#用于处理面板数据的包
图书馆(googleVis)#googleVis软件包
#使用WDI包导入数据

LONG您确实需要将数据从普通数据帧转换为面板数据帧。您可以使用数据帧参数直接调用“gvisMotionChart”

请参阅下面的代码:

library(WDI) # package for importing World Bank data
library(plm) # package for working with panel data
library(googleVis) # the googleVis package

# import the data using the WDI package
LONG <- WDI(
   country=c("AGO","BEN","BWA","BFA","BDI"), 
   indicator=c("SP.DYN.CBRT.IN", "SP.DYN.TFRT.IN", "SP.POP.TOTL", "NY.GDP.PCAP.KN"), 
   start=2005, 
   end=2009, 
   extra=FALSE)

# plot in a MotionChart using googleVis
MC <- gvisMotionChart(LONG, idvar="country", timevar="year")
plot(MC) 
library(WDI)#用于导入世界银行数据的包
库(plm)#用于处理面板数据的包
图书馆(googleVis)#googleVis软件包
#使用WDI包导入数据

在造币厂12上用R15.0长时间没有错误好吧,那一定是别的东西了。感谢您抽出时间查看!顺便说一句,我还有R2.15和最新的谷歌版本。