Hi, I have used R for some time, but managed to avoid writing loops. But this time I am afraid there is no way around it. I have a dataframe with time and salinity (see below). I would like to extract the time intervals where salinity changes by less than 0.05. So using the values below this would mean that a subset was made from 2003-07-19 to 2003-07-24, where the change in salinity exceeded 0.05, and then stating again from 2003-07-24. In other words, I want to make subset of the time intervals where the one variable stays stabile within a certain set range.> head (D, 10)Salt time 1 35.65114 2003-07-19 2 35.64226 2003-07-20 3 35.62411 2003-07-21 4 35.62473 2003-07-22 5 35.65893 2003-07-23 6 35.70140 2003-07-24 7 35.62157 2003-07-25 8 35.64122 2003-07-26 9 35.63515 2003-07-27 10 35.63798 2003-07-28 Is this possible? Can some please help with how I need to write the loop to achieve this? Many thanks, Helen -- View this message in context: http://r.789695.n4.nabble.com/Help-with-loop-tp4650313.html Sent from the R help mailing list archive at Nabble.com.
Hello, If I understand it well, this might avoid a loop. dat <- read.table(text=" Salt time 1 35.65114 2003-07-19 2 35.64226 2003-07-20 3 35.62411 2003-07-21 4 35.62473 2003-07-22 5 35.65893 2003-07-23 6 35.70140 2003-07-24 7 35.62157 2003-07-25 8 35.64122 2003-07-26 9 35.63515 2003-07-27 10 35.63798 2003-07-28 ", header = TRUE, stringsAsFactors = FALSE) dat$time <- as.Date(dat$time) change <- cumsum(c(FALSE, abs(diff(dat$Salt)) > 0.05)) split(dat, change) Hope this helps, Rui Barradas Em 21-11-2012 16:39, Hefri escreveu:> Hi, > > I have used R for some time, but managed to avoid writing loops. But this > time I am afraid there is no way around it. > > I have a dataframe with time and salinity (see below). I would like to > extract the time intervals where salinity changes by less than 0.05. So > using the values below this would mean that a subset was made from > 2003-07-19 to 2003-07-24, where the change in salinity exceeded 0.05, and > then stating again from 2003-07-24. In other words, I want to make subset of > the time intervals where the one variable stays stabile within a certain set > range. > >> head (D, 10) > Salt time > 1 35.65114 2003-07-19 > 2 35.64226 2003-07-20 > 3 35.62411 2003-07-21 > 4 35.62473 2003-07-22 > 5 35.65893 2003-07-23 > 6 35.70140 2003-07-24 > 7 35.62157 2003-07-25 > 8 35.64122 2003-07-26 > 9 35.63515 2003-07-27 > 10 35.63798 2003-07-28 > > Is this possible? Can some please help with how I need to write the loop to > achieve this? > > Many thanks, Helen > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with-loop-tp4650313.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
Hello, I'm glad it helped. You should have kept this in the list, the odds of getting more answers are bigger. As for your problem with NAs, what do you want to do with them? Should they count as within range? Rui Barradas Em 22-11-2012 10:18, Helene Frigstad escreveu:> Hi, > yes, that is a very elegant solution. Thank you very much! > Do you have any suggestions on how to deal with NA in such a situation? For example, if I have one missing value in the dataset, then the function will only work until it reaches the NA, and then the rest will be NA as well. >> head (D, 11) Salt time310 35.63511 2004-07-17311 35.62334 2004-07-18312 35.63498 2004-07-19313 35.64032 2004-07-20314 NA 2004-07-21315 35.66930 2004-07-22316 35.65394 2004-07-23317 35.64702 2004-07-24318 35.63810 2004-07-25319 35.63190 2004-07-26320 35.66033 2004-07-27 >> (change <- cumsum(c(FALSE, abs(diff(D$Salt)) > 0.05))) [1] 0 0 0 0 NA NA NA NA NA NA NA >> (split(D, change))$`0` Salt time310 35.63511 2004-07-17311 35.62334 2004-07-18312 35.63498 2004-07-19313 35.64032 2004-07-20 > Many thanks, Helene >
Hi again, I was thinking that the interval would stop when there was a NA, ignore subsequent NAs and only start a new interval when there was a new value. So in the example below, the interval would stop at row 320, and a new interval started at row 334.> head (D, 20)Salt time 319 35.63190 2004-07-26 320 35.66033 2004-07-27 321 NA 2004-07-28 322 NA 2004-07-29 323 NA 2004-07-30 324 NA 2004-07-31 325 NA 2004-08-01 326 NA 2004-08-02 327 NA 2004-08-03 328 NA 2004-08-04 329 NA 2004-08-05 330 NA 2004-08-06 331 NA 2004-08-07 332 NA 2004-08-08 333 NA 2004-08-09 334 35.57781 2004-08-10 335 35.59829 2004-08-11 Many thanks, Helene -- View this message in context: http://r.789695.n4.nabble.com/Help-with-loop-tp4650313p4650432.html Sent from the R help mailing list archive at Nabble.com.