[R] Help with loop
Rui Barradas
ruipbarradas at sapo.pt
Wed Nov 21 18:42:22 CET 2012
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.
More information about the R-help
mailing list