-1

In my data set in R, respondents were exposed to a stimuli, and their reactions were studied at baseline, one hour after exposure, and two hours after. In R, I adjusted the data by baseline. Here is an example of what my data looks like:

stimuli_no     base     hour   two_hour
1              0        0.02   -0.10
2              0        0.01   -0.03
3              0       -0.01    0.02
1              0       -0.05   -0.06
2              0        0.03    0.05
3              0        0.02    0.04

First thing I want to is get the mean of each time interval by the stimuli_no, which I did with this code:

transform(df, m_base = ave(base, stimuli_no), m_hour = ave(hour, stimuli_no),  m_twoh = ave(two_hour, stimuli_no))

Now I want to make a line graph that has the time intervals of baseline, hour, and two hour on the x axis, and the scores on the y axis, with separate lines for each of the stimuli. Here is an example:

enter image description here

Is there a simple way to do this in R with my data as is, or do I need to restructure my data? If I need to restructure, how would I go about that?

  • Most often you'll want to reshape your dataset into a long format. It is possible to leave your data in this wide format, but it can take a little more work. See the two answers with the most votes for [this question](https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) to see the main options (including reshaping). – aosmith Jul 07 '21 at 18:36

1 Answers1

1

Look into pivot_longer(), that has the functionality you're looking for. In long format you can also use the group_by() command to get means for each time interval. Both of these are tidyverse functions.

cbowers
  • 137
  • 8