if statement - R - calculating values assigned to the same number -
i have question regarding 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 summarize time spent looking @ 1 (as indexed in column 3). time assigned total looking time when 1 looked @ - need summarize first time when 1 newly indicated - 350 + 20 + 50.
an if
-loop like:
if (data$3 == 1) { sum <- data[:,2] }
does not work, values summarized. need addresses first 1 after 0.
use ddply
plyr package (mydata data , col3 column 3 in data name col3.
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
Comments
Post a Comment