[英]R - calculating values assigned to the same number
I have a question regarding this data: 我对此数据有疑问:
data =
1 time 3
2 20 0
3 20 0
4 20 0
5 350 1
6 350 1
7 350 1
8 10 0
9 20 1
10 37 0
11 37 0
12 50 1
13 50 1
14 40 0
15 40 0
16 40 0
I want to summarize the time that someone spent looking at 1 (as indexed in column 3). 我想总结一下某人花在看1上的时间(在第3列中进行了索引)。 Time always assigned the total looking time when 1 was looked at - so I need to summarize only the first time when 1 is newly indicated - something like 350 + 20 + 50.
时间总是分配观看1时的总观看时间-因此,我只需要在第一次新显示1时总结一下时间-例如350 + 20 + 50。
An if
-loop like: 一个
if
-loop像:
if (data$3 == 1) {
sum <- data[:,2] }
does not work, as all the values get summarized. 不起作用,因为所有值都会汇总。 I will need something that addresses always only the first 1 after a 0.
我将需要始终只解决0之后的第一个1的问题。
Use ddply
from plyr package (mydata is your data and col3 is actually column 3 in your data with the name col3. 使用
ddply
软件包中的ddply(mydata是您的数据,col3实际上是数据中列3的名称为col3的列。
mydata mydata
> mydata
col1 time col3
1 1 20 0
2 2 20 0
3 3 20 0
4 4 350 1
5 5 350 1
6 6 350 1
7 7 10 0
8 8 20 1
9 9 37 0
10 10 37 0
11 11 50 1
12 12 50 1
13 13 40 0
14 14 40 0
15 15 40 0
library(plyr)
ddply(mydata,.(col3), summarize, mysum=sum(unique(time)))
col3 mysum
1 0 107
2 1 420
data = read.table(text = '
1 time 3
2 20 0
3 20 0
4 20 0
5 350 1
6 350 1
7 350 1
8 10 0
9 20 1
10 37 0
11 37 0
12 50 1
13 50 1
14 40 0
15 40 0
16 40 0
', header = TRUE)
data$first <- sequence(rle(data$time)$lengths)
data
sum(data$time[data$first==1 & data$X3==1])
# [1] 420
df2 <- unique(df[df$X3 == 1, c("time", "X3")])
sum(df2$time)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.