[英]R - Find the begin and the end of timestamp according with values in another column
Dears, 亲爱,
I am newby in R programming, for this reason, I come here to ask you for help. 我是R编程的新手,因此,我来这里寻求帮助。 I am trying to figure out a way to solve this issue. 我正在尝试找出解决此问题的方法。 I have been trying hard but without success. 我一直在努力,但没有成功。
I have a data.frame similar to that... 我有一个类似于的data.frame ...
df2 <- data.frame(Recordig = c("Rec1", "Rec1", "Rec1", "Rec1", "Rec1", "Rec1",
"Rec2","Rec2","Rec2","Rec2","Rec2","Rec2"),
MediaName = c("Imagem1","Imagem1","Imagem1",
"Estimulo1","Estimulo1","Estimulo1",
"Imagem1","Imagem1","Imagem1",
"Estimulo1","Estimulo1","Estimulo1"),
Timestamp = c( 4975 , 5155 , 5312 ,25076, 25463 ,26040 , 5035 , 5248, 5551, 17047 , 17263, 17533))
simplified version below 下面的简化版
Recordig MediaName Timestamp
1 Rec1 Imagem1 4975
2 Rec1 Imagem1 5155
3 Rec1 Imagem1 5312
4 Rec1 Estimulo1 25076
5 Rec1 Estimulo1 25463
6 Rec1 Estimulo1 26040
7 Rec2 Imagem1 5035
8 Rec2 Imagem1 5248
9 Rec2 Imagem1 5551
10 Rec2 Estimulo1 17047
11 Rec2 Estimulo1 17263
12 Rec2 Estimulo1 17533
What is my point? 我的意思是什么? I need to know exactly how much time the participant (eg Rec1) spent viewing each image (Image1). 我需要确切知道参与者(例如Rec1)花了多少时间查看每个图像(Image1)。 In this case, the Timestamp for Image1 started at 4.975s and ended at 5.312 s, giving 333 ms 在这种情况下,Image1的时间戳开始于4.975s,结束于5.312 s,给出了333 ms
The point is that I have hundreds of images and thousand of respondents that spent differents time for observing the images . 关键是我有数百幅图像,成千上万的受访者花费不同的时间来观察图像。
Is there anyone with some idea to help me, please? 请问有人可以帮助我吗?
You can find the difference between the first and last timestamp for each participant ( Recordig
) and image ( MediaName
) using the dplyr
package: 您可以使用dplyr
包找到每个参与者的第一个时间戳和最后一个时间戳( Recordig
)和图像( MediaName
)之间的dplyr
:
library(dplyr)
df3 <- df2 %>%
dplyr::group_by(Recordig, MediaName) %>%
dplyr::summarise(duration = diff(range(Timestamp)))
df3
# Source: local data frame [4 x 3]
# Groups: Recordig [?]
#
# Recordig MediaName duration
# <fctr> <fctr> <dbl>
# 1 Rec1 Estimulo1 964
# 2 Rec1 Imagem1 337
# 3 Rec2 Estimulo1 486
# 4 Rec2 Imagem1 516
We can use base R
我们可以使用base R
aggregate(cbind(duration = Timestamp) ~Recordig + MediaName, df2,
FUN = function(x) diff(range(x)))
# Recordig MediaName duration
#1 Rec1 Estimulo1 964
#2 Rec2 Estimulo1 486
#3 Rec1 Imagem1 337
#4 Rec2 Imagem1 516
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.