Hello, I tried to construct my very first loop today and completly failed :-( Maybe someone can help me? I have a dataframe somewhat like this one: myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00", "24.09.2012 11:00", "25.09.2012 09:00", "25.09.2012 10:00", "25.09.2012 11:00"), Speed=c(1,1,2,5,1,6)) myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL myframe2 I want to construct a loop for every day, i.e. for each day I want to do some calculations. (I know in the example it would be easier to do it differently, my real data are little more complex). And BTW: Thanks for helping me earlier today with that other problem :-) -- View this message in context: http://r.789695.n4.nabble.com/loop-with-date-tp4650961.html Sent from the R help mailing list archive at Nabble.com.
On Tuesday, November 27, 2012, Tagmarie wrote:> Hello, > I tried to construct my very first loop today and completly failed :-( > Maybe someone can help me? > I have a dataframe somewhat like this one: > > myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00", > "24.09.2012 11:00", > "25.09.2012 09:00", "25.09.2012 10:00", > "25.09.2012 11:00"), > Speed=c(1,1,2,5,1,6)) > myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > myframe2 <- cbind (myframe,myframestime) > myframe2$Timestamp <- NULL > myframe2Where's the loopy bit that fails? Cheers, MW> > I want to construct a loop for every day, i.e. for each day I want to do > some calculations. > (I know in the example it would be easier to do it differently, my real > data > are little more complex). > > And BTW: Thanks for helping me earlier today with that other problem :-) > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/loop-with-date-tp4650961.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org <javascript:;> mailing list > 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]]
HI, If you want to do daily mean, sum etc. you could try ?tapply(), ?ave(), ?aggregate(), ?ddply() etc. For e.g. ?ave(myframe2$Speed,as.Date(myframe2$myframestime),FUN=sum) #[1]? 4? 4? 4 12 12 12 tapply(myframe2$Speed,as.Date(myframe2$myframestime),FUN=mean) #2012-09-24 2012-09-25 ?# 1.333333?? 4.000000 It is better to show the complex data as an example using dput() A.K. ----- Original Message ----- From: Tagmarie <Ramgad82 at gmx.net> To: r-help at r-project.org Cc: Sent: Tuesday, November 27, 2012 9:02 AM Subject: [R] loop with date Hello, I tried to construct my very first loop today and completly failed :-( Maybe someone can help me? I have a dataframe somewhat like this one: myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00", "24.09.2012 11:00", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "25.09.2012 09:00", "25.09.2012 10:00", "25.09.2012 11:00"), ? ? ? ? ? ? ? ? ? ? ? ? Speed=c(1,1,2,5,1,6))? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL? myframe2 I want to construct a loop for every day, i.e. for each day I want to do some calculations. (I know in the example it would be easier to do it differently, my real data are little more complex). And BTW: Thanks for helping me earlier today with that other problem :-) -- View this message in context: http://r.789695.n4.nabble.com/loop-with-date-tp4650961.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.
Here is an example of an approach:> myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00",+ "24.09.2012 11:00", + "25.09.2012 09:00", "25.09.2012 10:00", + "25.09.2012 11:00"), + Speed=c(1,1,2,5,1,6))> myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp),+ "%d.%m.%Y %H:%M"), tz="GMT")> myframe2 <- cbind (myframe,myframestime) > myframe2$Timestamp <- NULL > myframe2Speed myframestime 1 1 2012-09-24 09:00:00 2 1 2012-09-24 10:00:00 3 2 2012-09-24 11:00:00 4 5 2012-09-25 09:00:00 5 1 2012-09-25 10:00:00 6 6 2012-09-25 11:00:00> > # split the dataframe into "days' and then find average of Speed (for example) > tapply(myframe2$Speed, cut(myframe2$myframestime, 'day'), mean)2012-09-24 2012-09-25 1.333333 4.000000> >On Tue, Nov 27, 2012 at 9:02 AM, Tagmarie <Ramgad82 at gmx.net> wrote:> Hello, > I tried to construct my very first loop today and completly failed :-( > Maybe someone can help me? > I have a dataframe somewhat like this one: > > myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00", > "24.09.2012 11:00", > "25.09.2012 09:00", "25.09.2012 10:00", > "25.09.2012 11:00"), > Speed=c(1,1,2,5,1,6)) > myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > myframe2 <- cbind (myframe,myframestime) > myframe2$Timestamp <- NULL > myframe2 > > I want to construct a loop for every day, i.e. for each day I want to do > some calculations. > (I know in the example it would be easier to do it differently, my real data > are little more complex). > > And BTW: Thanks for helping me earlier today with that other problem :-) > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/loop-with-date-tp4650961.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Ok, sorry, I thought the more complex details might be confusing and nobody might answer. Here is something which looks more like my real dataframe and also what I want to do with it: That's my data frame: myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00", "24.09.2012 11:00", "25.09.2012 09:00", "25.09.2012 10:00", "25.09.2012 11:00"), Hunger=c(1,1,2,5,1,6) , Longitude=c(8.91617, 8.92700, 8.92711, 8.92722, 8.92733, 8.92744), Latitude=c(54.5485, 54.5410, 54.5412, 54.5413, 54.5414, 54.5424) , AnimalID= c(rep("Ernie"))) head(myframe) myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL myframesxy <- project(cbind(myframe2$Longitude,myframe2$Latitude),"+proj=utm +zone=32 +ellps=WGS84") colnames(myframesxy) <- c("Long", "Lat") myframe3 <- cbind(myframe2, myframesxy) myframe3$Longitude <- NULL myframe3$Latitude <- NULL myframe3 And here is what I want to do with it (make an ltraj element, calculate the brownian bridge homerange and get the kernel area of it - I take the 95 level): library(adehabitatHR) myframe3ltraj <- as.ltraj(myframesxy,myframestime, id=myframe3$AnimalID) myframeLiker <- liker (myframe3ltraj, sig2=18, rangesig1=c(1,10) ) myframeLiker MyframeBB <- kernelbb(myframe3ltraj, sig1=4.6036, sig2=18) kernel.area(MyframeBB, unout=c("km2") ) With the only difference that I don't want to calculate the homerange for the complete time but for each day. I could use "subset" and do it for every day by hand. But I'd have to do it for three month and then again for several animals. So I thought using a loop for each animal to get the results by date would be much faster. Does anybody have any idea? -- View this message in context: http://r.789695.n4.nabble.com/loop-with-date-tp4650961p4650983.html Sent from the R help mailing list archive at Nabble.com.
Possibly Parallel Threads
- mean of a value of the last 2 hours
- calculate a "rolling mean" including standard deviation
- apply a function at: dateX, dateX+1, dateX+2, ....
- average environmental data if AnimalID and Time is duplicated
- My very first loop!! I failed. May I have some start-up aid?