[R] My very first loop!! I failed. May I have some start-up aid?

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Aug 19 06:04:09 CEST 2017


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



More information about the R-help mailing list