[R] adding aggregate data to data frames
jrkrideau at yahoo.ca
Fri Oct 19 18:50:18 CEST 2007
I have been trying to figure out how Hadley Wickhams
"reshape" package works and I tried it on what may be
your problem. Here is my example. Does it do
something like what you want?
# Test Data
zz <- "subj cond t1 t2 t3
A 1 4 5 7
A 2 5 2 8
B 2 3 7 3
C 1 5 4 4
B 1 4 5 5
C 2 3 7 7"
mydata <- read.table(textConnection(zz), header=TRUE,
# Melt the data.frame
df1 <- melt(mydata, id=c("subj", "cond"),
measured=c("t1","t2","t3")) ; df1
# calculate the mean and sd
stats <- cast(df1, subj + cond ~ . , function(x)
c(mean= mean(x), sd=sd(x)))
# Merge the original data.frame and the stats
data.frame to get the
# desired results.
combined.results <- merge(mydata,stats,
--- Dieter Vanderelst
<dieter_vanderelst at emailengine.org> wrote:
> Dear List,
> I have a data frame containing reaction times of
> participants in some experiment.
> As usual each line is single trial in the
> experiment. Two factors denote the conditions in the
> experiment. Each participant completes different
> trials for each condition.
> Now, the question:
> I want to calculate per participant, per condition
> the mean reaction time and its standard deviation.
> I can do this using AGGREGATE(). However, I want to
> merge this info with the original data frame. This
> is, I want each line to contain the mean and SD of
> the reaction time for the participant and condition
> on that line.
> I have tried to solve this by looping trough data
> frame. For each line, I select using SUBSET() the
> lines that belong to the same participant and
> condition. Then I calculate the average/SD. But this
> takes a long time.
> BYTW: I find that finding proper subject for r-help
> list mails, is very hard. So, if any one knows a set
> of better keywords...
> Any ideas?
Perhaps a random word generator?
> Dieter Vanderelst
More information about the R-help