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.


From what I understood, you need to filter out column names from Dataset2 based on certain criteria in Dataset1.

mySegregatedList<- lapply(unique(dataset1$gender),function(x){ dataset1[dataset$gender==x,'participantID']})


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

  • $\begingroup$ 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? $\endgroup$
    – Slyrs
    Apr 19 '16 at 21:35
  • $\begingroup$ yea. you are right on both the instances. It should hv been dataset1 and mySegregatedList instead. Thanks for point those out :) $\endgroup$ Apr 20 '16 at 9:08
  • $\begingroup$ Thank you again. mydata is new dataset, right? May I ask you again: I geht the Error: unexpected symbol in df<- lapply(mySegregatedList,function(x) {mydata<- physio_scr[,x] return(rowMeans(mydata))}) $\endgroup$
    – Slyrs
    Apr 20 '16 at 12:54
  • $\begingroup$ I tried that out before putting it up here. So that has something to do with your environment. Will suggest a few things: 1. may be using semicolon before return could help 2. use debug() for finding the exact location. $\endgroup$ Apr 20 '16 at 13:21
  • $\begingroup$ Thanks Anurag. This helped a lot. I had to add a semicolon: df<- lapply(mySegregatedList,function(x) {mydata<- dataset2[,x]; return(rowMeans(mydata))}) $\endgroup$
    – Slyrs
    Apr 20 '16 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.