使用我的数据集运行googlevis时遇到问题

时间:2016-09-23 09:54:19

标签: r

我是R编程的新手。我试图想象一些数据集。我在R中使用Googlevis并且无法将其可视化。

我得到的错误是:

  

错误:逻辑索引向量的长度必须为1或8,得到:14835

有人可以帮忙吗?

数据集在这里: https://www.kaggle.com/c/predict-west-nile-virus/data

代码低于

# Read competition data files:
library(readr)
data_dir <- "C:/Users/Wesley/Desktop/input"
train <- read_csv(file.path(data_dir, "train.csv"))
spray <- read_csv(file.path(data_dir, "spray.csv"))


# Generate output files with write_csv(), plot() or ggplot()
# Any files you write to the current directory get shown as outputs


# Install and read packages
library(lubridate)
library(googleVis)

# Create useful date columns


spray$Date <- as.Date(as.character(spray$Date),format="%Y-%m-%d")
spray$Week <- isoweek(spray$Date)
spray$Year <- year(spray$Date)


# Create a total count of measurements

spray$Total <- 1

for(i in 1:nrow(spray)) {
    spray$Total[i] = i
}



# Aggregate data by Year, Week, Trap and order by old-new


spray_agg <- aggregate(cbind(Total)~Year+Week+Latitude+Longitude,data=spray,sum)



spray_agg <- spray[order(spray$Year,spray$Week),]



# Create a misc format for Week for Google Vis Motion Chart

spray_agg$Week_Format <- paste(spray_agg$Year,"W",spray_agg$Week,sep="")



# Function to create a motion chart together with a overview table
# It takes the aggregated data as input as well as a year of choice (2007,2009,2011,2013)
# It filters out "no presence" weeks since they distort the graphical view
# Next to that it creates an overview table of that year
# With gvisMerge you can merge the 3 html outputs into 1

create_motion <- function(data=spray_agg,year=2011){

data_motion <- data[data$Year==year]
motion <- gvisMotionChart(data=data_motion,idvar="Total",timevar="Week_Format",xvar="Longitude",yvar="Latitude"
                          ,sizevar=0.1,colorvar="Blue",options=list(width="600"))



return(motion)
}

# Get the per year motion charts
#motion1 <- create_motion(spray_agg,2007)
#motion2 <- create_motion(spray_agg,2009)
motion3 <- create_motion(spray_agg,2011) : (Error: Length of logical index vector must be 1 or 8, got: 14835)
motion4 <- create_motion(spray_agg,2013) :(Error: Length of logical index vector must be 1 or 8, got: 14835)


# Merge them together into 1 dashboard
output <- gvisMerge(gvisMerge(motion1,motion2,horizontal=TRUE),gvisMerge(motion3,motion4,horizontal=TRUE),horizontal=FALSE)
plot(output)
# Plot the output in your browser

0 个答案:

没有答案