Hey there, I am very much a newbie in the R world. I have to work with R for my internship. I really hope that someone can help me out here, since it costs me ages to run and adjust the same script over and over again. I have a SpatialPointsDataFrame table (sent2field2 at data) that I would like to split into different SpatialPointsDataFrame tables. The first table needs to consist out of [1:13] columns, the second table needs to consist of [,c(1,14:25)] the third needs to consist of [,c(1,26:37)]. The name of the tables need to be Sent2Field2_1, Sent2Field2_2, Sent2Field2_3, etc etc? So only 1,2,3,4 etc need to change within the name. This loop needs to go on until there are no columns left anymore in the dataset. Then the next step needs to add coordinates to the table (see script below). The name of the tables need to be Sent2F2_1, Sent2F2_2, Sent2F2_3, etc etc? So only 1,2,3,4 etc need to change within the name. The last step projects the table with the added coordinates into a SpatialPointsDataFrame. See the script below: (up to now I am adjusting the names manually and running it time after time, i am getting crazy, but I really don?t know how to make a loop) For now the separate steps are working fine. I really hope someone can help me out. Looking forward to anyones reply. Thank you already in advance. #field2 Sent2Field2_1<-Sent2Field2 at data[,1:13] Sent2F2_1<-cbind(CoordsSent2_F2,Sent2Field2_1) coordinates(Sent2F2_1) <- ~long+lat Sent2Field2_2<-Sent2Field2 at data[,c(1,14:25)] Sent2F2_2<-cbind(CoordsSent2_F2,Sent2Field2_2) coordinates(Sent2F2_2) <- ~long+lat Sent2Field2_3<-Sent2Field2 at data[,c(1,26:37)] Sent2F2_3<-cbind(CoordsSent2_F2,Sent2Field2_3) coordinates(Sent2F2_3) <- ~long+lat Sent2Field2_4<-Sent2Field2 at data[,c(1,38:49)] Sent2F2_4<-cbind(CoordsSent2_F2,Sent2Field2_4) coordinates(Sent2F2_4) <- ~long+lat Sent2Field2_5<-Sent2Field2 at data[,c(1,50:61)] Sent2F2_5<-cbind(CoordsSent2_F2,Sent2Field2_5) coordinates(Sent2F2_5) <- ~long+lat etc etc [[alternative HTML version deleted]]
Two things: 1. As your query is about spatial data, you may do better posting (in plain text, **not** html, which often gets mangled on these plain text lists) on the r-sig-geo list. 2. These lists can help, but do not replace, your obligation to do your own homework. There are many good R tutorials on the web that you can look to for help. Some recommendations, by no means all that you may wish to check, can be found here: https://www.rstudio.com/online-learning/#r-programming There are also both tutorials and "Vignettes" (the latter in the packages themselves) specifically for spatial data analysis and visualization. Searching on "R tutorial spatial data" brought up several. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Oct 5, 2018 at 8:12 AM Ivy Pieters <icwpieters at gmail.com> wrote:> Hey there, > I am very much a newbie in the R world. I have to work with R for my > internship. I really hope that someone can help me out here, since it costs > me ages to run and adjust the same script over and over again. > > I have a SpatialPointsDataFrame table (sent2field2 at data) that I would > like to split into different SpatialPointsDataFrame tables. The first table > needs to consist out of [1:13] columns, the second table needs to consist > of [,c(1,14:25)] the third needs to consist of [,c(1,26:37)]. The name of > the tables need to be Sent2Field2_1, Sent2Field2_2, Sent2Field2_3, etc etc? > So only 1,2,3,4 etc need to change within the name. This loop needs to go > on until there are no columns left anymore in the dataset. Then the next > step needs to add coordinates to the table (see script below). The name of > the tables need to be Sent2F2_1, Sent2F2_2, Sent2F2_3, etc etc? So only > 1,2,3,4 etc need to change within the name. The last step projects the > table with the added coordinates into a SpatialPointsDataFrame. See the > script below: (up to now I am adjusting the names manually and running it > time after time, i am getting crazy, but I really don?t know how to make a > loop) For now the separate steps are working fine. I really hope someone > can help me out. Looking forward to anyones reply. Thank you already in > advance. > > #field2 > Sent2Field2_1<-Sent2Field2 at data[,1:13] > Sent2F2_1<-cbind(CoordsSent2_F2,Sent2Field2_1) > coordinates(Sent2F2_1) <- ~long+lat > > Sent2Field2_2<-Sent2Field2 at data[,c(1,14:25)] > Sent2F2_2<-cbind(CoordsSent2_F2,Sent2Field2_2) > coordinates(Sent2F2_2) <- ~long+lat > > Sent2Field2_3<-Sent2Field2 at data[,c(1,26:37)] > Sent2F2_3<-cbind(CoordsSent2_F2,Sent2Field2_3) > coordinates(Sent2F2_3) <- ~long+lat > > Sent2Field2_4<-Sent2Field2 at data[,c(1,38:49)] > Sent2F2_4<-cbind(CoordsSent2_F2,Sent2Field2_4) > coordinates(Sent2F2_4) <- ~long+lat > > Sent2Field2_5<-Sent2Field2 at data[,c(1,50:61)] > Sent2F2_5<-cbind(CoordsSent2_F2,Sent2Field2_5) > coordinates(Sent2F2_5) <- ~long+lat > > etc > etc > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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]]
In addition to using plain text format when you post, please try to create a complete minimal example [1][2][3] that sets up a representative set of the variables you are using because the behavior of functions can change depending on what kind of data you are working with. For instance, so you have reason to believe that the number of columns will always divide out with no remainder? Why don't you split this data up before you create the one large SpatialPointsDataFrame? It is highly unusual to have multiple columns with the same names in any one data frame (it looks like you think two of each block of 12 columns will be named long and lat, but they all come from one source data frame). [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example [2] http://adv-r.had.co.nz/Reproducibility.html [3] https://cran.r-project.org/web/packages/reprex/index.html (read the vignette) On October 5, 2018 8:42:24 AM PDT, Bert Gunter <bgunter.4567 at gmail.com> wrote:>Two things: > >1. As your query is about spatial data, you may do better posting (in >plain >text, **not** html, which often gets mangled on these plain text lists) >on >the r-sig-geo list. > >2. These lists can help, but do not replace, your obligation to do your >own >homework. There are many good R tutorials on the web that you can look >to >for help. Some recommendations, by no means all that you may wish to >check, >can be found here: > >https://www.rstudio.com/online-learning/#r-programming > >There are also both tutorials and "Vignettes" (the latter in the >packages >themselves) specifically for spatial data analysis and visualization. >Searching on "R tutorial spatial data" brought up several. > >Cheers, >Bert > >Bert Gunter > >"The trouble with having an open mind is that people keep coming along >and >sticking things into it." >-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > >On Fri, Oct 5, 2018 at 8:12 AM Ivy Pieters <icwpieters at gmail.com> >wrote: > >> Hey there, >> I am very much a newbie in the R world. I have to work with R for my >> internship. I really hope that someone can help me out here, since it >costs >> me ages to run and adjust the same script over and over again. >> >> I have a SpatialPointsDataFrame table (sent2field2 at data) that I would >> like to split into different SpatialPointsDataFrame tables. The first >table >> needs to consist out of [1:13] columns, the second table needs to >consist >> of [,c(1,14:25)] the third needs to consist of [,c(1,26:37)]. The >name of >> the tables need to be Sent2Field2_1, Sent2Field2_2, Sent2Field2_3, >etc etc? >> So only 1,2,3,4 etc need to change within the name. This loop needs >to go >> on until there are no columns left anymore in the dataset. Then the >next >> step needs to add coordinates to the table (see script below). The >name of >> the tables need to be Sent2F2_1, Sent2F2_2, Sent2F2_3, etc etc? So >only >> 1,2,3,4 etc need to change within the name. The last step projects >the >> table with the added coordinates into a SpatialPointsDataFrame. See >the >> script below: (up to now I am adjusting the names manually and >running it >> time after time, i am getting crazy, but I really don?t know how to >make a >> loop) For now the separate steps are working fine. I really hope >someone >> can help me out. Looking forward to anyones reply. Thank you already >in >> advance. >> >> #field2 >> Sent2Field2_1<-Sent2Field2 at data[,1:13] >> Sent2F2_1<-cbind(CoordsSent2_F2,Sent2Field2_1) >> coordinates(Sent2F2_1) <- ~long+lat >> >> Sent2Field2_2<-Sent2Field2 at data[,c(1,14:25)] >> Sent2F2_2<-cbind(CoordsSent2_F2,Sent2Field2_2) >> coordinates(Sent2F2_2) <- ~long+lat >> >> Sent2Field2_3<-Sent2Field2 at data[,c(1,26:37)] >> Sent2F2_3<-cbind(CoordsSent2_F2,Sent2Field2_3) >> coordinates(Sent2F2_3) <- ~long+lat >> >> Sent2Field2_4<-Sent2Field2 at data[,c(1,38:49)] >> Sent2F2_4<-cbind(CoordsSent2_F2,Sent2Field2_4) >> coordinates(Sent2F2_4) <- ~long+lat >> >> Sent2Field2_5<-Sent2Field2 at data[,c(1,50:61)] >> Sent2F2_5<-cbind(CoordsSent2_F2,Sent2Field2_5) >> coordinates(Sent2F2_5) <- ~long+lat >> >> etc >> etc >> >> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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 at 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.-- Sent from my phone. Please excuse my brevity.
Bert (in his separate email) is right about learning the basics, and using R-sig-geo. You're kind of jumping right into the deep end! None the less, here's an example that should help. For one thing, you're being more complicated than necessary. SpatialPointsDataFrame objects can, much of the time, be treated just like regular data frames. You don't need to extract the @data slot and operate on it. ## example with simple fake data ## for the example, I've decided on groups of three columns ## and 5 rows set.seed(11198905) ## first, the coordinates and the single column that will be included every time. sdf <- data.frame(long=runif(5, -121, -120), lat=runif(5, 33, 34), nm=letters[1:5]) ## construct a set of data with 9 columns (3 sets of 3) tmp <- matrix( round(runif(45, 10,20)), nrow=5) colnames(tmp) <- paste0('v',1:9) sdf <- cbind(sdf, tmp) ## make it into a SpatialPointsDataFrame library(sp) coordinates(sdf) <- c('long','lat') plot(sdf) ## manually: ## split into three separate SpatialPointsDataFrames, each with three data columns s1 <- sdf[,c(1,2:4)] s2 <- sdf[,c(1,5:7)] s3 <- sdf[,c(1,8:10)] ## to use a loop, have to calculate which columns are needed each time through the loop ## (showing intermediate steps in more detail than truly necessary) for (ic in 1:3) { ## calculate the column numbers col1 <- ic*3-1 coln <- col1+2 cat('iteration',ic,' selecting columns 1 and',col1,'through',coln,'\n') sic <- sdf[, c(1, col1:coln)] ## calculate the name of the new SpatialPointsDataFrame nmi <- paste0('sd',ic) cat(' assigning to ',nmi,'\n') ## write the current columns to .GlobalEnv assign( nmi, sic) } ## now compare s1 with sd1, s2 with sd2, etc. ## they should be the same ## using assign() in this way is often recommended against. An alternative, which might be better depending on what you need to do with them, would be to save them as elements of a list object. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 ?On 10/5/18, 7:30 AM, "R-help on behalf of Ivy Pieters" <r-help-bounces at r-project.org on behalf of icwpieters at gmail.com> wrote: Hey there, I am very much a newbie in the R world. I have to work with R for my internship. I really hope that someone can help me out here, since it costs me ages to run and adjust the same script over and over again. I have a SpatialPointsDataFrame table (sent2field2 at data) that I would like to split into different SpatialPointsDataFrame tables. The first table needs to consist out of [1:13] columns, the second table needs to consist of [,c(1,14:25)] the third needs to consist of [,c(1,26:37)]. The name of the tables need to be Sent2Field2_1, Sent2Field2_2, Sent2Field2_3, etc etc? So only 1,2,3,4 etc need to change within the name. This loop needs to go on until there are no columns left anymore in the dataset. Then the next step needs to add coordinates to the table (see script below). The name of the tables need to be Sent2F2_1, Sent2F2_2, Sent2F2_3, etc etc? So only 1,2,3,4 etc need to change within the name. The last step projects the table with the added coordinates into a SpatialPointsDataFrame. See the script below: (up to now I am adjusting the names manually and running it time after time, i am getting crazy, but I really don?t know how to make a loop) For now the separate steps are working fine. I really hope someone can help me out. Looking forward to anyones reply. Thank you already in advance. #field2 Sent2Field2_1<-Sent2Field2 at data[,1:13] Sent2F2_1<-cbind(CoordsSent2_F2,Sent2Field2_1) coordinates(Sent2F2_1) <- ~long+lat Sent2Field2_2<-Sent2Field2 at data[,c(1,14:25)] Sent2F2_2<-cbind(CoordsSent2_F2,Sent2Field2_2) coordinates(Sent2F2_2) <- ~long+lat Sent2Field2_3<-Sent2Field2 at data[,c(1,26:37)] Sent2F2_3<-cbind(CoordsSent2_F2,Sent2Field2_3) coordinates(Sent2F2_3) <- ~long+lat Sent2Field2_4<-Sent2Field2 at data[,c(1,38:49)] Sent2F2_4<-cbind(CoordsSent2_F2,Sent2Field2_4) coordinates(Sent2F2_4) <- ~long+lat Sent2Field2_5<-Sent2Field2 at data[,c(1,50:61)] Sent2F2_5<-cbind(CoordsSent2_F2,Sent2Field2_5) coordinates(Sent2F2_5) <- ~long+lat etc etc [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Albrecht Kauffmann
2018-Oct-06 00:08 UTC
[R] How can I create a loop for this? Please help me
Hi Ivy, try this: k <- 0 i <- 1 while((k*12+13) <= ncol(Sent2Field2 at data) { i <- k+1 n <- paste("Sent2Field2",i, sep="_") assign(n, Sent2Field2 at data[,c(1,k*12+c(2:13))]) k <- k+1 } Best, Albrecht -- Albrecht Kauffmann alkauffm at fastmail.fm Am Fr, 5. Okt 2018, um 16:30, schrieb Ivy Pieters:> Hey there, > I am very much a newbie in the R world. I have to work with R for my > internship. I really hope that someone can help me out here, since it > costs me ages to run and adjust the same script over and over again. > > I have a SpatialPointsDataFrame table (sent2field2 at data) that I would > like to split into different SpatialPointsDataFrame tables. The first > table needs to consist out of [1:13] columns, the second table needs to > consist of [,c(1,14:25)] the third needs to consist of [,c(1,26:37)]. > The name of the tables need to be Sent2Field2_1, Sent2Field2_2, > Sent2Field2_3, etc etc? So only 1,2,3,4 etc need to change within the > name. This loop needs to go on until there are no columns left anymore > in the dataset. Then the next step needs to add coordinates to the table > (see script below). The name of the tables need to be Sent2F2_1, > Sent2F2_2, Sent2F2_3, etc etc? So only 1,2,3,4 etc need to change within > the name. The last step projects the table with the added coordinates > into a SpatialPointsDataFrame. See the script below: (up to now I am > adjusting the names manually and running it time after time, i am > getting crazy, but I really don?t know how to make a loop) For now the > separate steps are working fine. I really hope someone can help me out. > Looking forward to anyones reply. Thank you already in advance. > > #field2 > Sent2Field2_1<-Sent2Field2 at data[,1:13] > Sent2F2_1<-cbind(CoordsSent2_F2,Sent2Field2_1) > coordinates(Sent2F2_1) <- ~long+lat > > Sent2Field2_2<-Sent2Field2 at data[,c(1,14:25)] > Sent2F2_2<-cbind(CoordsSent2_F2,Sent2Field2_2) > coordinates(Sent2F2_2) <- ~long+lat > > Sent2Field2_3<-Sent2Field2 at data[,c(1,26:37)] > Sent2F2_3<-cbind(CoordsSent2_F2,Sent2Field2_3) > coordinates(Sent2F2_3) <- ~long+lat > > Sent2Field2_4<-Sent2Field2 at data[,c(1,38:49)] > Sent2F2_4<-cbind(CoordsSent2_F2,Sent2Field2_4) > coordinates(Sent2F2_4) <- ~long+lat > > Sent2Field2_5<-Sent2Field2 at data[,c(1,50:61)] > Sent2F2_5<-cbind(CoordsSent2_F2,Sent2Field2_5) > coordinates(Sent2F2_5) <- ~long+lat > > etc > etc > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.