Hi dear R users, I'm a R beginner and I have a basic question about sequential treatments of lists. I have a time based (i.e. events are consecutive) list of values of a biological property. Like : time value 1 5 2 10 3 7 4 10 5 19 6 21 7 20 8 18 9 10 10 7 11 8 12 12 13 17 14 19 15 24 16 18 17 15 18 10 19 9 [...] And I have to define a threshold and to attach each event to his group, i.e. values upper the threshold. Like, for a threshold value of 17 time value group 1 5 NA 2 10 NA 3 7 NA 4 10 NA 5 19 1 6 21 1 7 20 1 8 18 1 9 10 NA 10 7 NA 11 8 NA 12 12 NA 13 17 2 14 19 2 15 24 2 16 18 2 17 15 NA 18 10 NA 19 9 NA [...] The only solution that I have found is to do a sequentially read and write : for(i in 1:length(my_events_list)) { [...] } But I very slow. Do you have another ideas ? And after I need to extract maximum values for each group Like : group max_value 1 21 2 24 [...] and for each event which is part of a group to know if is't a ascending phase or no. Yes, lot of questions !! Sorry, but I think that the solution may be unique. In advance, thank you a lot regards JS _____________________________________________________________________________
Try nested ifelse() statements to label the group ex. time$group=ifelse(time$time<5,NA, ifelse(time$time<9,1, ifelse(time$time<13,NA, ifelse(time$time<17,2,NA) ) ) ) Then use aggregate to find the max value. ex. time.max=aggregate(time$value,list(group=time$group),max) On 21-May-07, at 6:39 AM, Robert wrote:> Hi dear R users, > > I'm a R beginner and I have a basic question about sequential > treatments of lists. > > I have a time based (i.e. events are consecutive) list of values of > a biological property. > > Like : > > time value > 1 5 > 2 10 > 3 7 > 4 10 > 5 19 > 6 21 > 7 20 > 8 18 > 9 10 > 10 7 > 11 8 > 12 12 > 13 17 > 14 19 > 15 24 > 16 18 > 17 15 > 18 10 > 19 9 > [...] > > > And I have to define a threshold and to attach each event to his > group, i.e. values upper the threshold. > > Like, for a threshold value of 17 > > time value group > 1 5 NA > 2 10 NA > 3 7 NA > 4 10 NA > 5 19 1 > 6 21 1 > 7 20 1 > 8 18 1 > 9 10 NA > 10 7 NA > 11 8 NA > 12 12 NA > 13 17 2 > 14 19 2 > 15 24 2 > 16 18 2 > 17 15 NA > 18 10 NA > 19 9 NA > [...] > > > The only solution that I have found is to do a sequentially read > and write : > for(i in 1:length(my_events_list)) > { > [...] > } > > But I very slow. Do you have another ideas ? > > And after I need to extract maximum values for each group > Like : > group max_value > 1 21 > 2 24 > [...] > > and for each event which is part of a group to know if is't a > ascending phase or no. > > > Yes, lot of questions !! Sorry, but I think that the solution may > be unique. > > In advance, thank you a lot > > regards > > JS > > > > > > > > > ______________________________________________________________________ > _______ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Mike Lawrence Graduate Student, Dalhousie University Department of Psychology http://myweb.dal.ca/mc973993 "The road to wisdom? Well, it's plain and simple to express: Err and err and err again, but less and less and less." - Piet Hein
This should do it for you. It makes use of 'rle' to find the 'runs' of thresholds and then changes the values to the sequence numbers> x <- "time value+ 1 5 + 2 10 + 3 7 + 4 10 + 5 19 + 6 21 + 7 20 + 8 18 + 9 10 + 10 7 + 11 8 + 12 12 + 13 17 + 14 19 + 15 24 + 16 18 + 17 15 + 18 10 + 19 9 + "> x <- read.table(textConnection(x), header=TRUE) > # find the 'runs' of thresholds > threshold <- rle(x$value >= 17) > # get offset of each group > offset <- cumsum(c(1,threshold$lengths)) > # create sequence numbers for the groups > seqNum <- cumsum(threshold$value) > # compute the indices of where the thresholds are > x$group <- NA # initialize the vector > # change the values for the groups > for (i in which(threshold$value)){+ x$group[seq(offset[i], length=threshold$length[i])] <- seqNum[i] + }> # extract max for each group > xtime value group 1 1 5 NA 2 2 10 NA 3 3 7 NA 4 4 10 NA 5 5 19 1 6 6 21 1 7 7 20 1 8 8 18 1 9 9 10 NA 10 10 7 NA 11 11 8 NA 12 12 12 NA 13 13 17 2 14 14 19 2 15 15 24 2 16 16 18 2 17 17 15 NA 18 18 10 NA 19 19 9 NA> tapply(x$value, x$group, max)1 2 21 24 On 5/21/07, Robert <jsubei@yahoo.fr> wrote:> > Hi dear R users, > > I'm a R beginner and I have a basic question about sequential treatments > of lists. > > I have a time based (i.e. events are consecutive) list of values of a > biological property. > > Like : > > time value > 1 5 > 2 10 > 3 7 > 4 10 > 5 19 > 6 21 > 7 20 > 8 18 > 9 10 > 10 7 > 11 8 > 12 12 > 13 17 > 14 19 > 15 24 > 16 18 > 17 15 > 18 10 > 19 9 > [...] > > > And I have to define a threshold and to attach each event to his group, > i.e. values upper the threshold. > > Like, for a threshold value of 17 > > time value group > 1 5 NA > 2 10 NA > 3 7 NA > 4 10 NA > 5 19 1 > 6 21 1 > 7 20 1 > 8 18 1 > 9 10 NA > 10 7 NA > 11 8 NA > 12 12 NA > 13 17 2 > 14 19 2 > 15 24 2 > 16 18 2 > 17 15 NA > 18 10 NA > 19 9 NA > [...] > > > The only solution that I have found is to do a sequentially read and write > : > for(i in 1:length(my_events_list)) > { > [...] > } > > But I very slow. Do you have another ideas ? > > And after I need to extract maximum values for each group > Like : > group max_value > 1 21 > 2 24 > [...] > > and for each event which is part of a group to know if is't a ascending > phase or no. > > > Yes, lot of questions !! Sorry, but I think that the solution may be > unique. > > In advance, thank you a lot > > regards > > JS > > > > > > > > > _____________________________________________________________________________ > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]