Dagmar
2017-Aug-19 03:08 UTC
[R] My very first loop!! I failed. May I have some start-up aid?
Dear all, I have a data similar to this: myframe<- data.frame (ID=c("Ernie", "Ernie","Ernie","Ernie"), Timestamp=c("24.09.2012 08:00", "24.09.2012 09:00", "24.09.2012 10:00", "25.09.2012 10:00"), Longitude=c("8.481","8.482","8.483","8.481"), Latitude=c("54.753","54.753","54.752","54.751") ) myframe # Now this is where my loop is supposed to start. In this example I want to run the following functions 3 times. (In real life more often.) How do I do that? library(adehabitatHR) library(rgdal) mysample <- myframe[sample(1:nrow(myframe), 3,replace=FALSE),] mysample mysampletime <- as.POSIXct (strptime(as.character(mysample$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") mysamplexy <- project (cbind (mysample$Longitude, mysample$Latitude), "+proj=utm +zone=32 +ellps=WGS84") colnames(mysamplexy) <- c ("xCord", "yCord") ID <- mysample$ID datltraj <- as.ltraj(mysamplexy, mysampletime, id=ID) Ddat <- BRB.D(datltraj, Tmax=21600, Lmin=36) BRBdat <- BRB(datltraj, D= Ddat,type=c("UD"),Tmax=21600,Lmin=36, hmin=100) kernel.area(BRBdat, unout=c("km2")) # unfortunately my data are not a very good example. Sorry about that. But I guess you know what I mean. # Because I wish to run the functions three times I do want a data frame (or matrix) in the end, which includes the results (kernel areas): That means 12 columns and the three lines. # How do I do that? Many thanks in advance, Tagmarie
Hasan Diwan
2017-Aug-19 03:31 UTC
[R] My very first loop!! I failed. May I have some start-up aid?
[answers inline] On 18 August 2017 at 20:08, Dagmar <Ramgad82 at gmx.net> wrote:> > myframe<- data.frame (ID=c("Ernie", "Ernie","Ernie","Ernie"), > Timestamp=c("24.09.2012 08:00", "24.09.2012 09:00", "24.09.2012 10:00", > "25.09.2012 10:00"), Longitude=c("8.481","8.482","8.483","8.481"), > Latitude=c("54.753","54.753","54.752","54.751") > ) > myframe > > # Now this is where my loop is supposed to start. In this example I want > to run the following functions 3 times. (In real life more often.) How do I > do that? > > library(adehabitatHR) > library(rgdal) >you should use require instead of library if this is a script, require returns boolean, whereas library raises an exception. mysample <- myframe[sample(1:nrow(myframe), 3,replace=FALSE),]> mysample > mysampletime <- as.POSIXct (strptime(as.character(mysample$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > mysamplexy <- project (cbind (mysample$Longitude, mysample$Latitude), > "+proj=utm +zone=32 +ellps=WGS84") > colnames(mysamplexy) <- c ("xCord", "yCord") > ID <- mysample$ID > datltraj <- as.ltraj(mysamplexy, mysampletime, id=ID) > Ddat <- BRB.D(datltraj, Tmax=21600, Lmin=36) > BRBdat <- BRB(datltraj, D= Ddat,type=c("UD"),Tmax=21600,Lmin=36, hmin=100) > kernel.area(BRBdat, unout=c("km2")) > > # unfortunately my data are not a very good example. Sorry about that. But > I guess you know what I mean. > > # Because I wish to run the functions three times I do want a data frame > (or matrix) in the end, which includes the results (kernel areas): That > means 12 columns and the three lines. > > # How do I do that? >Use the rep function, with times being 3? Best of luck -- H -- OpenPGP: https://sks-keyservers.net/pks/lookup?op=get&search=0xFEBAD7FFD041BBA1 If you wish to request my time, please do so using http://bit.ly/hd1ScheduleRequest. Si vous voudrais faire connnaisance, allez a http://bit.ly/hd1ScheduleRequest. <https://sks-keyservers.net/pks/lookup?op=get&search=0xFEBAD7FFD041BBA1>Sent from my mobile device Envoye de mon portable [[alternative HTML version deleted]]
Jeff Newmiller
2017-Aug-19 04:04 UTC
[R] My very first loop!! I failed. May I have some start-up aid?
The advice to use require is incorrect. The only time you should use require is if you are testing the return value from the require function AND you have a plan of what to do if the package is not available. 99% of the time raising an exception when the package is missing is the correct behavior, so calling the library function is the correct approach. Note that calling the require function without inspecting the return value is always wrong usage. -- Sent from my phone. Please excuse my brevity. On August 18, 2017 8:31:57 PM PDT, Hasan Diwan <hasan.diwan at gmail.com> wrote:>[answers inline] > >On 18 August 2017 at 20:08, Dagmar <Ramgad82 at gmx.net> wrote: >> >> myframe<- data.frame (ID=c("Ernie", "Ernie","Ernie","Ernie"), >> Timestamp=c("24.09.2012 08:00", "24.09.2012 09:00", "24.09.2012 >10:00", >> "25.09.2012 10:00"), Longitude=c("8.481","8.482","8.483","8.481"), >> Latitude=c("54.753","54.753","54.752","54.751") >> ) >> myframe >> >> # Now this is where my loop is supposed to start. In this example I >want >> to run the following functions 3 times. (In real life more often.) >How do I >> do that? >> >> library(adehabitatHR) >> library(rgdal) >> > >you should use require instead of library if this is a script, require >returns boolean, whereas library raises an exception. > >mysample <- myframe[sample(1:nrow(myframe), 3,replace=FALSE),] >> mysample >> mysampletime <- as.POSIXct >(strptime(as.character(mysample$Timestamp), >> "%d.%m.%Y %H:%M"), tz="GMT") >> mysamplexy <- project (cbind (mysample$Longitude, mysample$Latitude), >> "+proj=utm +zone=32 +ellps=WGS84") >> colnames(mysamplexy) <- c ("xCord", "yCord") >> ID <- mysample$ID >> datltraj <- as.ltraj(mysamplexy, mysampletime, id=ID) >> Ddat <- BRB.D(datltraj, Tmax=21600, Lmin=36) >> BRBdat <- BRB(datltraj, D= Ddat,type=c("UD"),Tmax=21600,Lmin=36, >hmin=100) >> kernel.area(BRBdat, unout=c("km2")) >> >> # unfortunately my data are not a very good example. Sorry about >that. But >> I guess you know what I mean. >> >> # Because I wish to run the functions three times I do want a data >frame >> (or matrix) in the end, which includes the results (kernel areas): >That >> means 12 columns and the three lines. >> >> # How do I do that? >> > >Use the rep function, with times being 3? Best of luck -- H
Jeff Newmiller
2017-Aug-19 06:11 UTC
[R] My very first loop!! I failed. May I have some start-up aid?
Thank you for providing the example code... for the request of running it multiple times it would have helped if you could have confirmed that the example ran through without errors... there were a lot of mistakes in it. Look into using the reprex package to check your example next time. I don't do this kind of analysis... I really don't know what to expect from the functions. The final step in your sequence produces a vector of sixteen values, not 1 value, nor 4 values, so I don't know how to obtain the 4x3 result you said you expected... I got 16x3. myframe <- data.frame( ID = c( "Ernie", "Ernie","Ernie","Ernie","Ernie","Ernie") , Timestamp = c( "24.09.2012 08:00", "24.09.2012 09:00" , "24.09.2012 10:00", "25.09.2012 10:00" , "26.09.2012 10:00", "27.09.2012 10:00" ) , Longitude = c( 8.481, 8.482, 8.483, 8.481, 8.483, 8.481 ) , Latitude = c( 54.753, 54.753, 54.752, 54.751, 54.752, 54.751 ) , stringsAsFactors = FALSE ) myframe #> ID Timestamp Longitude Latitude #> 1 Ernie 24.09.2012 08:00 8.481 54.753 #> 2 Ernie 24.09.2012 09:00 8.482 54.753 #> 3 Ernie 24.09.2012 10:00 8.483 54.752 #> 4 Ernie 25.09.2012 10:00 8.481 54.751 #> 5 Ernie 26.09.2012 10:00 8.483 54.752 #> 6 Ernie 27.09.2012 10:00 8.481 54.751 # Now this is where my loop is supposed to start. In this example I want #to run the following functions 3 times. (In real life more often.) How #do I do that? library(adehabitatHR) #> Loading required package: sp #> Loading required package: deldir #> deldir 0.1-14 #> Loading required package: ade4 #> Loading required package: adehabitatMA #> Loading required package: adehabitatLT #> Loading required package: CircStats #> Loading required package: MASS #> Loading required package: boot library(rgdal) #> rgdal: version: 1.2-8, (SVN revision 663) #> Geospatial Data Abstraction Library extensions to R successfully loaded #> Loaded GDAL runtime: GDAL 1.11.3, released 2015/09/16 #> Path to GDAL shared files: /usr/share/gdal/1.11 #> Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492] #> Path to PROJ.4 shared files: (autodetected) #> Linking to sp version: 1.2-5 myframe$mysampletime <- as.POSIXct( myframe$Timestamp , format = "%d.%m.%Y %H:%M" , tz="GMT" ) set.seed( 42 ) N <- 3 result <- matrix( NA, nrow = 16, ncol = N ) for ( i in seq.int( N ) ) { mysample <- myframe[ sample( seq_along( myframe[[ 1 ]] ) , 3 , replace=FALSE ) , ] mysamplexy <- project( as.matrix( mysample[ , c( "Longitude" , "Latitude" ) ] ) , "+proj=utm +zone=32 +ellps=WGS84" ) colnames( mysamplexy ) <- c( "xCord", "yCord" ) datltraj <- as.ltraj( mysamplexy, mysample$mysampletime, id=mysample$ID ) Ddat <- BRB.D( datltraj, Tmax=21600, Lmin=36 ) BRBdat <- BRB( datltraj, D = Ddat, type = "UD", Tmax = 21600, Lmin = 36, hmin = 100 ) result[ , i ] <- kernel.area( BRBdat, unout = "km2" ) } #> Warning in kernel.area(BRBdat, unout = "km2"): The grid is too small to allow the estimation of home-range #> for the following value of percent: 70,75,80,85,90,95. You should rerun kernelUD with a larger extent parameter #> Warning in kernel.area(BRBdat, unout = "km2"): The grid is too small to allow the estimation of home-range #> for the following value of percent: 30,35,40,45,50,55,60,65,70,75,80,85,90,95. You should rerun kernelUD with a larger extent parameter #> Error in getvolumeUD(x, standardize = standardize): NA/NaN/Inf in foreign function call (arg 1) result #> [,1] [,2] [,3] #> [1,] 0.02271428 0.01841934 NA #> [2,] 0.02916494 0.02374206 NA #> [3,] 0.03599915 0.02943263 NA #> [4,] 0.04326174 0.03558258 NA #> [5,] 0.05102744 0.04230549 NA #> [6,] 0.05937594 0.04978273 NA #> [7,] 0.06842678 0.05844826 NA #> [8,] 0.07832443 0.06780540 NA #> [9,] 0.08940262 0.06780540 NA #> [10,] 0.10219933 0.06780540 NA #> [11,] 0.11765102 0.06780540 NA #> [12,] 0.13991202 0.06780540 NA #> [13,] 0.19924810 0.06780540 NA #> [14,] 0.19924810 0.06780540 NA #> [15,] 0.19924810 0.06780540 NA #> [16,] 0.19924810 0.06780540 NA On Sat, 19 Aug 2017, Dagmar wrote:> Dear all, > > I have a data similar to this: > > myframe<- data.frame (ID=c("Ernie", "Ernie","Ernie","Ernie"), > Timestamp=c("24.09.2012 08:00", "24.09.2012 09:00", "24.09.2012 10:00", > "25.09.2012 10:00"), Longitude=c("8.481","8.482","8.483","8.481"), > Latitude=c("54.753","54.753","54.752","54.751") > ) > myframe > > # Now this is where my loop is supposed to start. In this example I want to > run the following functions 3 times. (In real life more often.) How do I do > that? > > library(adehabitatHR) > library(rgdal) > mysample <- myframe[sample(1:nrow(myframe), 3,replace=FALSE),] > mysample > mysampletime <- as.POSIXct (strptime(as.character(mysample$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > mysamplexy <- project (cbind (mysample$Longitude, mysample$Latitude), > "+proj=utm +zone=32 +ellps=WGS84") > colnames(mysamplexy) <- c ("xCord", "yCord") > ID <- mysample$ID > datltraj <- as.ltraj(mysamplexy, mysampletime, id=ID) > Ddat <- BRB.D(datltraj, Tmax=21600, Lmin=36) > BRBdat <- BRB(datltraj, D= Ddat,type=c("UD"),Tmax=21600,Lmin=36, hmin=100) > kernel.area(BRBdat, unout=c("km2")) > > # unfortunately my data are not a very good example. Sorry about that. But I > guess you know what I mean. > > # Because I wish to run the functions three times I do want a data frame (or > matrix) in the end, which includes the results (kernel areas): That means 12 > columns and the three lines. > > # How do I do that? > > Many thanks in advance, > > Tagmarie > > ______________________________________________ > 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. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
Dagmar
2017-Aug-22 07:40 UTC
[R] My very first loop!! I failed. May I have some start-up aid?
Dear Jeff, Thank you! You helped me a lot! Tagmarie Am 19.08.2017 um 08:11 schrieb Jeff Newmiller:> Thank you for providing the example code... for the request of running > it multiple times it would have helped if you could have confirmed > that the example ran through without errors... there were a lot of > mistakes in it. Look into using the reprex package to check your > example next time. > > I don't do this kind of analysis... I really don't know what to expect > from the functions. The final step in your sequence produces a vector > of sixteen values, not 1 value, nor 4 values, so I don't know how to > obtain the 4x3 result you said you expected... I got 16x3. > > myframe <- data.frame( ID = c( "Ernie", > "Ernie","Ernie","Ernie","Ernie","Ernie") > ???????????????????? , Timestamp = c( "24.09.2012 08:00", "24.09.2012 > 09:00" > ??????????????????????????????????? , "24.09.2012 10:00", "25.09.2012 > 10:00" > ??????????????????????????????????? , "26.09.2012 10:00", "27.09.2012 > 10:00" > ??????????????????????????????????? ) > ???????????????????? , Longitude = c( 8.481, 8.482, 8.483, 8.481, > 8.483, 8.481 ) > ???????????????????? , Latitude = c( 54.753, 54.753, 54.752, 54.751, > 54.752, 54.751 ) > ???????????????????? , stringsAsFactors = FALSE > ???????????????????? ) > myframe > #>????? ID??????? Timestamp Longitude Latitude > #> 1 Ernie 24.09.2012 08:00???? 8.481?? 54.753 > #> 2 Ernie 24.09.2012 09:00???? 8.482?? 54.753 > #> 3 Ernie 24.09.2012 10:00???? 8.483?? 54.752 > #> 4 Ernie 25.09.2012 10:00???? 8.481?? 54.751 > #> 5 Ernie 26.09.2012 10:00???? 8.483?? 54.752 > #> 6 Ernie 27.09.2012 10:00???? 8.481?? 54.751 > > # Now this is where my loop is supposed to start. In this example I want > #to run the following functions 3 times. (In real life more often.) How > #do I do that? > > library(adehabitatHR) > #> Loading required package: sp > #> Loading required package: deldir > #> deldir 0.1-14 > #> Loading required package: ade4 > #> Loading required package: adehabitatMA > #> Loading required package: adehabitatLT > #> Loading required package: CircStats > #> Loading required package: MASS > #> Loading required package: boot > library(rgdal) > #> rgdal: version: 1.2-8, (SVN revision 663) > #>? Geospatial Data Abstraction Library extensions to R successfully > loaded > #>? Loaded GDAL runtime: GDAL 1.11.3, released 2015/09/16 > #>? Path to GDAL shared files: /usr/share/gdal/1.11 > #>? Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: > 492] > #>? Path to PROJ.4 shared files: (autodetected) > #>? Linking to sp version: 1.2-5 > > myframe$mysampletime <- as.POSIXct( myframe$Timestamp > ????????????????????????????????? , format = "%d.%m.%Y %H:%M" > ????????????????????????????????? , tz="GMT" > ????????????????????????????????? ) > > set.seed( 42 ) > N <- 3 > result <- matrix( NA, nrow = 16, ncol = N ) > for ( i in seq.int( N ) ) { > ? mysample <- myframe[ sample( seq_along( myframe[[ 1 ]] ) > ???????????????????????????? , 3 > ???????????????????????????? , replace=FALSE > ???????????????????????????? ) > ??????????????????? , > ??????????????????? ] > ? mysamplexy <- project( as.matrix( mysample[ , c( "Longitude" > ???????????????????????????????????????????????? , "Latitude" > ???????????????????????????????????????????????? ) > ??????????????????????????????????????????? ] > ????????????????????????????????? ) > ?????????????????????? , "+proj=utm +zone=32 +ellps=WGS84" > ?????????????????????? ) > ? colnames( mysamplexy ) <- c( "xCord", "yCord" ) > ? datltraj <- as.ltraj( mysamplexy, mysample$mysampletime, > id=mysample$ID ) > ? Ddat <- BRB.D( datltraj, Tmax=21600, Lmin=36 ) > ? BRBdat <- BRB( datltraj, D = Ddat, type = "UD", Tmax = 21600, Lmin = > 36, hmin = 100 ) > ? result[ , i ] <- kernel.area( BRBdat, unout = "km2" ) > } > #> Warning in kernel.area(BRBdat, unout = "km2"): The grid is too > small to allow the estimation of home-range > #> for the following value of percent: 70,75,80,85,90,95. You should > rerun kernelUD with a larger extent parameter > #> Warning in kernel.area(BRBdat, unout = "km2"): The grid is too > small to allow the estimation of home-range > #> for the following value of percent: > 30,35,40,45,50,55,60,65,70,75,80,85,90,95. You should rerun kernelUD > with a larger extent parameter > #> Error in getvolumeUD(x, standardize = standardize): NA/NaN/Inf in > foreign function call (arg 1) > result > #>???????????? [,1]?????? [,2] [,3] > #>? [1,] 0.02271428 0.01841934?? NA > #>? [2,] 0.02916494 0.02374206?? NA > #>? [3,] 0.03599915 0.02943263?? NA > #>? [4,] 0.04326174 0.03558258?? NA > #>? [5,] 0.05102744 0.04230549?? NA > #>? [6,] 0.05937594 0.04978273?? NA > #>? [7,] 0.06842678 0.05844826?? NA > #>? [8,] 0.07832443 0.06780540?? NA > #>? [9,] 0.08940262 0.06780540?? NA > #> [10,] 0.10219933 0.06780540?? NA > #> [11,] 0.11765102 0.06780540?? NA > #> [12,] 0.13991202 0.06780540?? NA > #> [13,] 0.19924810 0.06780540?? NA > #> [14,] 0.19924810 0.06780540?? NA > #> [15,] 0.19924810 0.06780540?? NA > #> [16,] 0.19924810 0.06780540?? NA > > ?On Sat, 19 Aug 2017, Dagmar wrote: > >> Dear all, >> >> I have a data similar to this: >> >> myframe<- data.frame (ID=c("Ernie", "Ernie","Ernie","Ernie"), >> Timestamp=c("24.09.2012 08:00", "24.09.2012 09:00", "24.09.2012 10:00", >> "25.09.2012 10:00"), Longitude=c("8.481","8.482","8.483","8.481"), >> Latitude=c("54.753","54.753","54.752","54.751") >> ) >> myframe >> >> # Now this is where my loop is supposed to start. In this example I >> want to run the following functions 3 times. (In real life more >> often.) How do I do that? >> >> library(adehabitatHR) >> library(rgdal) >> mysample <- myframe[sample(1:nrow(myframe), 3,replace=FALSE),] >> mysample >> mysampletime <- as.POSIXct >> (strptime(as.character(mysample$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") >> mysamplexy <- project (cbind (mysample$Longitude, mysample$Latitude), >> "+proj=utm +zone=32 +ellps=WGS84") >> colnames(mysamplexy) <- c ("xCord", "yCord") >> ID <- mysample$ID >> datltraj <- as.ltraj(mysamplexy, mysampletime, id=ID) >> Ddat <- BRB.D(datltraj, Tmax=21600, Lmin=36) >> BRBdat <- BRB(datltraj, D= Ddat,type=c("UD"),Tmax=21600,Lmin=36, >> hmin=100) >> kernel.area(BRBdat, unout=c("km2")) >> >> # unfortunately my data are not a very good example. Sorry about >> that. But I guess you know what I mean. >> >> # Because I wish to run the functions three times I do want a data >> frame (or matrix) in the end, which includes the results (kernel >> areas): That means 12 columns and the three lines. >> >> # How do I do that? >> >> Many thanks in advance, >> >> Tagmarie >> >> ______________________________________________ >> 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. >> > > --------------------------------------------------------------------------- > > Jeff Newmiller??????????????????????? The???? .....?????? ..... Go > Live... > DCN:<jdnewmil at dcn.davis.ca.us>??????? Basics: ##.#. ##.#.? Live Go... > ????????????????????????????????????? Live:?? OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries??????????? O.O#.?????? #.O#. with > /Software/Embedded Controllers)?????????????? .OO#.?????? .OO#. > rocks...1k > --------------------------------------------------------------------------- > >
Possibly Parallel Threads
- My very first loop!! I failed. May I have some start-up aid?
- My very first loop!! I failed. May I have some start-up aid?
- Sampling problems
- Which Durbin-Watson is correct? (weights involved) - using durbinWatsonTest and dwtest (packages car and lmtest)
- Question About Repeat Random Sampling from a Data Frame