[R] filling a matrix with data
antonio rodriguez
yerupaja01 at terra.es
Thu Mar 16 19:38:07 CET 2006
aw2471 at bristol.ac.uk wrote:
> Hi,
>
> I've extracted netcdf data in R, and am trying to output it in a matrix
> format. I can make the matrix, and I have the data, but I can't get the
> matrix to include the data...
>
> I'm not very experienced at using R, so please forgive the crudeness of
> these codes...
>
> # For the matrix
>
> wind_dir_data<-matrix( NA, 17, 365 , byrow = FALSE )
>
> wind_speed_data<-matrix( NA, 17, 365, byrow = FALSE )
>
> # to put the data into the matrix
>
> wind_dir_data[level, ]<-wind_dir_corrected
>
> wind_speed_data[level, ]<-rwind
>
> #This is the response from R: "Error in "[<-"(`*tmp*`, level, , value =
> c(-0.889991788193583, -1.45999177545309, :
> number of items to replace is not a multiple of replacement length"
>
>
> If I try to call the data using wind_dir_data[1, ], i get a matrix full
> of NA, so the data hasn't transferred.
>
> Does anyone know what I'm doing wrong?
>
> Many thanks,
>
> Amy
Hope this helps:
library(netCDF)
rm(list=ls(all=TRUE))
#for example COADS data
sst<-read.netCDF("/home/toni/Tesis/COADS/sst.cdf")
str(sst)
a<-sst$"SST"
a[a == attributes(a)$"missing_value"] <- NA
dimnames(a) <- list(1:456,sst$"LAT125_128",sst$"LON351_355")
^ ^
^ ^
looking at str(sst) you get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note that 1:456 is the time length of your data
If you want to make a contour plot:
contour(a[1,,])
If you want to see the time series of a particular grid point (i.e.
34.5n/350.5e):
plot((a[,"34.5","350.5"]),type="l")
a mean latitudinal value:
lat.mean <- apply(a,c(1,2),mean)
If you have 'NA's' :
sst.mean <- apply(a,c(2,3),mean,na.rm=T)
And, if you want a Matrix ;-)
F <- matrix(a,456,20) (456 months, 4x5 grid space = 20 cuadricules)
# <- 20 locations ->
# -----------------------
# | | |
# | | |
# | | |
# | | |
# | | |
# | | |
# | | |
#
# time ... ...
#
# | | |
# | | |
# | | |
# | | |
# | | |
# | | |
# V | |
# -----------------------
Antonio
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list