Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-17 Thread Petr Savicky
On Sun, Jan 16, 2011 at 11:09:58PM -0800, surreyj wrote: Hello, Back again, I thought the problem was solved but I realised that the only reason I was getting the correct answer was because my data set happened to only have two rfts to choose from, so it looked correct. I have been

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-17 Thread surreyj
Hi Freddy, I have a long column of event codes e.g. below (in multiple files) that I am trying to analyse. OutMag FirstResp InMag MagUp OutMag MagDwn Resp Resp Resp InMag MagUp OutMag InMag OutMag InMag OutMag InMag OutMag InMag MagDwn OutMag Resp MagUp InMag MagDwn OutMag Resp MagUp With

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-17 Thread surreyj
Thanks so Peter, works great! Surrey -- View this message in context: http://r.789695.n4.nabble.com/Selecting-the-first-occurrence-of-a-value-after-an-occurrence-of-a-different-value-tp3217340p3221271.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-16 Thread surreyj
Hello, Back again, I thought the problem was solved but I realised that the only reason I was getting the correct answer was because my data set happened to only have two rfts to choose from, so it looked correct. I have been using:

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-15 Thread surreyj
Thanks so much for your help everyone, got it sorted now :) this is what I used in the end: timeofonlyfirstresponseafterrft-testdata$Time[test-which(!diff(as.numeric(factor(Stat, levels = c(MagDwn, Resp)] timeofonlyfirstresponseafterrft1-as.POSIXct(timeofonlyfirstresponseafterrft,

[R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-14 Thread surreyj
Hello everyone, I am currently working with some data where I need to select the first occurrence of a value after the occurrence of another value. The data has two columns, one with a time and one with occurence of certain events. The column of data I want to select from looks like this

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-14 Thread Taras Zakharko
Hi, I'll do something like this (there is probably a nicer way, but this code is what first comes up to my mind) provided your data is in x pos1 - which(x %in% val1)[1] pos2 - which(x %in% val2)[which(x %in% val2) pos1][1] -- View this message in context:

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-14 Thread Henrique Dallazuanna
Try this: which(x %in% c(MagDwn, Resp))[2] On Fri, Jan 14, 2011 at 7:22 AM, surreyj surreyjack...@gmail.com wrote: Hello everyone, I am currently working with some data where I need to select the first occurrence of a value after the occurrence of another value. The data has two

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-14 Thread Henrique Dallazuanna
Other option: which(!diff(as.numeric(factor(x, levels = c(MagDwn, Resp)[1] On Fri, Jan 14, 2011 at 10:12 AM, Henrique Dallazuanna www...@gmail.comwrote: Try this: which(x %in% c(MagDwn, Resp))[2] On Fri, Jan 14, 2011 at 7:22 AM, surreyj surreyjack...@gmail.com wrote: Hello

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-14 Thread surreyj
Thanks so much that works, but I just realised I was unclear, I meant I need to select all the MagDwn after every Resp :) -- View this message in context: http://r.789695.n4.nabble.com/Selecting-the-first-occurrence-of-a-value-after-an-occurrence-of-a-different-value-tp3217340p3217456.html Sent

Re: [R] Selecting the first occurrence of a value after an occurrence of a different value

2011-01-14 Thread Bart Joosen
Ofcourse you can loop over your data, but a vectorised way: f.search - function(x, ref, search) { cs - match(cumsum(x==ref), cumsum(x==ref)) outp - suppressWarnings(tapply(x==search,cs, function(x) min(which(x==1 outp - outp[outp!=Inf] # To remove the occurences where nothing was