Hi lily, Something like this should work: DF1<-read.table(text= "latitude longitude Precip 45.5 110.5 3.2 45.5 111 5.0 45.5 111.5 1.8 45.5 112 2.0 46 110.5 6.1 46 111 4.5 46 111.5 7.8 46 112 5.5", header=TRUE) lats<-sort(unique(DF1$latitude),decreasing=TRUE) lons<-sort(unique(DF1$longitude)) DF2<-matrix(NA,nrow=length(lats),ncol=length(lons)) rownames(DF2)<-lats colnames(DF2)<-lons nval<-dim(DF1)[1] for(val in 1:nval) { row<-which(lats == DF1$latitude[val]) col<-which(lons == DF1$longitude[val]) DF2[row,col]<-DF1$Precip[val] }
Jim On Tue, Nov 13, 2018 at 6:22 PM lily li <chocol...@gmail.com> wrote: > > Hi R users, > > I have a question about manipulating data. For example, I have DF1 as the > following, how to transform it to a gridded dataset DF2? In DF2, each value > Precip is an attribute of the corresponding grid cell. So DF2 is like a > spatial surface, and can be imported to ArcGIS. Thanks for your help. > > DF1 > latitude longitude Precip > 45.5 110.5 3.2 > 45.5 111 5.0 > 45.5 111.5 1.8 > 45.5 112 2.0 > 46 110.5 6.1 > 46 111 4.5 > 46 111.5 7.8 > 46 112 5.5 > ... > > > DF2 > 6.1 4.5 7.8 5.5 > 3.2 5.0 1.8 2.0 > ... > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.