# Select and aggregate time series based on selection information of a second dataset

General problem: I have two datasets in r and I do not know how I can calculate information across groups of time series in one dataset based on selection-information of another dataset.

The details: The first dataset (dataframe imported from a csv-file) carries information about more than 200 participants of an experimental study (gender, age, experimental condition, psychographic variables, etc.). Every row is one participant, every column is a variable. Each participant is identified in the first column with a unique string, e.g. "AE123E".

I have a second dataset which carries physiological data that has been recorded from every participant during the experiment. This dataframe has one column for each participant (e.g. his/her heartrate), the rows are the seconds (several 1'000, because the sample rate was 40 values per second). The column names are the IDs of the participant (e.g., "AE123E"). Hence, this is a "time series" dataframe with time series of each participant in a column.

One task is now: I would like, for example, to compare the (average) time series between men and women, experimental conditions etc. For example, I would like to plot two timelines in one plot - one shows the average heartrate of woman (for every second over time), the other one shows the average heartrate of men (for every second over time). I was already able to plot all columns of physiological time series with ggplot.

My question is: How can I bring the two datasets together (or make a flexible selcetion) in an intelligent and convenient way? For example: Compute the mean across all men and across all women for every second and then plot this two new "columns" ( or time series) oder time.

mySegregatedList<- lapply(unique(dataset1$gender),function(x){ dataset1[dataset$gender==x,'participantID']}) 
names(mylist)<-unique(dataset1$gender) df<- lapply(mylist,function(x){mydata<- dataset2[,x] return(rowMeans(mydata))}) Then use df to plot. Though this answers your immediate query, a lot depends on how you want to slice-dice your data • Thank you very much for you help. In your second row: Mustn't it read dataset1[dataset1$gender? And: Do I have to use in the third row mySegregatedList instead of mylist? – Slyrs Apr 19 '16 at 21:35