0

I have a .csv file with several columns, but I am only interested in two of the columns(TIME and USER). The USER column consists of the value markers 1 or 2 in chunks and the TIME column consists of a value in seconds. I want to calculate the difference between the TIME value of the first 2 in a chunk in the USER column and the first 1 in a chunk in the USER column. I want to accomplish this through R. It would be ideal for their to be another column added to my data file with these differences.

So far I have only imported the .csv into R.

Latency <- read.csv("/Users/alinazjoo/Documents/Latency_allgaze.csv")

user3849871
  • 33
  • 1
  • 3
  • It would be much easier if you included [sample data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your question. Read the answers at that link for tips on how to do that. – MrFlick Jul 17 '14 at 15:54

1 Answers1

0

I'm going to guess your data looks like this

# sample data
set.seed(15)
rr<-sample(1:4, 10, replace=T)
dd<-data.frame(
   user=rep(1:5, each=10),
   marker=rep(rep(1:2,10), c(rbind(rr, 5-rr))),
   time=1:50
)

Then you can calculate the difference using the base function aggregate and transform. Observe

namin<-function(...) min(..., na.rm=T)
dx<-transform(aggregate(
    cbind(m2=ifelse(marker==2,time,NA), m1=ifelse(marker==1, time,NA)) ~ user, 
    dd, namin, na.action=na.pass),
    diff = m2-m1)    
dx

#   user m2 m1 diff
# 1    1  4  1    3
# 2    2 15 11    4
# 3    3 23 21    2
# 4    4 35 31    4
# 5    5 44 41    3

We use aggregate to find the minimal time for each of the two kinds or markers, then we use transform to calculate the difference between them.

MrFlick
  • 195,160
  • 17
  • 277
  • 295