surreyj
2011-Jan-14 09:22 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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 (and each of these events have a corresponding time in another column). Stat71 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 Using "which" I have selected all the individual occurrences of each event and created variables with the time of said events within them which I used to calculate differences in time etc for analysis. But now I am faced with the issue that I need to select only the first "Resp" that occurs after a "MagDwn" so that I can calculate the differences in time between these two events. Any help would be appreciated, usually I just read these forums and try and figure out problems for myself but this is for my masters and I'm running out of time to get it finished :) Thanks 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-tp3217340p3217340.html Sent from the R help mailing list archive at Nabble.com.
Taras Zakharko
2011-Jan-14 09:57 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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: http://r.789695.n4.nabble.com/Selecting-the-first-occurrence-of-a-value-after-an-occurrence-of-a-different-value-tp3217340p3217385.html Sent from the R help mailing list archive at Nabble.com.
surreyj
2011-Jan-14 10:55 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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 from the R help mailing list archive at Nabble.com.
Bart Joosen
2011-Jan-14 11:21 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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 found return(as.vector(as.numeric(names(outp))+outp - 1)) } f.search(x, "MagDwn", "Resp") Good luck Bart -- 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-tp3217340p3217482.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2011-Jan-14 12:12 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
Try this: which(x %in% c("MagDwn", "Resp"))[2] On Fri, Jan 14, 2011 at 7:22 AM, surreyj <surreyjackson@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 columns, one with a time and one with occurence of certain > events. > > > The column of data I want to select from looks like this (and each of these > events have a corresponding time in another column). > > Stat71 > 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 > > > Using "which" I have selected all the individual occurrences of each event > and created variables with the time of said events within them which I used > to calculate differences in time etc for analysis. > > But now I am faced with the issue that I need to select only the first > "Resp" that occurs after a "MagDwn" so that I can calculate the differences > in time between these two events. > > Any help would be appreciated, usually I just read these forums and try and > figure out problems for myself but this is for my masters and I'm running > out of time to get it finished :) > > Thanks > > 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-tp3217340p3217340.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
surreyj
2011-Jan-15 02:01 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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, origin="timeofstart[1]", format="%Y,%m,%d, %H, %M, %OS") Cheers 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-tp3217340p3218653.html Sent from the R help mailing list archive at Nabble.com.
surreyj
2011-Jan-17 07:09 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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: onlyfirstresponseafterrft<-which(!diff(as.numeric(factor(Stat, levels c("MagDwn", "Resp")))))> onlyfirstresponseafterrft[1] 11 12 13 19 20 21 27 28 29 35 36 37 43 44 45 51 52 53 [19] 59 60 61 67 68 69 75 76 77 83 84 85 91 92 93 103 104 105 [37] 113 114 115 121 122 123 129 130 131 137 138 139 145 146 147 153 154 155 [55] 161 162 163 173 174 175 181 182 183 191 192 193 199 200 201 207 208 209 [73] 215 216 217 225 226 227 233 234 235 241 242 243 251 252 253 259 260 261 [91] 267 268 269 275 276 277 283 284 285 291 292 293 303 304 305 311 312 313 [109] 319 320 321 329 330 331 337 338 339 to get my results and what is being delivered is the rows at which a "resp" occurs after a "magdwn" except I only want the first "resp" after a mag down... This seems simple to figure out and I have tried a lot of things but its just not happening! Can anyone help me please? Thanks -- 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-tp3217340p3220788.html Sent from the R help mailing list archive at Nabble.com.
surreyj
2011-Jan-17 08:40 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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 these files I have been using which to select the appropriate event codes so I can do analysis on timing between events using the time appropriate time coloumn. This has worked well so far and now I am faced with the problem that I need to select the first "Resp" that occurs after each "MagDwn". Sometimes there will be just one "Resp" in between a "MagDwn" and sometimes there will be many e.g. up to 500. This code onlyfirstresponseafterrft<-which(!diff(as.numeric(factor(Stat, levels = c("MagDwn", "Resp"))))) allowed me to select the first "Resp" (or so I thought) but in the file I tried it on there were only two occurences of "Resp" inbetween each "MagDwn" and now that I have tried it on files with more "Resp" it is actually selecting all but the last one that happens before the "Mag Dwn" but I only need the first one. Hope that makes sense, thanks for your help. -- 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-tp3217340p3220852.html Sent from the R help mailing list archive at Nabble.com.
Petr Savicky
2011-Jan-17 08:50 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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 using: > > onlyfirstresponseafterrft<-which(!diff(as.numeric(factor(Stat, levels > c("MagDwn", "Resp")))))[...]> > to get my results and what is being delivered is the rows at which a "resp" > occurs after a "magdwn" except I only want the first "resp" after a mag > down... This seems simple to figure out and I have tried a lot of things but > its just not happening!Is it required to select positions with Resp, which are the end-points of subsequences of the form MagDwn other^* Resp ? For the sequence 1 MagDwn 2 other 3 MagDwn 4 Resp 5 other 6 Resp 7 MagDwn 8 other 9 Resp this would be positions 4 and 9. The positions of Resp in these end-points may be computed, for example Vals <- c("MagDwn", "Resp", "other") Stat <- Vals[c(1, 3, 1, 2, 3, 2, 1, 3, 2)] ind <- which(Stat %in% c("MagDwn", "Resp")) Reduced <- Stat[ind] ind[which(diff(Reduced == "Resp") == 1) + 1] # [1] 4 9 The positions of the corresponding MagDwn are ind[which(diff(Reduced == "Resp") == 1)] # [1] 3 7 Petr Savicky.
surreyj
2011-Jan-17 23:29 UTC
[R] Selecting the first occurrence of a value after an occurrence of a different value
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.