Skip to content
Prev 306318 / 398506 Next

average environmental data if AnimalID and Time is duplicated

HI,

Just to add to Jim's solution with data.table()
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")
?)
library(data.table)
myframe1<-data.table(myframe)
?myframe2<-within(myframe1,{Hunger<-as.numeric(as.character(Hunger)); Temp<-as.numeric(as.character(Temp))})
?myframe2[,list(meanHunger=mean(Hunger),meanTemp=mean(Temp)),list(ID,Timestamp)]
?# ??? ID??????? Timestamp meanHunger meanTemp
#1: Ernie 24.09.2012 09:00????????? 3???? 27.5
#2:? Bert 24.09.2012 10:00????????? 2???? 27.0
#3:? Bert 25.09.2012 10:00????????? 2???? 28.0
A.K.


----- Original Message -----
From: Jim Lemon <jim at bitwrit.com.au>
To: Tagmarie <Ramgad82 at gmx.net>
Cc: r-help at r-project.org
Sent: Wednesday, September 26, 2012 7:29 AM
Subject: Re: [R] average environmental data if AnimalID and Time is duplicated
On 09/26/2012 08:53 PM, Tagmarie wrote:
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

______________________________________________
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.