简体   繁体   English

For 循环以获取跨多个向量的索引平均值(时间序列平均值图)

[英]For loop to get the average values of indices across multiple vectors (Time Series Average Plot)

I am currently developing my pipeline in R for data processing/analysis.我目前正在 R 中开发用于数据处理/分析的管道。

My data is in a long format (sample rate = 1000Hz).我的数据采用长格式(采样率 = 1000Hz)。 Throughout the dataframe I have added a trialNum variable for each trial, but I am having issues reshaping my data to wide.在整个数据框中,我为每个试验添加了一个 trialNum 变量,但是我在将我的数据重新整形为广泛时遇到了问题。

What I am trying to do, and I think should be possible with a for loop or two... Is to get the average value of x at index 1:100, based on the trialNum.我正在尝试做的事情,我认为应该可以使用一个或两个 for 循环......是根据 trialNum 在索引 1:100 处获得 x 的平均值。

Here is a simple version...这是一个简单的版本...

Pupil Size瞳孔大小 TrialNum试用号
500 500 1 1
502 502 1 1
504 504 1 1
506 506 1 1
508 508 1 1
507 507 2 2
508 508 2 2
510 510 2 2
511 511 2 2
512 512 2 2
513 513 3 3
515 515 3 3
514 514 3 3
512 512 3 3
515 515 3 3

So stated simply... I would get the first index of Pupil size for each TrialNum, and average together, and add to a new variable (average_pupil_size).所以简单地说......我会得到每个 TrialNum 的瞳孔大小的第一个索引,然后平均在一起,然后添加到一个新变量(average_pupil_size)。

In this example, each trial has 5 inputs, so I would end up with a variable output of length = 5...在这个例子中,每个试验都有 5 个输入,所以我最终会得到一个长度为 5 的可变输出......

average_size <- c(507, 508, 509, 510, 512)

I could then plot this signal for all my trials... I hope I have explained myself clearly... Apologies for the chaos that is my mind.然后我可以为我所有的试验绘制这个信号……我希望我已经清楚地解释了自己……为我头脑中的混乱道歉。

Does anyone know how to do this?有谁知道如何做到这一点? It is a bit beyond me.这有点超出我的想象。

Thanks in advance!提前致谢!

We could add an index within each TrialNum using row_number() , and then group-summarize within those.我们可以使用row_number()在每个 TrialNum 中添加一个索引,然后在其中进行分组汇总。

library(dplyr)
df %>%
  group_by(TrialNum) %>%
  mutate(index = row_number()) %>%
  group_by(index) %>%
  summarize(avg = mean(Pupil.Size))

Result结果

# A tibble: 5 × 2
  index   avg
  <int> <dbl>
1     1  507.
2     2  508.
3     3  509.
4     4  510.
5     5  512.

in base R, if the data has same length for each trial, eg in this case 5, we can do:在基础 R 中,如果每个试验的数据长度相同,例如在这种情况下为 5,我们可以这样做:

rowMeans(unstack(df))

[1] 506.6667 508.3333 509.3333 509.6667 511.6667

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM