"Argument of length zero" in R -
i have data , dont know how make plot it, wirte script change form
and here result of dput(head(e1)):
structure(list(lon = c(-26.583, -26.25, -26.417, -67.25, -67.25, -67.417), lat = c(-59.083, -58.417, -58.417, -55.917, -55.75, -55.75), pre1 = c(105.4, 106.3, 106.6, 73.1, 68.7, 70.2)), .names = c("lon", "lat", "pre1"), row.names = c(na, 6l), class = "data.frame")
the first column longitude , second 1 latitude, third 1 precipitation value of point. want make map data, dont know how deal format, want change 3 matrix: 1 longitude,one latitude, 1 precipitation, , can use function image.plot(lon,lat,pre1) make map of precipitation.
this script:
rerange<-function(e1) { latq<-sort(e1$lat,decreasing = t) latq<-as.matrix(latq) latq1<-unique.matrix(latq) lonq<-sort(e1$lon) lonq<-as.matrix(lonq) lonq1<-unique.matrix(lonq) lenlon<-length(lonq1) lenlat<-length(latq1) finalq<-matrix(0,lenlon,lenlat) (i in 1:lenlat) { (j in 1:lenlon) { finalq[i,j]<-e1$pre1[e1$lon == lonq1[j] & e1$lat == latq1[i] ] } } return(finalq) }
but return error this:
error in finalq[i, j] <- e1$pre1[e1$lon == lonq1[j] & e1$lat == latq1[i]] : replacement has length 0
i have try other ways, unable figure out error.how can fix it? assistance appreciated.
ok, checking out data, think more or less after:
# create dataframe df <- structure(list(lon = c(-26.583, -26.25, -26.417, -67.25, -67.25, -67.417), lat = c(-59.083, -58.417, -58.417, -55.917, -55.75, -55.75), pre1 = c(105.4, 106.3, 106.6, 73.1, 68.7, 70.2)), .names = c("lon", "lat", "pre1"), row.names = c(na, 6l), class = "data.frame") # change long wide format test <- reshape(df, timevar = "lat", idvar = "lon", direction = "wide") # turn missing values zeros test[is.na(test)] <- 0 # use lon rownames rownames(test) <- test[, 1] # drop first column test <- test[,c(2:5)] # load stringr library library(stringr) # drop text column names colnames(test) <- str_replace(colnames(test), "pre1.", "") # put rownames , colnames increasing order test <- test[order(rownames(test), decreasing = true), order(colnames(test), decreasing = true)] test # load plotting library library(fields) # make plot image.plot(as.numeric(rownames(test)), as.numeric(colnames(test)), as.matrix(test))
is need?
Comments
Post a Comment