Hello, I have a data frame 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"), Hunger=c(1,1,1,2,2,1) ) myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL myframe2 Now I want to get the sum of "Hunger" for each day. In the end I want something which looks like the following dataframe: myoutcome <- data.frame(Timestamp=c("24.09.2012", "25.09.2012"), sumHunger=c(3, 5)) Does anyone know how to do that? That would be very helpful and to all people who are willing to help me: Thank you in advance! Best regards, Tagmarie -- View this message in context: http://r.789695.n4.nabble.com/sum-per-day-tp4650937.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try aggregate(Hunger ~ cut(myframe2$myframestime, "day"), data = myframe2, FUN = sum) Hope this helps, Rui Barradas Em 27-11-2012 09:13, Tagmarie escreveu:> Hello, > > I have a data frame 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"), Hunger=c(1,1,1,2,2,1) ) > myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), > "%d.%m.%Y %H:%M"), tz="GMT") > myframe2 <- cbind (myframe,myframestime) > myframe2$Timestamp <- NULL > myframe2 > > Now I want to get the sum of "Hunger" for each day. In the end I want > something which looks like the following dataframe: > > myoutcome <- data.frame(Timestamp=c("24.09.2012", "25.09.2012"), > sumHunger=c(3, 5)) > > Does anyone know how to do that? > That would be very helpful and to all people who are willing to help me: > Thank you in advance! > > Best regards, > Tagmarie > > > > -- > View this message in context: http://r.789695.n4.nabble.com/sum-per-day-tp4650937.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.
HI, Try this: library(plyr) res<-ddply(myframe2,.(Timestamp=as.Date(myframe2$myframestime)),function(x) sum(x$Hunger)) ?names(res)[2]<-"sumHunger" ?res[,1]<-format(res[,1],"%d.%m.%Y") ?res #?? Timestamp sumHunger #1 24.09.2012???????? 3 #2 25.09.2012???????? 5 A.K. ----- Original Message ----- From: Tagmarie <Ramgad82 at gmx.net> To: r-help at r-project.org Cc: Sent: Tuesday, November 27, 2012 4:13 AM Subject: [R] sum per day Hello, I have a data frame 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"), Hunger=c(1,1,1,2,2,1) )? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp), "%d.%m.%Y %H:%M"), tz="GMT") myframe2 <- cbind (myframe,myframestime) myframe2$Timestamp <- NULL? myframe2 Now I want to get the sum of "Hunger" for each day. In the end I want something which looks like the following dataframe: myoutcome <- data.frame(Timestamp=c("24.09.2012", "25.09.2012"), sumHunger=c(3, 5)) Does anyone know how to do that? That would be very helpful and to all people who are willing to help me: Thank you in advance! Best regards, Tagmarie -- View this message in context: http://r.789695.n4.nabble.com/sum-per-day-tp4650937.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.