[R] How can I create a loop for this? Please help me

Albrecht Kauffmann @|k@u||m @end|ng |rom |@@tm@||@|m
Sat Oct 6 02:08:49 CEST 2018


Hi Ivy,

try this:

k <- 0
i  <- 1
while((k*12+13) <= ncol(Sent2Field2 using data) {
    i <- k+1
    n <- paste("Sent2Field2",i, sep="_")
    assign(n, Sent2Field2 using data[,c(1,k*12+c(2:13))])
    k <- k+1
}

Best, 
Albrecht

-- 
  Albrecht Kauffmann
  alkauffm using 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 using 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 using data[,1:13]
> Sent2F2_1<-cbind(CoordsSent2_F2,Sent2Field2_1)
> coordinates(Sent2F2_1) <- ~long+lat
> 
> Sent2Field2_2<-Sent2Field2 using data[,c(1,14:25)]
> Sent2F2_2<-cbind(CoordsSent2_F2,Sent2Field2_2)
> coordinates(Sent2F2_2) <- ~long+lat
> 
> Sent2Field2_3<-Sent2Field2 using data[,c(1,26:37)]
> Sent2F2_3<-cbind(CoordsSent2_F2,Sent2Field2_3)
> coordinates(Sent2F2_3) <- ~long+lat
> 
> Sent2Field2_4<-Sent2Field2 using data[,c(1,38:49)]
> Sent2F2_4<-cbind(CoordsSent2_F2,Sent2Field2_4)
> coordinates(Sent2F2_4) <- ~long+lat
> 
> Sent2Field2_5<-Sent2Field2 using 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 using 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.




More information about the R-help mailing list