Re: [R] How to create gridded data
If the grid is not full you need to identify the missing cells. As an example we can remove rows 3 and 6 from DF1: > DF1miss <- DF1[-c(3, 6), ] > DF2miss <- xtabs(Precip~latitude+longitude, DF1miss) > DF2miss longitude latitude 110.5 111 111.5 112 45.5 3.2 5.0 0.0 2.0 46 6.1 0.0 7.8 5.5 The table command inserts 0 for empty cells, but for your data zero is a valid value so we need to identify the missing values and replace them with NA: > DF2mod <- xtabs(~latitude+longitude, DF1miss) < 1 > DF2mod longitude latitude 110.5 111 111.5 112 45.5 FALSE FALSE TRUE FALSE 46 FALSE TRUE FALSE FALSE > DF2miss[DF2mod] <- NA > DF2miss # Print table with blanks for missing values: longitude latitude 110.5 111 111.5 112 45.5 3.2 5.0 2.0 46 6.1 7.8 5.5 > print(DF2miss, na.print=NA) # Print table with for missing values: longitude latitude 110.5 111 111.5 112 45.5 3.2 5.0 2.0 46 6.17.8 5.5 David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 From: lily li Sent: Monday, March 25, 2019 1:18 AM To: David L Carlson Cc: r-help Subject: Re: [R] How to create gridded data Now I have new question about this post. If the grid coordinates in DF1 are not complete, i.e. there are missing coordinates, how to fill these with -99 in the exported DF2? Thanks. On Thu, Nov 15, 2018 at 10:57 PM David L Carlson <mailto:dcarl...@tamu.edu> wrote: It would depend on the format of the gridded data. Assuming it is a data frame like DF2 in my earlier answer, you just reverse the steps: > DF2 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 > DF3 <- data.frame(as.table(as.matrix(DF2))) Var1 Var2 Freq 1 46 110.5 6.1 2 45.5 110.5 3.2 3 46 111 4.5 4 45.5 111 5.0 5 46 111.5 7.8 6 45.5 111.5 1.8 7 46 112 5.5 8 45.5 112 2.0 But the latitude and longitude get converted to factors and we lose the column names: > DF3 <- data.frame(as.table(as.matrix(DF2))) > colnames(DF3) <- c("latitude", "longitude", "Precip") > DF3$latitude <- as.numeric(as.character(DF3$latitude)) > DF3$longitude <- as.numeric(as.character(DF3$longitude)) David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 From: lily li <mailto:chocol...@gmail.com> Sent: Tuesday, November 13, 2018 10:50 PM To: David L Carlson <mailto:dcarl...@tamu.edu> Cc: Sarah Goslee <mailto:sarah.gos...@gmail.com>; R mailing list <mailto:r-help@r-project.org> Subject: Re: [R] How to create gridded data Thanks, Sarah's answer helps the question. Now how to change the gridded data back to DF1 format? I don't know how to name the format, thanks. On Tue, Nov 13, 2018 at 10:56 PM David L Carlson <mailto:mailto:dcarl...@tamu.edu> wrote: Sarah's answer is probably better depending on what you want to do with the resulting data, but here's a way to go from your original DF1 to DF2: > DF1 <- structure(list(latitude = c(45.5, 45.5, 45.5, 45.5, 46, 46, 46, + 46), longitude = c(110.5, 111, 111.5, 112, 110.5, 111, 111.5, + 112), Precip = c(3.2, 5, 1.8, 2, 6.1, 4.5, 7.8, 5.5)), + class = "data.frame", row.names = c(NA, -8L)) > # Convert to table with xtabs() > DF2 <- xtabs(Precip~latitude+longitude, DF1) > # Reverse the order of the latitudes > DF2 <- DF2[rev(rownames(DF2)), ] > DF2 longitude latitude 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 # Convert to a data frame > DF2 <- as.data.frame.matrix(DF2) > DF2 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 -Original Message- From: R-help <mailto:mailto:r-help-boun...@r-project.org> On Behalf Of Sarah Goslee Sent: Tuesday, November 13, 2018 8:16 AM To: lily li <mailto:mailto:chocol...@gmail.com> Cc: r-help <mailto:mailto:r-help@r-project.org> Subject: Re: [R] How to create gridded data If you want an actual spatial dataset, the best place to ask is R-sig-geo R has substantial capabilities for dealing with gridded spatial data, including in the sp, raster, and sf packages. Here's one approach, creating a SpatialGridDataFrame, which can be exported in any standard raster format using the rgdal package. DF2 <- DF1 coordinates(DF2) <- ~longitude + latitude gridded(DF2) <- TRUE fullgrid(DF2) <- TRUE I recommend Roger Bivand's excellent book: https://urldefense.proofpoint.com/v2/url?u=https-3A__www.springer.com_us_book_9781461476177=DwMFaQ=ODFT-G5SujMiGrKuoJJjVg=veMG
Re: [R] How to create gridded data
Now I have new question about this post. If the grid coordinates in DF1 are not complete, i.e. there are missing coordinates, how to fill these with -99 in the exported DF2? Thanks. On Thu, Nov 15, 2018 at 10:57 PM David L Carlson wrote: > It would depend on the format of the gridded data. Assuming it is a data > frame like DF2 in my earlier answer, you just reverse the steps: > > > DF2 > 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > > DF3 <- data.frame(as.table(as.matrix(DF2))) > Var1 Var2 Freq > 1 46 110.5 6.1 > 2 45.5 110.5 3.2 > 3 46 111 4.5 > 4 45.5 111 5.0 > 5 46 111.5 7.8 > 6 45.5 111.5 1.8 > 7 46 112 5.5 > 8 45.5 112 2.0 > > But the latitude and longitude get converted to factors and we lose the > column names: > > > DF3 <- data.frame(as.table(as.matrix(DF2))) > > colnames(DF3) <- c("latitude", "longitude", "Precip") > > DF3$latitude <- as.numeric(as.character(DF3$latitude)) > > DF3$longitude <- as.numeric(as.character(DF3$longitude)) > > > David L Carlson > Department of Anthropology > Texas A University > College Station, TX 77843-4352 > > From: lily li > Sent: Tuesday, November 13, 2018 10:50 PM > To: David L Carlson > Cc: Sarah Goslee ; R mailing list < > r-help@r-project.org> > Subject: Re: [R] How to create gridded data > > Thanks, Sarah's answer helps the question. Now how to change the gridded > data back to DF1 format? I don't know how to name the format, thanks. > > On Tue, Nov 13, 2018 at 10:56 PM David L Carlson <mailto:dcarl...@tamu.edu> > wrote: > Sarah's answer is probably better depending on what you want to do with > the resulting data, but here's a way to go from your original DF1 to DF2: > > > DF1 <- structure(list(latitude = c(45.5, 45.5, 45.5, 45.5, 46, 46, 46, > + 46), longitude = c(110.5, 111, 111.5, 112, 110.5, 111, 111.5, > + 112), Precip = c(3.2, 5, 1.8, 2, 6.1, 4.5, 7.8, 5.5)), > + class = "data.frame", row.names = c(NA, -8L)) > > > # Convert to table with xtabs() > > DF2 <- xtabs(Precip~latitude+longitude, DF1) > > > > # Reverse the order of the latitudes > > DF2 <- DF2[rev(rownames(DF2)), ] > > DF2 > longitude > latitude 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > # Convert to a data frame > > DF2 <- as.data.frame.matrix(DF2) > > DF2 > 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > > David L Carlson > Department of Anthropology > Texas A University > College Station, TX 77843-4352 > > > -Original Message- > From: R-help <mailto:r-help-boun...@r-project.org> On Behalf Of Sarah > Goslee > Sent: Tuesday, November 13, 2018 8:16 AM > To: lily li <mailto:chocol...@gmail.com> > Cc: r-help <mailto:r-help@r-project.org> > Subject: Re: [R] How to create gridded data > > If you want an actual spatial dataset, the best place to ask is R-sig-geo > > R has substantial capabilities for dealing with gridded spatial data, > including in the sp, raster, and sf packages. > > Here's one approach, creating a SpatialGridDataFrame, which can be > exported in any standard raster format using the rgdal package. > > DF2 <- DF1 > coordinates(DF2) <- ~longitude + latitude > gridded(DF2) <- TRUE > fullgrid(DF2) <- TRUE > > I recommend Roger Bivand's excellent book: > > https://urldefense.proofpoint.com/v2/url?u=https-3A__www.springer.com_us_book_9781461476177=DwMFaQ=ODFT-G5SujMiGrKuoJJjVg=veMGHMCNZShld-KX-bIj4jRE_tP9ojUvB_Lqp0ieSdk=vZqNKoDe8N1TzBzeK12g2oa0cBS8VD6NDCs-hUhvt5o=B73PwZQrdKUmM1ML2Y5zjaEz7xqkHlzBDCrhluogK2U= > > and there are abundant web tutorials. > > Sarah > On Tue, Nov 13, 2018 at 2:22 AM lily li <mailto: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 1115.0 > > 45.5 111.5 1.8 > > 45.5 1122.0 > > 46 110.5 6.1 > > 46 111
Re: [R] How to create gridded data
It would depend on the format of the gridded data. Assuming it is a data frame like DF2 in my earlier answer, you just reverse the steps: > DF2 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 > DF3 <- data.frame(as.table(as.matrix(DF2))) Var1 Var2 Freq 1 46 110.5 6.1 2 45.5 110.5 3.2 3 46 111 4.5 4 45.5 111 5.0 5 46 111.5 7.8 6 45.5 111.5 1.8 7 46 112 5.5 8 45.5 112 2.0 But the latitude and longitude get converted to factors and we lose the column names: > DF3 <- data.frame(as.table(as.matrix(DF2))) > colnames(DF3) <- c("latitude", "longitude", "Precip") > DF3$latitude <- as.numeric(as.character(DF3$latitude)) > DF3$longitude <- as.numeric(as.character(DF3$longitude)) David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 From: lily li Sent: Tuesday, November 13, 2018 10:50 PM To: David L Carlson Cc: Sarah Goslee ; R mailing list Subject: Re: [R] How to create gridded data Thanks, Sarah's answer helps the question. Now how to change the gridded data back to DF1 format? I don't know how to name the format, thanks. On Tue, Nov 13, 2018 at 10:56 PM David L Carlson <mailto:dcarl...@tamu.edu> wrote: Sarah's answer is probably better depending on what you want to do with the resulting data, but here's a way to go from your original DF1 to DF2: > DF1 <- structure(list(latitude = c(45.5, 45.5, 45.5, 45.5, 46, 46, 46, + 46), longitude = c(110.5, 111, 111.5, 112, 110.5, 111, 111.5, + 112), Precip = c(3.2, 5, 1.8, 2, 6.1, 4.5, 7.8, 5.5)), + class = "data.frame", row.names = c(NA, -8L)) > # Convert to table with xtabs() > DF2 <- xtabs(Precip~latitude+longitude, DF1) > # Reverse the order of the latitudes > DF2 <- DF2[rev(rownames(DF2)), ] > DF2 longitude latitude 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 # Convert to a data frame > DF2 <- as.data.frame.matrix(DF2) > DF2 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 -Original Message- From: R-help <mailto:r-help-boun...@r-project.org> On Behalf Of Sarah Goslee Sent: Tuesday, November 13, 2018 8:16 AM To: lily li <mailto:chocol...@gmail.com> Cc: r-help <mailto:r-help@r-project.org> Subject: Re: [R] How to create gridded data If you want an actual spatial dataset, the best place to ask is R-sig-geo R has substantial capabilities for dealing with gridded spatial data, including in the sp, raster, and sf packages. Here's one approach, creating a SpatialGridDataFrame, which can be exported in any standard raster format using the rgdal package. DF2 <- DF1 coordinates(DF2) <- ~longitude + latitude gridded(DF2) <- TRUE fullgrid(DF2) <- TRUE I recommend Roger Bivand's excellent book: https://urldefense.proofpoint.com/v2/url?u=https-3A__www.springer.com_us_book_9781461476177=DwMFaQ=ODFT-G5SujMiGrKuoJJjVg=veMGHMCNZShld-KX-bIj4jRE_tP9ojUvB_Lqp0ieSdk=vZqNKoDe8N1TzBzeK12g2oa0cBS8VD6NDCs-hUhvt5o=B73PwZQrdKUmM1ML2Y5zjaEz7xqkHlzBDCrhluogK2U= and there are abundant web tutorials. Sarah On Tue, Nov 13, 2018 at 2:22 AM lily li <mailto: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 > ... > -- Sarah Goslee (she/her) https://urldefense.proofpoint.com/v2/url?u=http-3A__www.numberwright.com=DwMFaQ=ODFT-G5SujMiGrKuoJJjVg=veMGHMCNZShld-KX-bIj4jRE_tP9ojUvB_Lqp0ieSdk=vZqNKoDe8N1TzBzeK12g2oa0cBS8VD6NDCs-hUhvt5o=qSosThG59aeSFYzVFf1e-YQGbuBKVbvgVi1z9nFm884= __ mailto:R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp=DwMFaQ=ODFT-G5SujMiGrKuoJJjVg=veMGHMCNZShld-KX-bIj4jRE_tP9ojUvB_Lqp0ieSdk=vZqNKoDe8N1TzBzeK12g2oa0cBS8VD6NDCs-hUhvt5o=2pS9yFu5bpcRyCi1vX_OEDD2Ie8ZihvOQrkDQSNu8RM= PLEASE do read the posting guide https://urldefense.proofp
Re: [R] How to create gridded data
Sarah's answer is probably the best approach, but to do it using very basic R methods that predate the very good spatial support that R now has, I would likely do this: ## Thanks, Jim Lemon, for this step: df1 <- read.table(text= "latitude longitude Precip 45.5 110.5 3.2 45.5 1115.0 45.5 111.5 1.8 45.5 1122.0 46 110.5 6.1 46 1114.5 46 111.5 7.8 46 1125.5", header=TRUE) ## first sort df1 <- df1[order(df1$latitude, df1$longitude) , ] ## convert vector of precipitations to matrix df2 <- matrix(df1$Precip, nrow=length(unique(df1$latitude)), byrow=TRUE) ## reorder the latitudes (rows) df2 <- df2[ nrow(df2):1 , ] ## > df2 ## [,1] [,2] [,3] [,4] ## [1,] 6.1 4.5 7.8 5.5 ## [2,] 3.2 5.0 1.8 2.0 ## From the original question: ## DF2 ## 6.1 4.5 7.8 5.5 ## 3.2 5.0 1.8 2.0 ## ... -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 11/13/18, 8:50 PM, "R-help on behalf of lily li" wrote: Thanks, Sarah's answer helps the question. Now how to change the gridded data back to DF1 format? I don't know how to name the format, thanks. On Tue, Nov 13, 2018 at 10:56 PM David L Carlson wrote: > Sarah's answer is probably better depending on what you want to do with > the resulting data, but here's a way to go from your original DF1 to DF2: > > > DF1 <- structure(list(latitude = c(45.5, 45.5, 45.5, 45.5, 46, 46, 46, > + 46), longitude = c(110.5, 111, 111.5, 112, 110.5, 111, 111.5, > + 112), Precip = c(3.2, 5, 1.8, 2, 6.1, 4.5, 7.8, 5.5)), > + class = "data.frame", row.names = c(NA, -8L)) > > > # Convert to table with xtabs() > > DF2 <- xtabs(Precip~latitude+longitude, DF1) > > > > # Reverse the order of the latitudes > > DF2 <- DF2[rev(rownames(DF2)), ] > > DF2 > longitude > latitude 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > # Convert to a data frame > > DF2 <- as.data.frame.matrix(DF2) > > DF2 > 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > > David L Carlson > Department of Anthropology > Texas A University > College Station, TX 77843-4352 > > > -Original Message- > From: R-help On Behalf Of Sarah Goslee > Sent: Tuesday, November 13, 2018 8:16 AM > To: lily li > Cc: r-help > Subject: Re: [R] How to create gridded data > > If you want an actual spatial dataset, the best place to ask is R-sig-geo > > R has substantial capabilities for dealing with gridded spatial data, > including in the sp, raster, and sf packages. > > Here's one approach, creating a SpatialGridDataFrame, which can be > exported in any standard raster format using the rgdal package. > > DF2 <- DF1 > coordinates(DF2) <- ~longitude + latitude > gridded(DF2) <- TRUE > fullgrid(DF2) <- TRUE > > I recommend Roger Bivand's excellent book: > https://www.springer.com/us/book/9781461476177 > > and there are abundant web tutorials. > > Sarah > On Tue, Nov 13, 2018 at 2:22 AM lily li 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 1115.0 > > 45.5 111.5 1.8 > > 45.5 1122.0 > > 46 110.5 6.1 > > 46 1114.5 > > 46 111.5 7.8 > > 46 1125.5 > > ... > > > > > > DF2 > > 6.1 4.5 7.8 5.5 > > 3.2 5.0 1.8 2.0 > > ... > > > > > -- > Sarah Goslee (she/her) > http://www.numberwright.com > > __ > R
Re: [R] How to create gridded data
Thanks, Sarah's answer helps the question. Now how to change the gridded data back to DF1 format? I don't know how to name the format, thanks. On Tue, Nov 13, 2018 at 10:56 PM David L Carlson wrote: > Sarah's answer is probably better depending on what you want to do with > the resulting data, but here's a way to go from your original DF1 to DF2: > > > DF1 <- structure(list(latitude = c(45.5, 45.5, 45.5, 45.5, 46, 46, 46, > + 46), longitude = c(110.5, 111, 111.5, 112, 110.5, 111, 111.5, > + 112), Precip = c(3.2, 5, 1.8, 2, 6.1, 4.5, 7.8, 5.5)), > + class = "data.frame", row.names = c(NA, -8L)) > > > # Convert to table with xtabs() > > DF2 <- xtabs(Precip~latitude+longitude, DF1) > > > > # Reverse the order of the latitudes > > DF2 <- DF2[rev(rownames(DF2)), ] > > DF2 > longitude > latitude 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > # Convert to a data frame > > DF2 <- as.data.frame.matrix(DF2) > > DF2 > 110.5 111 111.5 112 > 46 6.1 4.5 7.8 5.5 > 45.5 3.2 5.0 1.8 2.0 > > > David L Carlson > Department of Anthropology > Texas A University > College Station, TX 77843-4352 > > > -----Original Message----- > From: R-help On Behalf Of Sarah Goslee > Sent: Tuesday, November 13, 2018 8:16 AM > To: lily li > Cc: r-help > Subject: Re: [R] How to create gridded data > > If you want an actual spatial dataset, the best place to ask is R-sig-geo > > R has substantial capabilities for dealing with gridded spatial data, > including in the sp, raster, and sf packages. > > Here's one approach, creating a SpatialGridDataFrame, which can be > exported in any standard raster format using the rgdal package. > > DF2 <- DF1 > coordinates(DF2) <- ~longitude + latitude > gridded(DF2) <- TRUE > fullgrid(DF2) <- TRUE > > I recommend Roger Bivand's excellent book: > https://www.springer.com/us/book/9781461476177 > > and there are abundant web tutorials. > > Sarah > On Tue, Nov 13, 2018 at 2:22 AM lily li 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 1115.0 > > 45.5 111.5 1.8 > > 45.5 1122.0 > > 46 110.5 6.1 > > 46 1114.5 > > 46 111.5 7.8 > > 46 1125.5 > > ... > > > > > > DF2 > > 6.1 4.5 7.8 5.5 > > 3.2 5.0 1.8 2.0 > > ... > > > > > -- > Sarah Goslee (she/her) > http://www.numberwright.com > > __ > 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. > [[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.
Re: [R] How to create gridded data
Sarah's answer is probably better depending on what you want to do with the resulting data, but here's a way to go from your original DF1 to DF2: > DF1 <- structure(list(latitude = c(45.5, 45.5, 45.5, 45.5, 46, 46, 46, + 46), longitude = c(110.5, 111, 111.5, 112, 110.5, 111, 111.5, + 112), Precip = c(3.2, 5, 1.8, 2, 6.1, 4.5, 7.8, 5.5)), + class = "data.frame", row.names = c(NA, -8L)) > # Convert to table with xtabs() > DF2 <- xtabs(Precip~latitude+longitude, DF1) > # Reverse the order of the latitudes > DF2 <- DF2[rev(rownames(DF2)), ] > DF2 longitude latitude 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 # Convert to a data frame > DF2 <- as.data.frame.matrix(DF2) > DF2 110.5 111 111.5 112 46 6.1 4.5 7.8 5.5 45.5 3.2 5.0 1.8 2.0 David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 -Original Message- From: R-help On Behalf Of Sarah Goslee Sent: Tuesday, November 13, 2018 8:16 AM To: lily li Cc: r-help Subject: Re: [R] How to create gridded data If you want an actual spatial dataset, the best place to ask is R-sig-geo R has substantial capabilities for dealing with gridded spatial data, including in the sp, raster, and sf packages. Here's one approach, creating a SpatialGridDataFrame, which can be exported in any standard raster format using the rgdal package. DF2 <- DF1 coordinates(DF2) <- ~longitude + latitude gridded(DF2) <- TRUE fullgrid(DF2) <- TRUE I recommend Roger Bivand's excellent book: https://www.springer.com/us/book/9781461476177 and there are abundant web tutorials. Sarah On Tue, Nov 13, 2018 at 2:22 AM lily li 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 1115.0 > 45.5 111.5 1.8 > 45.5 1122.0 > 46 110.5 6.1 > 46 1114.5 > 46 111.5 7.8 > 46 1125.5 > ... > > > DF2 > 6.1 4.5 7.8 5.5 > 3.2 5.0 1.8 2.0 > ... > -- Sarah Goslee (she/her) http://www.numberwright.com __ 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.
Re: [R] How to create gridded data
If you want an actual spatial dataset, the best place to ask is R-sig-geo R has substantial capabilities for dealing with gridded spatial data, including in the sp, raster, and sf packages. Here's one approach, creating a SpatialGridDataFrame, which can be exported in any standard raster format using the rgdal package. DF2 <- DF1 coordinates(DF2) <- ~longitude + latitude gridded(DF2) <- TRUE fullgrid(DF2) <- TRUE I recommend Roger Bivand's excellent book: https://www.springer.com/us/book/9781461476177 and there are abundant web tutorials. Sarah On Tue, Nov 13, 2018 at 2:22 AM lily li 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 1115.0 > 45.5 111.5 1.8 > 45.5 1122.0 > 46 110.5 6.1 > 46 1114.5 > 46 111.5 7.8 > 46 1125.5 > ... > > > DF2 > 6.1 4.5 7.8 5.5 > 3.2 5.0 1.8 2.0 > ... > -- Sarah Goslee (she/her) http://www.numberwright.com __ 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.
Re: [R] How to create gridded data
You might take a look at the reshape package, which switches from 'long' to 'wide' formats and vice versa in a fairly flexible way. S Ellison > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of lily li > Sent: 13 November 2018 07:22 > To: R mailing list > Subject: [R] How to create gridded data > > 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 1115.0 > 45.5 111.5 1.8 > 45.5 1122.0 > 46 110.5 6.1 > 46 1114.5 > 46 111.5 7.8 > 46 1125.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. *** This email and any attachments are confidential. Any use...{{dropped:8}} __ 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.
Re: [R] How to create gridded data
Hi lily, Something like this should work: DF1<-read.table(text= "latitude longitude Precip 45.5 110.5 3.2 45.5 1115.0 45.5 111.5 1.8 45.5 1122.0 46 110.5 6.1 46 1114.5 46 111.5 7.8 46 1125.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 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 1115.0 > 45.5 111.5 1.8 > 45.5 1122.0 > 46 110.5 6.1 > 46 1114.5 > 46 111.5 7.8 > 46 1125.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.