Tagmarie
2012-Sep-26 10:53 UTC
[R] average environmental data if AnimalID and Time is duplicated
Hello,
I tried for about three hours now to solve this problem but I can't figure
it out. I am sure someone knows how do it. At least I hope so.
I have a data frame somewhat like this:
myframe <- data.frame (ID=c("Ernie", "Ernie",
"Bert", "Bert"),
Timestamp=c("24.09.2012 09:00", "24.09.2012 09:00",
"24.09.2012 10:00",
"25.09.2012 10:00"),
Hunger=c("1","5","2","2"),
Temp=c("25","30","27","28")
)
myframe
As you can see for Ernie I do have different data for 24.09.2012 9:00. Now I
would like to average the Hunger and Temp value for this timestamp to get a
data frame without duplicated Times and the respective average Temp and
Hunger.
I tried something like
Meanframe<- by(myframe[, 3:4], duplicated(myframe$ID,
Zusatzdaten3$Timestamp) == TRUE, mean)
but it doesn't work and I guess that it is also totally crap ;-)
Tagmarie
--
View this message in context:
http://r.789695.n4.nabble.com/average-environmental-data-if-AnimalID-and-Time-is-duplicated-tp4644218.html
Sent from the R help mailing list archive at Nabble.com.
Jim Lemon
2012-Sep-26 11:29 UTC
[R] average environmental data if AnimalID and Time is duplicated
On 09/26/2012 08:53 PM, Tagmarie wrote:> Hello, > I tried for about three hours now to solve this problem but I can't figure > it out. I am sure someone knows how do it. At least I hope so. > > I have a data frame somewhat like this: > > myframe<- data.frame (ID=c("Ernie", "Ernie", "Bert", "Bert"), > Timestamp=c("24.09.2012 09:00", "24.09.2012 09:00", "24.09.2012 10:00", > "25.09.2012 10:00"), Hunger=c("1","5","2","2"), Temp=c("25","30","27","28") > ) > myframe > > As you can see for Ernie I do have different data for 24.09.2012 9:00. Now I > would like to average the Hunger and Temp value for this timestamp to get a > data frame without duplicated Times and the respective average Temp and > Hunger. > > I tried something like > Meanframe<- by(myframe[, 3:4], duplicated(myframe$ID, > Zusatzdaten3$Timestamp) == TRUE, mean) > but it doesn't work and I guess that it is also totally crap ;-) >Hi Tagmarie, Your problem is that both Hunger and Temp are read in as factors. If you try it like this: by(as.numeric(as.character(myframe[,3])),myframe[,"ID"],mean) by(as.numeric(as.character(myframe[,4])),myframe[,"ID"],mean) You might get what you want. The "as.character" call is necessary, otherwise you will get the wrong mean values. Jim
Rui Barradas
2012-Sep-26 11:52 UTC
[R] average environmental data if AnimalID and Time is duplicated
Hello,
Why do you have Hunger and Temp recorded as characters? Between double
quotes?
myframe <- data.frame (ID=c("Ernie", "Ernie",
"Bert", "Bert"),
Timestamp=c("24.09.2012 09:00", "24.09.2012 09:00",
"24.09.2012 10:00",
"25.09.2012 10:00"),
Hunger=c("1","5","2","2"),
Temp=c("25","30","27","28")
)
str(myframe)
myframe$Hunger <- as.numeric(levels(myframe$Hunger)[myframe$Hunger])
myframe$Temp <- as.numeric(levels(myframe$Temp)[myframe$Temp])
aggregate(cbind(Hunger, Temp) ~ ID, data = myframe, FUN = mean)
Hope this helps,
Rui Barradas
Em 26-09-2012 11:53, Tagmarie escreveu:> Hello,
> I tried for about three hours now to solve this problem but I can't
figure
> it out. I am sure someone knows how do it. At least I hope so.
>
> I have a data frame somewhat like this:
>
> myframe <- data.frame (ID=c("Ernie", "Ernie",
"Bert", "Bert"),
> Timestamp=c("24.09.2012 09:00", "24.09.2012 09:00",
"24.09.2012 10:00",
> "25.09.2012 10:00"),
Hunger=c("1","5","2","2"),
Temp=c("25","30","27","28")
> )
> myframe
>
> As you can see for Ernie I do have different data for 24.09.2012 9:00. Now
I
> would like to average the Hunger and Temp value for this timestamp to get a
> data frame without duplicated Times and the respective average Temp and
> Hunger.
>
> I tried something like
> Meanframe<- by(myframe[, 3:4], duplicated(myframe$ID,
> Zusatzdaten3$Timestamp) == TRUE, mean)
> but it doesn't work and I guess that it is also totally crap ;-)
>
> Tagmarie
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/average-environmental-data-if-AnimalID-and-Time-is-duplicated-tp4644218.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.