Quantcast

Imposing more than one condition to if

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Imposing more than one condition to if

Santiago Guallar
Hi,
 
I have a dataset which contains several time records for a number of days, plus a variable (light) that allows to determine night time (lihgt= 0) and daytime (light> 0). I need to obtain get dusk time and dawn time for each day and place them in two columns.

This is the starting point (d):
day time light
1     1       20
1     12     10
1     11     6
1     9       0
1     6       0
1     12     0
...
30     8     0
30     3     0
30     8     0
30     3     0
30     8     8
30     9     20


And this what I want to get:
day time light dusk dawn
1     1      20     11     10
1     12    10     11     10
1     11     6      11     10
1     9       0      11     10
1     6       0      11     10
1     12     0      11     10
...
30     8     0       9     5
30     3     0       9     5
30     8     0       9     5
30     3     0       9     5
30     8     8       9     5
30     9     20     9     5

This is the code for data frame d:
day= rep(1:30, each=10)
n= length(dia); x= c(1:24)
time= sample(x, 300, replace= T)
light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
d=data.frame(day,time,light)

I'd need to impose a double condition like the next but if does not take more than one:
attach(d)
for (i in 1: n){
if (light[i-1]>2 & light[i]<2){
d$dusk<- time[i-1]
}
if (light[i-1]<2 & light[i]>2){
d$dawn<- time[i]
}
}
detach(d)
d

Thank you for your help
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

John Kane
No idea of how to do what you want but your data set is not working.
I think that you want

 x= c(1:24)
day= rep(1:30, each=10)
time= sample(x, 300, replace= T)
light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
d=data.frame(day,time,light)
n= length(day)

John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Sun, 15 Jul 2012 09:32:33 -0700 (PDT)
> To: [hidden email]
> Subject: [R] Imposing more than one condition to if
>
> Hi,
>
> I have a dataset which contains several time records for a number of
> days, plus a variable (light) that allows to determine night time (lihgt=
> 0) and daytime (light> 0). I need to obtain get dusk time and dawn time
> for each day and place them in two columns.
>
> This is the starting point (d):
> day time light
> 1     1       20
> 1     12     10
> 1     11     6
> 1     9       0
> 1     6       0
> 1     12     0
> ...
> 30     8     0
> 30     3     0
> 30     8     0
> 30     3     0
> 30     8     8
> 30     9     20
>
>
> And this what I want to get:
> day time light dusk dawn
> 1     1      20     11     10
> 1     12    10     11     10
> 1     11     6      11     10
> 1     9       0      11     10
> 1     6       0      11     10
> 1     12     0      11     10
> ...
> 30     8     0       9     5
> 30     3     0       9     5
> 30     8     0       9     5
> 30     3     0       9     5
> 30     8     8       9     5
> 30     9     20     9     5
>
> This is the code for data frame d:
> day= rep(1:30, each=10)
> n= length(dia); x= c(1:24)
> time= sample(x, 300, replace= T)
> light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
> d=data.frame(day,time,light)
>
> I'd need to impose a double condition like the next but if does not take
> more than one:
> attach(d)
> for (i in 1: n){
> if (light[i-1]>2 & light[i]<2){
> d$dusk<- time[i-1]
> }
> if (light[i-1]<2 & light[i]>2){
> d$dawn<- time[i]
> }
> }
> detach(d)
> d
>
> Thank you for your help
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Rui Barradas
In reply to this post by Santiago Guallar
Hello,

There are obvious bugs in your code, you are testing for light > 2 or
ligth < 2 but this would mean that dusk and dawn are undetermined for
light == 2 and that they happen at light == 1.

Without loops or compound logical conditions:


f <- function(x){
        x$dawn <- x$time[ which.min(x$light) ]
        x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
        x
}

do.call(rbind, by(d, d$day, f))

Hope this helps,

Rui Barradas

Em 15-07-2012 17:32, Santiago Guallar escreveu:

> Hi,
>
> I have a dataset which contains several time records for a number of days, plus a variable (light) that allows to determine night time (lihgt= 0) and daytime (light> 0). I need to obtain get dusk time and dawn time for each day and place them in two columns.
>
> This is the starting point (d):
> day time light
> 1     1       20
> 1     12     10
> 1     11     6
> 1     9       0
> 1     6       0
> 1     12     0
> ...
> 30     8     0
> 30     3     0
> 30     8     0
> 30     3     0
> 30     8     8
> 30     9     20
>
>
> And this what I want to get:
> day time light dusk dawn
> 1     1      20     11     10
> 1     12    10     11     10
> 1     11     6      11     10
> 1     9       0      11     10
> 1     6       0      11     10
> 1     12     0      11     10
> ...
> 30     8     0       9     5
> 30     3     0       9     5
> 30     8     0       9     5
> 30     3     0       9     5
> 30     8     8       9     5
> 30     9     20     9     5
>
> This is the code for data frame d:
> day= rep(1:30, each=10)
> n= length(dia); x= c(1:24)
> time= sample(x, 300, replace= T)
> light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
> d=data.frame(day,time,light)
>
> I'd need to impose a double condition like the next but if does not take more than one:
> attach(d)
> for (i in 1: n){
> if (light[i-1]>2 & light[i]<2){
> d$dusk<- time[i-1]
> }
> if (light[i-1]<2 & light[i]>2){
> d$dawn<- time[i]
> }
> }
> detach(d)
> d
>
> Thank you for your help
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

jholtman
In reply to this post by Santiago Guallar
try this:

> set.seed(1)
> day= rep(1:30, each=10)
> n= length(day); x= c(1:24)
> time= sample(x, 300, replace= T)
> light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
> d=data.frame(day,time,light)
>
> # create a dawn/dusk column to mark where it happens
> d$dawn <- c(FALSE, (head(d$light, -1) < 2) & (tail(d$light, -1) > 2))
> d$dusk <- c((head(d$light, -1) > 2) & (tail(d$light, -1) < 2), FALSE)
>
> # now split and recombine to get the values for the day
> result <- do.call(rbind, lapply(split(d, d$day), function(.day){
+     # create new dataframe with the values
+     cbind(.day[, c('day', 'time', 'light')]
+         , dawn = .day$time[.day$dawn]
+         , dusk = .day$time[.day$dusk]
+         )
+ }))
>
>
>
> result
       day time light dawn dusk
1.1      1    7    20   16   14
1.2      1    9    10   16   14
1.3      1   14     6   16   14
1.4      1   22     0   16   14
1.5      1    5     0   16   14
1.6      1   22     0   16   14
1.7      1   23     0   16   14
1.8      1   16     0   16   14
1.9      1   16     8   16   14
1.10     1    2    20   16   14
2.11     2    5    20   10   17
2.12     2    5    10   10   17
2.13     2   17     6   10   17
2.14     2   10     0   10   17
2.15     2   19     0   10   17
2.16     2   12     0   10   17
2.17     2   18     0   10   17
2.18     2   24     0   10   17
2.19     2   10     8   10   17
2.20     2   19    20   10   17
3.21     3   23    20   21   16
3.22     3    6    10   21   16
3.23     3   16     6   21   16
3.24     3    4     0   21   16
3.25     3    7     0   21   16
3.26     3   10     0   21   16
3.27     3    1     0   21   16
3.28     3   10     0   21   16
3.29     3   21     8   21   16
3.30     3    9    20   21   16
4.31     4   12    20   18   12
4.32     4   15    10   18   12
4.33     4   12     6   18   12
4.34     4    5     0   18   12
4.35     4   20     0   18   12
4.36     4   17     0   18   12
4.37     4   20     0   18   12
4.38     4    3     0   18   12
4.39     4   18     8   18   12
4.40     4   10    20   18   12




On Sun, Jul 15, 2012 at 12:32 PM, Santiago Guallar <[hidden email]> wrote:

> Hi,
>
> I have a dataset which contains several time records for a number of days, plus a variable (light) that allows to determine night time (lihgt= 0) and daytime (light> 0). I need to obtain get dusk time and dawn time for each day and place them in two columns.
>
> This is the starting point (d):
> day time light
> 1     1       20
> 1     12     10
> 1     11     6
> 1     9       0
> 1     6       0
> 1     12     0
> ...
> 30     8     0
> 30     3     0
> 30     8     0
> 30     3     0
> 30     8     8
> 30     9     20
>
>
> And this what I want to get:
> day time light dusk dawn
> 1     1      20     11     10
> 1     12    10     11     10
> 1     11     6      11     10
> 1     9       0      11     10
> 1     6       0      11     10
> 1     12     0      11     10
> ...
> 30     8     0       9     5
> 30     3     0       9     5
> 30     8     0       9     5
> 30     3     0       9     5
> 30     8     8       9     5
> 30     9     20     9     5
>
> This is the code for data frame d:
> day= rep(1:30, each=10)
> n= length(dia); x= c(1:24)
> time= sample(x, 300, replace= T)
> light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
> d=data.frame(day,time,light)
>
> I'd need to impose a double condition like the next but if does not take more than one:
> attach(d)
> for (i in 1: n){
> if (light[i-1]>2 & light[i]<2){
> d$dusk<- time[i-1]
> }
> if (light[i-1]<2 & light[i]>2){
> d$dawn<- time[i]
> }
> }
> detach(d)
> d
>
> Thank you for your help
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Rui Barradas
In reply to this post by Rui Barradas
Hello,

My code couldn't find the right input columns because your real data has
different names, it could only find the example dataset's names.

And there's another problem, my code would give correct answers with a
limted number of possible inputs and fail with real data.

Corrected:


f <- function(x){
     zrle <- rle(x$lig == 0)
     if(zrle$values[1]){
         idusk <- sum(zrle$lengths[1:2]) + 1
         idawn <- zrle$lengths[1] + 1
         x$dusk <- x$dtime[ idusk ]
         x$dawn <- x$dtime[ idawn ]
     }else{
         idusk <- zrle$lengths[1] + 1
         x$dusk <- x$dtime[ idusk ]
         x$dawn <- NA
     }
     x
}

do.call(rbind, by(d, d$date, f))


One more thing, you are reading your dataset into a data.frame
forgetting that character strings become factors. Try str(d) to see it.
('d' is the data.frame.) You could/should coerce the date/time values to
appropriate classes, something like


d$time <- as.character(d$time)
d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
d$date <- as.character(d$date)
d$date <- as.Date(d$date, format="%d/%m/%y")


Hope this helps,

Rui Barradas

Em 17-07-2012 07:14, Santiago Guallar escreveu:

> Thank you Rui,
>
> When applied to my original data, your code goes through although it
> does not produce the correct results: for dusk gives the first time
> value of next day, for dawn it gives NA. It seems that the function f
> doesn't find the right input columns.
> A, Ilso had to push up the memory size.
> Attached a file (containing just 3000 of the original c. 45000 rows)
> after dput().
>
> Santi
>
>
>     ------------------------------------------------------------------------
>     *From:* Rui Barradas <[hidden email]>
>     *To:* Santiago Guallar <[hidden email]>
>     *Cc:* [hidden email]
>     *Sent:* Sunday, July 15, 2012 7:21 PM
>     *Subject:* Re: [R] Imposing more than one condition to if
>
>     Hello,
>
>     There are obvious bugs in your code, you are testing for light > 2 or
>     ligth < 2 but this would mean that dusk and dawn are undetermined for
>     light == 2 and that they happen at light == 1.
>
>     Without loops or compound logical conditions:
>
>
>     f <- function(x){
>          x$dawn <- x$time[ which.min(x$light) ]
>          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>          x
>     }
>
>     do.call(rbind, by(d, d$day, f))
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Em 15-07-2012 17:32, Santiago Guallar escreveu:
>      > Hi,
>      >
>      > I have a dataset which contains several time records for a number
>     of days, plus a variable (light) that allows to determine night time
>     (lihgt= 0) and daytime (light> 0). I need to obtain get dusk time
>     and dawn time for each day and place them in two columns.
>      >
>      > This is the starting point (d):
>      > day time light
>      > 1    1      20
>      > 1    12    10
>      > 1    11    6
>      > 1    9      0
>      > 1    6      0
>      > 1    12    0
>      > ...
>      > 30    8    0
>      > 30    3    0
>      > 30    8    0
>      > 30    3    0
>      > 30    8    8
>      > 30    9    20
>      >
>      >
>      > And this what I want to get:
>      > day time light dusk dawn
>      > 1    1      20    11    10
>      > 1    12    10    11    10
>      > 1    11    6      11    10
>      > 1    9      0      11   10
>      > 1    6      0      11    10
>      > 1    12    0      11    10
>      > ...
>      > 30    8    0      9    5
>      > 30    3    0      9    5
>      > 30    8    0      9    5
>      > 30    3    0      9    5
>      > 30    8    8      9    5
>      > 30    9    20    9    5
>      >
>      > This is the code for data frame d:
>      > day= rep(1:30, each=10)
>      > n= length(dia); x= c(1:24)
>      > time= sample(x, 300, replace= T)
>      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>      > d=data.frame(day,time,light)
>      >
>      > I'd need to impose a double condition like the next but if does
>     not take more than one:
>      > attach(d)
>      > for (i in 1: n){
>      > if (light[i-1]>2 & light[i]<2){
>      > d$dusk<- time[i-1]
>      > }
>      > if (light[i-1]<2 & light[i]>2){
>      > d$dawn<- time[i]
>      > }
>      > }
>      > detach(d)
>      > d
>      >
>      > Thank you for your help
>      >     [[alternative HTML version deleted]]
>      >
>      >
>      >
>      > ______________________________________________
>      > [hidden email] <mailto:[hidden email]> 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.
>      >
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Santiago Guallar
Thank for your time, Rui.
 
Now,  I get this error message:
Error en rbind(deparse.level, ...) :
  numbers of columns of arguments do not match

Apparently, some columns have missing values and  rbind doesn't work. I tried:
require(plyr)
do.call(rbind.fill, by(z, z$date, f))
 
Then the code runs through but dusk the variable dusk is missing and dawn is filled with NA.
 
Just in case the problem simply lies in a name, this is your code after I changed the object names (basically 'x' and 'd' by 'z') to adapt them to the names of my dataset:
 
f <- function(z){
    zrle <- rle(z$lig == 0)
    if(zrle$values[1]){
        idusk <- sum(zrle$lengths[1:2]) + 1
        idawn <- zrle$lengths[1] + 1
        z$dusk <- z$dtime[ idusk ]
        z$dawn <- z$dtime[ idawn ]
    }else{
        idusk <- zrle$lengths[1] + 1
        z$dusk <- z$dtime[ idusk ]
        z$dawn <- NA
    }
    z
}

do.call(rbind, by(z, z$date, f))
 
Again, I attached a dput() with the object z which contains my dataset.
 
Santi

From: Rui Barradas <[hidden email]>

>To: Santiago Guallar <[hidden email]>
>Cc: [hidden email]
>Sent: Tuesday, July 17, 2012 11:52 AM
>Subject: Re: [R] Imposing more than one condition to if
>
>Hello,
>
>My code couldn't find the right input columns because your real data has
>different names, it could only find the example dataset's names.
>
>And there's another problem, my code would give correct answers with a
>limted number of possible inputs and fail with real data.
>
>Corrected:
>
>
>f <- function(x){
>    zrle <- rle(x$lig == 0)
>    if(zrle$values[1]){
>        idusk <- sum(zrle$lengths[1:2]) + 1
>        idawn <- zrle$lengths[1] + 1
>        x$dusk <- x$dtime[ idusk ]
>        x$dawn <- x$dtime[ idawn ]
>    }else{
>        idusk <- zrle$lengths[1] + 1
>        x$dusk <- x$dtime[ idusk ]
>        x$dawn <- NA
>    }
>    x
>}
>
>do.call(rbind, by(d, d$date, f))
>
>
>One more thing, you are reading your dataset into a data.frame
>forgetting that character strings become factors. Try str(d) to see it.
>('d' is the data.frame.) You could/should coerce the date/time values to
>appropriate classes, something like
>
>
>d$time <- as.character(d$time)
>d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
>d$date <- as.character(d$date)
>d$date <- as.Date(d$date, format="%d/%m/%y")
>
>
>Hope this helps,
>
>Rui Barradas
>
>Em 17-07-2012 07:14, Santiago Guallar escreveu:
>> Thank you Rui,
>>
>> When applied to my original data, your code goes through although it
>> does not produce the correct results: for dusk gives the first time
>> value of next day, for dawn it gives NA. It seems that the function f
>> doesn't find the right input columns.
>> A, Ilso had to push up the memory size.
>> Attached a file (containing just 3000 of the original c. 45000 rows)
>> after dput().
>>
>> Santi
>>
>>
>>    ------------------------------------------------------------------------
>>    *From:* Rui Barradas <[hidden email]>
>>    *To:* Santiago Guallar <[hidden email]>
>>    *Cc:* [hidden email]
>>    *Sent:* Sunday, July 15, 2012 7:21 PM
>>    *Subject:* Re: [R] Imposing more than one condition to if
>>
>>    Hello,
>>
>>    There are obvious bugs in your code, you are testing for light > 2 or
>>    ligth < 2 but this would mean that dusk and dawn are undetermined for
>>    light == 2 and that they happen at light == 1.
>>
>>    Without loops or compound logical conditions:
>>
>>
>>    f <- function(x){
>>          x$dawn <- x$time[ which.min(x$light) ]
>>          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>>          x
>>    }
>>
>>    do.call(rbind, by(d, d$day, f))
>>
>>    Hope this helps,
>>
>>    Rui Barradas
>>
>>    Em 15-07-2012 17:32, Santiago Guallar escreveu:
>>      > Hi,
>>      >
>>      > I have a dataset which contains several time records for a number
>>    of days, plus a variable (light) that allows to determine night time
>>    (lihgt= 0) and daytime (light> 0). I need to obtain get dusk time
>>    and dawn time for each day and place them in two columns.
>>      >
>>      > This is the starting point (d):
>>      > day time light
>>      > 1    1      20
>>      > 1    12    10
>>      > 1    11    6
>>      > 1    9      0
>>      > 1    6      0
>>      > 1    12    0
>>      > ...
>>      > 30    8    0
>>      > 30    3    0
>>      > 30    8    0
>>      > 30    3    0
>>      > 30    8    8
>>      > 30    9    20
>>      >
>>      >
>>      > And this what I want to get:
>>      > day time light dusk dawn
>>      > 1    1      20    11    10
>>      > 1    12    10    11    10
>>      > 1    11    6      11    10
>>      > 1    9      0      11  10
>>      > 1    6      0      11    10
>>      > 1    12    0      11    10
>>      > ...
>>      > 30    8    0      9    5
>>      > 30    3    0      9    5
>>      > 30    8    0      9    5
>>      > 30    3    0      9    5
>>      > 30    8    8      9    5
>>      > 30    9    20    9    5
>>      >
>>      > This is the code for data frame d:
>>      > day= rep(1:30, each=10)
>>      > n= length(dia); x= c(1:24)
>>      > time= sample(x, 300, replace= T)
>>      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>>      > d=data.frame(day,time,light)
>>      >
>>      > I'd need to impose a double condition like the next but if does
>>    not take more than one:
>>      > attach(d)
>>      > for (i in 1: n){
>>      > if (light[i-1]>2 & light[i]<2){
>>      > d$dusk<- time[i-1]
>>      > }
>>      > if (light[i-1]<2 & light[i]>2){
>>      > d$dawn<- time[i]
>>      > }
>>      > }
>>      > detach(d)
>>      > d
>>      >
>>      > Thank you for your help
>>      >    [[alternative HTML version deleted]]
>>      >
>>      >
>>      >
>>      > ______________________________________________
>>      > [hidden email] <mailto:[hidden email]> 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.
>>      >
>>
>>
>>
>
>
>
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Santiago Guallar
Thank for your time, Rui.
 
Now, I get this error message:
Error en rbind(deparse.level, ...) :
numbers of columns of arguments do not match
 
Apparently, some columns have missing values and rbind doesn't work. I tried:
require(plyr)
do.call(rbind.fill, by(z, z$date, f))
 
Then the code runs through but dusk the variable dusk is missing and dawn is filled with NA.
Just in case the problem simply lies in a name, this is your code after I changed the object names (basically 'x' and 'd' by 'z') to adapt them to the names of my dataset:
f <- function(z){
zrle <- rle(z$lig == 0)
if(zrle$values[1]){
idusk <- sum(zrle$lengths[1:2]) + 1
idawn <- zrle$lengths[1] + 1
z$dusk <- z$dtime[ idusk ]
z$dawn <- z$dtime[ idawn ]
}else{
idusk <- zrle$lengths[1] + 1
z$dusk <- z$dtime[ idusk ]
z$dawn <- NA
}
z
}

do.call(rbind, by(z, z$date, f))
 
Again, I attached a dput() with the object z which contains my dataset.
 
Santi

From: Rui Barradas <[hidden email]>

>To: Santiago Guallar <[hidden email]>
>Cc: [hidden email]
>Sent: Tuesday, July 17, 2012 11:52 AM
>Subject: Re: [R] Imposing more than one condition to if
>
>
>>Hello,
>>
>>My code couldn't find the right input columns because your real data has
>>different names, it could only find the example dataset's names.
>>
>>And there's another problem, my code would give correct answers with a
>>limted number of possible inputs and fail with real data.
>>
>>Corrected:
>>
>>
>>f <- function(x){
>>    zrle <- rle(x$lig == 0)
>>    if(zrle$values[1]){
>>        idusk <- sum(zrle$lengths[1:2]) + 1
>>        idawn <- zrle$lengths[1] + 1
>>        x$dusk <- x$dtime[ idusk ]
>>        x$dawn <- x$dtime[ idawn ]
>>    }else{
>>        idusk <- zrle$lengths[1] + 1
>>        x$dusk <- x$dtime[ idusk ]
>>        x$dawn <- NA
>>    }
>>    x
>>}
>>
>>do.call(rbind, by(d, d$date, f))
>>
>>
>>One more thing, you are reading your dataset into a data.frame
>>forgetting that character strings become factors. Try str(d) to see it.
>>('d' is the data.frame.) You could/should coerce the date/time values to
>>appropriate classes, something like
>>
>>
>>d$time <- as.character(d$time)
>>d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
>>d$date <- as.character(d$date)
>>d$date <- as.Date(d$date, format="%d/%m/%y")
>>
>>
>>Hope this helps,
>>
>>Rui Barradas
>>
>>Em 17-07-2012 07:14, Santiago Guallar escreveu:
>>> Thank you Rui,
>>>
>>> When applied to my original data, your code goes through although it
>>> does not produce the correct results: for dusk gives the first time
>>> value of next day, for dawn it gives NA. It seems that the function f
>>> doesn't find the right input columns.
>>> A, Ilso had to push up the memory size.
>>> Attached a file (containing just 3000 of the original c. 45000 rows)
>>> after dput().
>>>
>>> Santi
>>>
>>>
>>>    ------------------------------------------------------------------------
>>>    *From:* Rui Barradas <[hidden email]>
>>>    *To:* Santiago Guallar <[hidden email]>
>>>    *Cc:* [hidden email]
>>>    *Sent:* Sunday, July 15, 2012 7:21 PM
>>>    *Subject:* Re: [R] Imposing more than one condition to if
>>>
>>>    Hello,
>>>
>>>    There are obvious bugs in your code, you are testing for light > 2 or
>>>    ligth < 2 but this would mean that dusk and dawn are undetermined for
>>>    light == 2 and that they happen at light == 1.
>>>
>>>    Without loops or compound logical conditions:
>>>
>>>
>>>    f <- function(x){
>>>          x$dawn <- x$time[ which.min(x$light) ]
>>>          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>>>          x
>>>    }
>>>
>>>    do.call(rbind, by(d, d$day, f))
>>>
>>>    Hope this helps,
>>>
>>>    Rui Barradas
>>>
>>>    Em 15-07-2012 17:32, Santiago Guallar escreveu:
>>>      > Hi,
>>>      >
>>>      > I have a dataset which contains several time records for a number
>>>    of days, plus a variable (light) that allows to determine night time
>>>    (lihgt= 0) and daytime (light> 0). I need to obtain get dusk time
>>>    and dawn time for each day and place them in two columns.
>>>      >
>>>      > This is the starting point (d):
>>>      > day time light
>>>      > 1    1      20
>>>      > 1    12    10
>>>      > 1    11    6
>>>      > 1    9      0
>>>      > 1    6      0
>>>      > 1    12    0
>>>      > ...
>>>      > 30    8    0
>>>      > 30    3    0
>>>      > 30    8    0
>>>      > 30    3    0
>>>      > 30    8    8
>>>      > 30    9    20
>>>      >
>>>      >
>>>      > And this what I want to get:
>>>      > day time light dusk dawn
>>>      > 1    1      20    11    10
>>>      > 1    12    10    11    10
>>>      > 1    11    6      11    10
>>>      > 1    9      0      11  10
>>>      > 1    6      0      11    10
>>>      > 1    12    0      11    10
>>>      > ...
>>>      > 30    8    0      9    5
>>>      > 30    3    0      9    5
>>>      > 30    8    0      9    5
>>>      > 30    3    0      9    5
>>>      > 30    8    8      9    5
>>>      > 30    9    20    9    5
>>>      >
>>>      > This is the code for data frame d:
>>>      > day= rep(1:30, each=10)
>>>      > n= length(dia); x= c(1:24)
>>>      > time= sample(x, 300, replace= T)
>>>      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>>>      > d=data.frame(day,time,light)
>>>      >
>>>      > I'd need to impose a double condition like the next but if does
>>>    not take more than one:
>>>      > attach(d)
>>>      > for (i in 1: n){
>>>      > if (light[i-1]>2 & light[i]<2){
>>>      > d$dusk<- time[i-1]
>>>      > }
>>>      > if (light[i-1]<2 & light[i]>2){
>>>      > d$dawn<- time[i]
>>>      > }
>>>      > }
>>>      > detach(d)
>>>      > d
>>>      >
>>>      > Thank you for your help
>>>      >    [[alternative HTML version deleted]]
>>>      >
>>>      >
>>>      >
>>>      > ______________________________________________
>>>      > [hidden email] <mailto:[hidden email]> 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.
>>>      >
>>>
>>>
>>>
>>
>>
>>
>>
>
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Rui Barradas
hELLO,

There was no nedd to change the names of the variables inside the
fucntion. What was going on s that in this new file column 'dtime'
doesn't exist. Since column 'clock' exists in all files, I've changed
the function once again, making it more general.

Note that there is an argument 'colTime' with a default value. In the
function's use below I call it with and without that argument.


f <- function(x, colTime="clock"){
     zrle <- rle(x$lig == 0)
     if(zrle$values[1]){
         idusk <- sum(zrle$lengths[1:2]) + 1
         idawn <- zrle$lengths[1] + 1
         x$dusk <- x[ idusk , colTime ]
         x$dawn <- x[ idawn , colTime ]
     }else{
         idusk <- zrle$lengths[1] + 1
         x$dusk <- x[ idusk , colTime ]
         x$dawn <- NA
     }
     x
}

str(d)
#d$date <- as.Date(d$date, format="%d/%m%/y")

#library(chron)
#tm <- times(as.character(d$clock))
#d$clock <- tm

# See what will happen. This call uses the default 'colTime'
bb <- by(d, d$date, f)
for(i in seq_along(bb)) print(head(bb[[i]], 1))

# call and rbind. This call uses explicit arg 'colTime'
do.call(rbind, by(d, d$date, f, colTime="clock"))

# Alternatively, it could be, because 'bb' is already created,
do.call(rbind, bb)


In the code above, I use an optional conversion to date and time
classes; as.Date is part of base R, but class times needs package chron.
It's a good idea to convert these variables, you can later use, say,
arithmetics on dates and times, such as differences.

Hope this helps,

Rui Barradas

Em 17-07-2012 19:53, Santiago Guallar escreveu:

> Thank for your time, Rui.
> Now, I get this error message:
> Error en rbind(deparse.level, ...) :
> numbers of columns of arguments do not match
> Apparently, some columns have missing values and rbind doesn't work. I
> tried:
> require(plyr)
> do.call(rbind.fill, by(z, z$date, f))
> Then the code runs through but dusk the variable dusk is missing and
> dawn is filled with NA.
> Just in case the problem simply lies in a name, this is your code after
> I changed the object names (basically 'x' and 'd' by 'z') to adapt them
> to the names of my dataset:
> f <- function(z){
> zrle <- rle(z$lig == 0)
> if(zrle$values[1]){
> idusk <- sum(zrle$lengths[1:2]) + 1
> idawn <- zrle$lengths[1] + 1
> z$dusk <- z$dtime[ idusk ]
> z$dawn <- z$dtime[ idawn ]
> }else{
> idusk <- zrle$lengths[1] + 1
> z$dusk <- z$dtime[ idusk ]
> z$dawn <- NA
> }
> z
> }
>
> do.call(rbind, by(z, z$date, f))
> Again, I attached a dput() with the object z which contains my dataset.
> Santi
>
>     *From:* Rui Barradas <[hidden email]>
>     *To:* Santiago Guallar <[hidden email]>
>     *Cc:* [hidden email]
>     *Sent:* Tuesday, July 17, 2012 11:52 AM
>     *Subject:* Re: [R] Imposing more than one condition to if
>
>
>         Hello,
>
>         My code couldn't find the right input columns because your real
>         data has
>         different names, it could only find the example dataset's names.
>
>         And there's another problem, my code would give correct answers
>         with a
>         limted number of possible inputs and fail with real data.
>
>         Corrected:
>
>
>         f <- function(x){
>              zrle <- rle(x$lig == 0)
>              if(zrle$values[1]){
>                  idusk <- sum(zrle$lengths[1:2]) + 1
>                  idawn <- zrle$lengths[1] + 1
>                  x$dusk <- x$dtime[ idusk ]
>                  x$dawn <- x$dtime[ idawn ]
>              }else{
>                  idusk <- zrle$lengths[1] + 1
>                  x$dusk <- x$dtime[ idusk ]
>                  x$dawn <- NA
>              }
>              x
>         }
>
>         do.call(rbind, by(d, d$date, f))
>
>
>         One more thing, you are reading your dataset into a data.frame
>         forgetting that character strings become factors. Try str(d) to
>         see it.
>         ('d' is the data.frame.) You could/should coerce the date/time
>         values to
>         appropriate classes, something like
>
>
>         d$time <- as.character(d$time)
>         d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
>         d$date <- as.character(d$date)
>         d$date <- as.Date(d$date, format="%d/%m/%y")
>
>
>         Hope this helps,
>
>         Rui Barradas
>
>         Em 17-07-2012 07:14, Santiago Guallar escreveu:
>          > Thank you Rui,
>          >
>          > When applied to my original data, your code goes through
>         although it
>          > does not produce the correct results: for dusk gives the
>         first time
>          > value of next day, for dawn it gives NA. It seems that the
>         function f
>          > doesn't find the right input columns.
>          > A, Ilso had to push up the memory size.
>          > Attached a file (containing just 3000 of the original c.
>         45000 rows)
>          > after dput().
>          >
>          > Santi
>          >
>          >
>          >
>         ------------------------------------------------------------------------
>          >    *From:* Rui Barradas <[hidden email]
>         <mailto:[hidden email]>>
>          >    *To:* Santiago Guallar <[hidden email]
>         <mailto:[hidden email]>>
>          >    *Cc:* [hidden email] <mailto:[hidden email]>
>          >    *Sent:* Sunday, July 15, 2012 7:21 PM
>          >    *Subject:* Re: [R] Imposing more than one condition to if
>          >
>          > Hello,
>          >
>          >    There are obvious bugs in your code, you are testing for
>         light > 2 or
>          >    ligth < 2 but this would mean that dusk and dawn are
>         undetermined for
>          >    light == 2 and that they happen at light == 1.
>          >
>          >    Without loops or compound logical conditions:
>          >
>          >
>          >    f <- function(x){
>          >          x$dawn <- x$time[ which.min(x$light) ]
>          >          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>          >          x
>          >    }
>          >
>          >    do.call(rbind, by(d, d$day, f))
>          >
>          >    Hope this helps,
>          >
>          >    Rui Barradas
>          >
>          >    Em 15-07-2012 17:32, Santiago Guallar escreveu:
>          >      > Hi,
>          >      >
>          >   > I have a dataset which contains several time records for
>         a number
>          >    of days, plus a variable (light) that allows to determine
>         night time
>          >    (lihgt= 0) and daytime (light> 0). I need to obtain get
>         dusk time
>          >    and dawn time for each day and place them in two columns.
>          >      >
>          >      > This is the starting point (d):
>          >      > day time light
>          >      > 1    1      20
>          >      > 1    12    10
>          >      > 1    11    6
>          >      > 1    9      0
>          >      > 1    6      0
>          >      > 1    12    0
>          >      > ...
>          > > 30    8    0
>          >      > 30    3    0
>          >      > 30    8    0
>          >      > 30    3    0
>          >      > 30    8    8
>          >      > 30    9    20
>          >      >
>          >      >
>          >      > And this what I want to get:
>          >      > day time light dusk dawn
>          >      > 1    1      20    11    10
>          >      > 1    12    10    11    10
>          >      > 1    11    6      11    10
>          >      > 1    9      0      11 10
>          >      > 1    6      0      11    10
>          >      > 1    12    0      11    10
>          >      > ...
>          >      > 30    8    0      9    5
>          >      > 30    3    0      9    5
>          >      > 30    8    0      9    5
>          >      > 30    3    0      9    5
>          >      > 30    8    8      9    5
>          >      > 30    9    20    9    5
>          >      >
>          >      > This is the code for data frame d:
>          >      > day= rep(1:30, each=10)
>          >      > n= length(dia); x= c(1:24)
>          >      > time= sample(x, 300, replace= T)
>          >      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>          >      > d=data.frame(day,time,light)
>          >      >
>          >      > I'd need to impose a double condition like the next
>         but if does
>          >    not take more than one:
>          >      > attach(d)
>          >      > for (i in 1: n){
>          >      > if (light[i-1]>2 & light[i]<2){
>          >      > d$dusk<- time[i-1]
>          >      > }
>          >      > if (light[i-1]<2 & light[i]>2){
>          >      > d$dawn<- time[i]
>          >      > }
>          >      > }
>          >     > detach(d)
>          >      > d
>          >      >
>          >      > Thank you for your help
>          >      >    [[alternative HTML version deleted]]
>          >      >
>          >      >
>          >      >
>          >      > ______________________________________________
>          >      > [hidden email] <mailto:[hidden email]>
>         <mailto:[hidden email] <mailto:[hidden email]>>
>         mailing list
>          >      > https://stat.ethz.ch/mailman/listinfo/r-help
>          >      > PLEASE do read the posting guide
>          > http://www.R-project.org/posting-guide.html
>         <http://www.r-project.org/posting-guide.html>
>          >      > and provide commented, minimal, self-contained,
>         reproducible code.
>          >      >
>          >
>          >
>          >
>
>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Santiago Guallar
Nice! It works. Thank you, Rui
 
There's something that takes me back to the original question, though. The code takes the first value that meets the critical condition (light ==0); however, this value can appear more than once a day because these animals nest in dark caves, and once they are in the logger sensor registers a 0. So, a second condition limiting the time at which the critical condition can be accepted to calculate dusk should be implemented; something like (although R doesn't allow to impose the double condition):
 
if (05:00:00<z$clock> 17:00:00) {zrle <- rle(x$lig == 0)}
 
 
Santi

From: Rui Barradas <[hidden email]>

>To: Santiago Guallar <[hidden email]>
>Cc: [hidden email]
>Sent: Wednesday, July 18, 2012 11:37 AM
>Subject: Re: [R] Imposing more than one condition to if
>
>hELLO,
>
>There was no nedd to change the names of the variables inside the
>fucntion. What was going on s that in this new file column 'dtime'
>doesn't exist. Since column 'clock' exists in all files, I've changed
>the function once again, making it more general.
>
>Note that there is an argument 'colTime' with a default value. In the
>function's use below I call it with and without that argument.
>
>
>f <- function(x, colTime="clock"){
>    zrle <- rle(x$lig == 0)
>    if(zrle$values[1]){
>        idusk <- sum(zrle$lengths[1:2]) + 1
>        idawn <- zrle$lengths[1] + 1
>        x$dusk <- x[ idusk , colTime ]
>        x$dawn <- x[ idawn , colTime ]
>    }else{
>        idusk <- zrle$lengths[1] + 1
>        x$dusk <- x[ idusk , colTime ]
>        x$dawn <- NA
>    }
>    x
>}
>
>str(d)
>#d$date <- as.Date(d$date, format="%d/%m%/y")
>
>#library(chron)
>#tm <- times(as.character(d$clock))
>#d$clock <- tm
>
># See what will happen. This call uses the default 'colTime'
>bb <- by(d, d$date, f)
>for(i in seq_along(bb))    print(head(bb[[i]], 1))
>
># call and rbind. This call uses explicit arg 'colTime'
>do.call(rbind, by(d, d$date, f, colTime="clock"))
>
># Alternatively, it could be, because 'bb' is already created,
>do.call(rbind, bb)
>
>
>In the code above, I use an optional conversion to date and time
>classes; as.Date is part of base R, but class times needs package chron.
>It's a good idea to convert these variables, you can later use, say,
>arithmetics on dates and times, such as differences.
>
>Hope this helps,
>
>Rui Barradas
>
>Em 17-07-2012 19:53, Santiago Guallar escreveu:
>> Thank for your time, Rui.
>> Now, I get this error message:
>> Error en rbind(deparse.level, ...) :
>> numbers of columns of arguments do not match
>> Apparently, some columns have missing values and rbind doesn't work. I
>> tried:
>> require(plyr)
>> do.call(rbind.fill, by(z, z$date, f))
>> Then the code runs through but dusk the variable dusk is missing and
>> dawn is filled with NA.
>> Just in case the problem simply lies in a name, this is your code after
>> I changed the object names (basically 'x' and 'd' by 'z') to adapt them
>> to the names of my dataset:
>> f <- function(z){
>> zrle <- rle(z$lig == 0)
>> if(zrle$values[1]){
>> idusk <- sum(zrle$lengths[1:2]) + 1
>> idawn <- zrle$lengths[1] + 1
>> z$dusk <- z$dtime[ idusk ]
>> z$dawn <- z$dtime[ idawn ]
>> }else{
>> idusk <- zrle$lengths[1] + 1
>> z$dusk <- z$dtime[ idusk ]
>> z$dawn <- NA
>> }
>> z
>> }
>>
>> do.call(rbind, by(z, z$date, f))
>> Again, I attached a dput() with the object z which contains my dataset.
>> Santi
>>
>>    *From:* Rui Barradas <[hidden email]>
>>    *To:* Santiago Guallar <[hidden email]>
>>    *Cc:* [hidden email]
>>    *Sent:* Tuesday, July 17, 2012 11:52 AM
>>    *Subject:* Re: [R] Imposing more than one condition to if
>>
>>
>>        Hello,
>>
>>        My code couldn't find the right input columns because your real
>>        data has
>>        different names, it could only find the example dataset's names.
>>
>>        And there's another problem, my code would give correct answers
>>        with a
>>        limted number of possible inputs and fail with real data.
>>
>>        Corrected:
>>
>>
>>        f <- function(x){
>>              zrle <- rle(x$lig == 0)
>>              if(zrle$values[1]){
>>                  idusk <- sum(zrle$lengths[1:2]) + 1
>>                  idawn <- zrle$lengths[1] + 1
>>                  x$dusk <- x$dtime[ idusk ]
>>                  x$dawn <- x$dtime[ idawn ]
>>              }else{
>>                  idusk <- zrle$lengths[1] + 1
>>                  x$dusk <- x$dtime[ idusk ]
>>                  x$dawn <- NA
>>              }
>>              x
>>        }
>>
>>        do.call(rbind, by(d, d$date, f))
>>
>>
>>        One more thing, you are reading your dataset into a data.frame
>>        forgetting that character strings become factors. Try str(d) to
>>        see it.
>>        ('d' is the data.frame.) You could/should coerce the date/time
>>        values to
>>        appropriate classes, something like
>>
>>
>>        d$time <- as.character(d$time)
>>        d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
>>        d$date <- as.character(d$date)
>>        d$date <- as.Date(d$date, format="%d/%m/%y")
>>
>>
>>        Hope this helps,
>>
>>        Rui Barradas
>>
>>        Em 17-07-2012 07:14, Santiago Guallar escreveu:
>>          > Thank you Rui,
>>          >
>>          > When applied to my original data, your code goes through
>>        although it
>>          > does not produce the correct results: for dusk gives the
>>        first time
>>          > value of next day, for dawn it gives NA. It seems that the
>>        function f
>>          > doesn't find the right input columns.
>>          > A, Ilso had to push up the memory size.
>>          > Attached a file (containing just 3000 of the original c.
>>        45000 rows)
>>          > after dput().
>>          >
>>          > Santi
>>          >
>>          >
>>          >
>>        ------------------------------------------------------------------------
>>          >    *From:* Rui Barradas <[hidden email]
>>        <mailto:[hidden email]>>
>>          >    *To:* Santiago Guallar <[hidden email]
>>        <mailto:[hidden email]>>
>>          >    *Cc:* [hidden email] <mailto:[hidden email]>
>>          >    *Sent:* Sunday, July 15, 2012 7:21 PM
>>          >    *Subject:* Re: [R] Imposing more than one condition to if
>>          >
>>          > Hello,
>>          >
>>          >    There are obvious bugs in your code, you are testing for
>>        light > 2 or
>>          >    ligth < 2 but this would mean that dusk and dawn are
>>        undetermined for
>>          >    light == 2 and that they happen at light == 1.
>>          >
>>          >    Without loops or compound logical conditions:
>>          >
>>          >
>>          >    f <- function(x){
>>          >          x$dawn <- x$time[ which.min(x$light) ]
>>          >          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>>          >          x
>>          >    }
>>          >
>>          >    do.call(rbind, by(d, d$day, f))
>>          >
>>          >    Hope this helps,
>>          >
>>          >    Rui Barradas
>>          >
>>          >    Em 15-07-2012 17:32, Santiago Guallar escreveu:
>>          >      > Hi,
>>          >      >
>>          >  > I have a dataset which contains several time records for
>>        a number
>>          >    of days, plus a variable (light) that allows to determine
>>        night time
>>          >    (lihgt= 0) and daytime (light> 0). I need to obtain get
>>        dusk time
>>          >    and dawn time for each day and place them in two columns.
>>          >      >
>>          >      > This is the starting point (d):
>>          >      > day time light
>>          >      > 1    1      20
>>          >      > 1    12    10
>>          >      > 1    11    6
>>          >      > 1    9      0
>>          >      > 1    6      0
>>          >      > 1    12    0
>>          >      > ...
>>          > > 30    8    0
>>          >      > 30    3    0
>>          >      > 30    8    0
>>          >      > 30    3    0
>>          >      > 30    8    8
>>          >      > 30    9    20
>>          >      >
>>          >      >
>>          >      > And this what I want to get:
>>          >      > day time light dusk dawn
>>          >      > 1    1      20    11    10
>>          >      > 1    12    10    11    10
>>          >      > 1    11    6      11    10
>>          >      > 1    9      0      11 10
>>          >      > 1    6      0      11    10
>>          >      > 1    12    0      11    10
>>          >      > ...
>>          >      > 30    8    0      9    5
>>          >      > 30    3    0      9    5
>>          >      > 30    8    0      9    5
>>          >      > 30    3    0      9    5
>>          >      > 30    8    8      9    5
>>          >      > 30    9    20    9    5
>>          >      >
>>          >      > This is the code for data frame d:
>>          >      > day= rep(1:30, each=10)
>>          >      > n= length(dia); x= c(1:24)
>>          >      > time= sample(x, 300, replace= T)
>>          >      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>>          >      > d=data.frame(day,time,light)
>>          >      >
>>          >      > I'd need to impose a double condition like the next
>>        but if does
>>          >    not take more than one:
>>          >      > attach(d)
>>          >      > for (i in 1: n){
>>          >      > if (light[i-1]>2 & light[i]<2){
>>          >      > d$dusk<- time[i-1]
>>          >      > }
>>          >      > if (light[i-1]<2 & light[i]>2){
>>          >      > d$dawn<- time[i]
>>          >      > }
>>          >      > }
>>          >    > detach(d)
>>          >      > d
>>          >      >
>>          >      > Thank you for your help
>>          >      >    [[alternative HTML version deleted]]
>>          >      >
>>          >      >
>>          >      >
>>          >      > ______________________________________________
>>          >      > [hidden email] <mailto:[hidden email]>
>>        <mailto:[hidden email] <mailto:[hidden email]>>
>>        mailing list
>>          >      > https://stat.ethz.ch/mailman/listinfo/r-help
>>          >      > PLEASE do read the posting guide
>>          > http://www.R-project.org/posting-guide.html
>>        <http://www.r-project.org/posting-guide.html>
>>          >      > and provide commented, minimal, self-contained,
>>        reproducible code.
>>          >      >
>>          >
>>          >
>>          >
>>
>>
>>
>>
>>
>
>
>
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Rui Barradas
Hello,

You're right. I had thought of this, and I believe there's a day when it
happens to have a zero in the middle of the day. R doesn't allow
conditions like the one you've written but it does allow multiple
conditions, combined using the logical connectives, 'not' - '!', 'or' -
'|' and 'and' - '&'. Let's see:

Day <- 05:00:00 <= x$clock & x$clock <= 17:00:00
Night <- !(05:00:00 <= x$clock & x$clock <= 17:00:00) # equal to:
Night <- x$clock < 05:00:00 | x$clock > 17:00:00

So, you had the first condition reversed and maybe !Day is more readable ;)

(Note: to check if a variable is in an interval, say (a, b), I find it
better to write a < x & x < b with the interval's end points as
condition extremes, like Day above. Then if negation is needed half the
work is already done.)

Anyway, it should be easy to put the compound condition in the function f().

Rui Barradas

Em 18-07-2012 19:02, Santiago Guallar escreveu:

> Nice! It works. Thank you, Rui
> There's something that takes me back to the original question, though.
> The code takes the first value that meets the critical condition (light
> ==0); however, this value can appear more than once a day because these
> animals nest in dark caves, and once they are in the logger sensor
> registers a 0. So, a second condition limiting the time at which the
> critical condition can be accepted to calculate dusk should be
> implemented; something like (although R doesn't allow to impose the
> double condition):
> if (05:00:00<z$clock> 17:00:00) {zrle <- rle(x$lig == 0)}
> Santi
>
>     *From:* Rui Barradas <[hidden email]>
>     *To:* Santiago Guallar <[hidden email]>
>     *Cc:* [hidden email]
>     *Sent:* Wednesday, July 18, 2012 11:37 AM
>     *Subject:* Re: [R] Imposing more than one condition to if
>
>     hELLO,
>
>     There was no nedd to change the names of the variables inside the
>     fucntion. What was going on s that in this new file column 'dtime'
>     doesn't exist. Since column 'clock' exists in all files, I've changed
>     the function once again, making it more general.
>
>     Note that there is an argument 'colTime' with a default value. In the
>     function's use below I call it with and without that argument.
>
>
>     f <- function(x, colTime="clock"){
>          zrle <- rle(x$lig == 0)
>          if(zrle$values[1]){
>              idusk <- sum(zrle$lengths[1:2]) + 1
>              idawn <- zrle$lengths[1] + 1
>              x$dusk <- x[ idusk , colTime ]
>              x$dawn <- x[ idawn , colTime ]
>          }else{
>              idusk <- zrle$lengths[1] + 1
>              x$dusk <- x[ idusk , colTime ]
>              x$dawn <- NA
>          }
>          x
>     }
>
>     str(d)
>     #d$date <- as.Date(d$date, format="%d/%m%/y")
>
>     #library(chron)
>     #tm <- times(as.character(d$clock))
>     #d$clock <- tm
>
>     # See what will happen. This call uses the default 'colTime'
>     bb <- by(d, d$date, f)
>     for(i in seq_along(bb))    print(head(bb[[i]], 1))
>
>     # call and rbind. This call uses explicit arg 'colTime'
>     do.call(rbind, by(d, d$date, f, colTime="clock"))
>
>     # Alternatively, it could be, because 'bb' is already created,
>     do.call(rbind, bb)
>
>
>     In the code above, I use an optional conversion to date and time
>     classes; as.Date is part of base R, but class times needs package
>     chron.
>     It's a good idea to convert these variables, you can later use, say,
>     arithmetics on dates and times, such as differences.
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Em 17-07-2012 19:53, Santiago Guallar escreveu:
>      > Thank for your time, Rui.
>      > Now, I get this error message:
>      > Error en rbind(deparse.level, ...) :
>      > numbers of columns of arguments do not match
>      > Apparently, some columns have missing values and rbind doesn't
>     work. I
>      > tried:
>      > require(plyr)
>      > do.call(rbind.fill, by(z, z$date, f))
>      > Then the code runs through but dusk the variable dusk is missing and
>      > dawn is filled with NA.
>      > Just in case the problem simply lies in a name, this is your code
>     after
>      > I changed the object names (basically 'x' and 'd' by 'z') to
>     adapt them
>      > to the names of my dataset:
>      > f <- function(z){
>      > zrle <- rle(z$lig == 0)
>      > if(zrle$values[1]){
>      > idusk <- sum(zrle$lengths[1:2]) + 1
>      > idawn <- zrle$lengths[1] + 1
>      > z$dusk <- z$dtime[ idusk ]
>      > z$dawn <- z$dtime[ idawn ]
>      > }else{
>      > idusk <- zrle$lengths[1] + 1
>      > z$dusk <- z$dtime[ idusk ]
>      > z$dawn <- NA
>      > }
>      > z
>      > }
>      >
>      > do.call(rbind, by(z, z$date, f))
>      > Again, I attached a dput() with the object z which contains my
>     dataset.
>      > Santi
>      >
>      >    *From:* Rui Barradas <[hidden email]
>     <mailto:[hidden email]>>
>      >    *To:* Santiago Guallar <[hidden email]
>     <mailto:[hidden email]>>
>      >    *Cc:* [hidden email] <mailto:[hidden email]>
>      >    *Sent:* Tuesday, July 17, 2012 11:52 AM
>      >    *Subject:* Re: [R] Imposing more than one condition to if
>      >
>      >
>      >        Hello,
>      >
>      >        My code couldn't find the right input columns because your
>     real
>      >        data has
>      >        different names, it could only find the example dataset's
>     names.
>      >
>      >        And there's another problem, my code would give correct
>     answers
>      >        with a
>      >        limted number of possible inputs and fail with real data.
>      >
>      >        Corrected:
>      >
>      >
>      >        f <- function(x){
>      >              zrle <- rle(x$lig == 0)
>      >              if(zrle$values[1]){
>      >                  idusk <- sum(zrle$lengths[1:2]) + 1
>      >                  idawn <- zrle$lengths[1] + 1
>      >                  x$dusk <- x$dtime[ idusk ]
>      >                  x$dawn <- x$dtime[ idawn ]
>      >              }else{
>      >                  idusk <- zrle$lengths[1] + 1
>      >                  x$dusk <- x$dtime[ idusk ]
>      >                  x$dawn <- NA
>      >              }
>      >              x
>      >        }
>      >
>      >        do.call(rbind, by(d, d$date, f))
>      >
>      >
>      >        One more thing, you are reading your dataset into a data.frame
>      >        forgetting that character strings become factors. Try
>     str(d) to
>      >        see it.
>      >        ('d' is the data.frame.) You could/should coerce the date/time
>      >       values to
>      >        appropriate classes, something like
>      >
>      >
>      >        d$time <- as.character(d$time)
>      >        d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
>      >        d$date <- as.character(d$date)
>      >        d$date <- as.Date(d$date, format="%d/%m/%y")
>      >
>      >
>      >        Hope this helps,
>      >
>      >        Rui Barradas
>      >
>      >        Em 17-07-2012 07:14, Santiago Guallar escreveu:
>      >          > Thank you Rui,
>      >          >
>      >          > When applied to my original data, your code goes through
>      >        although it
>      >          > does not produce the correct results: for dusk gives the
>      >        first time
>      >          > value of next day, for dawn it gives NA. It seems that the
>      >        function f
>      >          > doesn't find the right input columns.
>      >          > A, Ilso had to push up the memory size.
>      >          > Attached a file (containing just 3000 of the original c.
>      >        45000 rows)
>      >          > after dput().
>      >          >
>      >          > Santi
>      >          >
>      >          >
>      >          >
>      >
>     ------------------------------------------------------------------------
>      >          >    *From:* Rui Barradas <[hidden email]
>     <mailto:[hidden email]>
>      >        <mailto:[hidden email] <mailto:[hidden email]>>>
>      >          >    *To:* Santiago Guallar <[hidden email]
>     <mailto:[hidden email]>
>      >        <mailto:[hidden email] <mailto:[hidden email]>>>
>      >          >    *Cc:* [hidden email]
>     <mailto:[hidden email]> <mailto:[hidden email]
>     <mailto:[hidden email]>>
>      >          >    *Sent:* Sunday, July 15, 2012 7:21 PM
>      >          >    *Subject:* Re: [R] Imposing more than one condition
>     to if
>      >          >
>      >          > Hello,
>      >          >
>      >          >    There are obvious bugs in your code, you are
>     testing for
>      >        light > 2 or
>      >          >    ligth < 2 but this would mean that dusk and dawn are
>      >        undetermined for
>      >          >    light == 2 and that they happen at light == 1.
>      >          >
>      > >    Without loops or compound logical conditions:
>      >          >
>      >          >
>      >          >    f <- function(x){
>      >          >          x$dawn <- x$time[ which.min(x$light) ]
>      >          >          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>      >          >          x
>      >          >    }
>      >          >
>      >          >    do.call(rbind, by(d, d$day, f))
>      >          >
>      >          >    Hope this helps,
>      >          >
>      >         >    Rui Barradas
>      >          >
>      >          >    Em 15-07-2012 17:32, Santiago Guallar escreveu:
>      >          >      > Hi,
>      >          >      >
>      >          >  > I have a dataset which contains several time
>     records for
>      >        a number
>      >          >    of days, plus a variable (light) that allows to
>     determine
>      >        night time
>      >          >    (lihgt= 0) and daytime (light> 0). I need to obtain get
>      >        dusk time
>      >          >    and dawn time for each day and place them in two
>     columns.
>      >          >      >
>      >          >      > This is the starting point (d):
>      >          >      > day time light
>      >          >      > 1    1      20
>      >          >      > 1    12    10
>      >          >      > 1    11    6
>      >          >      > 1    9      0
>      >          >      > 1    6      0
>      >          >      > 1    12    0
>      > >      > ...
>      >          > > 30    8    0
>      >          >      > 30    3    0
>      >          >      > 30    8    0
>      >          >      > 30    3    0
>      >          >      > 30    8    8
>      >          >      > 30    9    20
>      >          >      >
>      >          >      >
>      >          >      > And this what I want to get:
>      >          >      > day time light dusk dawn
>      >          >      > 1    1      20    11    10
>      >          >      > 1    12    10    11    10
>      >          >      > 1    11    6      11    10
>      >          >      > 1    9      0      11 10
>      >          >      > 1    6      0      11    10
>      >          >      > 1    12    0      11    10
>      >          >      > ...
>      >         >      > 30    8    0      9    5
>      >          >      > 30    3    0      9    5
>      >          >      > 30    8    0      9    5
>      >          >      > 30    3    0      9    5
>      >          >      > 30    8    8      9    5
>      >          >      > 30    9    20    9    5
>      >          >      >
>      >          >   > This is the code for data frame d:
>      >          >      > day= rep(1:30, each=10)
>      >          >      > n= length(dia); x= c(1:24)
>      >          >      > time= sample(x, 300, replace= T)
>      >          >      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>      >          >      > d=data.frame(day,time,light)
>      >          >      >
>      >          >      > I'd need to impose a double condition like the next
>      >        but if does
>      >          >    not take more than one:
>      >          >      > attach(d)
>      >          >      > for (i in 1: n){
>      >          >      > if (light[i-1]>2 & light[i]<2){
>      >          >      > d$dusk<- time[i-1]
>      >          >      > }
>      >          >      > if (light[i-1]<2 & light[i]>2){
>      >          >      > d$dawn<- time[i]
>      >          >      > }
>      >          >      > }
>      >          >    > detach(d)
>      >          >      > d
>      >          > >
>      >          >      > Thank you for your help
>      >          >      >    [[alternative HTML version deleted]]
>      >          >      >
>      >          >      >
>      >          >      >
>      >          >      > ______________________________________________
>      >          >      > [hidden email]
>     <mailto:[hidden email]> <mailto:[hidden email]
>     <mailto:[hidden email]>>
>      >        <mailto:[hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>>
>      >        mailing list
>      >          >      > https://stat.ethz.ch/mailman/listinfo/r-help
>      >          >      > PLEASE do read the posting guide
>      >          > http://www.R-project.org/posting-guide.html
>     <http://www.r-project.org/posting-guide.html>
>      >        <http://www.r-project.org/posting-guide.html>
>      >          >      > and provide commented, minimal, self-contained,
>      >        reproducible code.
>      >          >      >
>      >          >
>      >          >
>      >          >
>      >
>      >
>      >
>      >
>      >
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Imposing more than one condition to if

Santiago Guallar
Great! Thank you ever so much Rui.

Santi



>________________________________
> From: Rui Barradas <[hidden email]>
>To: Santiago Guallar <[hidden email]>
>Cc: [hidden email]
>Sent: Wednesday, July 18, 2012 8:29 PM
>Subject: Re: [R] Imposing more than one condition to if
>
>Hello,
>
>You're right. I had thought of this, and I believe there's a day when it
>happens to have a zero in the middle of the day. R doesn't allow
>conditions like the one you've written but it does allow multiple
>conditions, combined using the logical connectives, 'not' - '!', 'or' -
>'|' and 'and' - '&'. Let's see:
>
>Day <- 05:00:00 <= x$clock & x$clock <= 17:00:00
>Night <- !(05:00:00 <= x$clock & x$clock <= 17:00:00) # equal to:
>Night <- x$clock < 05:00:00 | x$clock > 17:00:00
>
>So, you had the first condition reversed and maybe !Day is more readable ;)
>
>(Note: to check if a variable is in an interval, say (a, b), I find it
>better to write a < x & x < b with the interval's end points as
>condition extremes, like Day above. Then if negation is needed half the
>work is already done.)
>
>Anyway, it should be easy to put the compound condition in the function f().
>
>Rui Barradas
>
>Em 18-07-2012 19:02, Santiago Guallar escreveu:
>> Nice! It works. Thank you, Rui
>> There's something that takes me back to the original question, though.
>> The code takes the first value that meets the critical condition (light
>> ==0); however, this value can appear more than once a day because these
>> animals nest in dark caves, and once they are in the logger sensor
>> registers a 0. So, a second condition limiting the time at which the
>> critical condition can be accepted to calculate dusk should be
>> implemented; something like (although R doesn't allow to impose the
>> double condition):
>> if (05:00:00<z$clock> 17:00:00) {zrle <- rle(x$lig == 0)}
>> Santi
>>
>>     *From:* Rui Barradas <[hidden email]>
>>     *To:* Santiago Guallar <[hidden email]>
>>     *Cc:* [hidden email]
>>     *Sent:* Wednesday, July 18, 2012 11:37 AM
>>     *Subject:* Re: [R] Imposing more than one condition to if
>>
>>     hELLO,
>>
>>     There was no nedd to change the names of the variables inside the
>>     fucntion. What was going on s that in this new file column 'dtime'
>>     doesn't exist. Since column 'clock' exists in all files, I've changed
>>     the function once again, making it more general.
>>
>>     Note that there is an argument 'colTime' with a default value. In the
>>     function's use below I call it with and without that argument.
>>
>>
>>     f <- function(x, colTime="clock"){
>>          zrle <- rle(x$lig == 0)
>>          if(zrle$values[1]){
>>              idusk <- sum(zrle$lengths[1:2]) + 1
>>              idawn <- zrle$lengths[1] + 1
>>              x$dusk <- x[ idusk , colTime ]
>>              x$dawn <- x[ idawn , colTime ]
>>          }else{
>>              idusk <- zrle$lengths[1] + 1
>>              x$dusk <- x[ idusk , colTime ]
>>              x$dawn <- NA
>>          }
>>          x
>>     }
>>
>>     str(d)
>>     #d$date <- as.Date(d$date, format="%d/%m%/y")
>>
>>     #library(chron)
>>     #tm <- times(as.character(d$clock))
>>     #d$clock <- tm
>>
>>     # See what will happen. This call uses the default 'colTime'
>>     bb <- by(d, d$date, f)
>>     for(i in seq_along(bb))    print(head(bb[[i]], 1))
>>
>>     # call and rbind. This call uses explicit arg 'colTime'
>>     do.call(rbind, by(d, d$date, f, colTime="clock"))
>>
>>     # Alternatively, it could be, because 'bb' is already created,
>>     do.call(rbind, bb)
>>
>>
>>     In the code above, I use an optional conversion to date and time
>>     classes; as.Date is part of base R, but class times needs package
>>     chron.
>>     It's a good idea to convert these variables, you can later use, say,
>>     arithmetics on dates and times, such as differences.
>>
>>     Hope this helps,
>>
>>     Rui Barradas
>>
>>     Em 17-07-2012 19:53, Santiago Guallar escreveu:
>>      > Thank for your time, Rui.
>>      > Now, I get this error message:
>>      > Error en rbind(deparse.level, ...) :
>>      > numbers of columns of arguments do not match
>>      > Apparently, some columns have missing values and rbind doesn't
>>     work. I
>>      > tried:
>>      > require(plyr)
>>      > do.call(rbind.fill, by(z, z$date, f))
>>      > Then the code runs through but dusk the variable dusk is missing and
>>      > dawn is filled with NA.
>>      > Just in case the problem simply lies in a name, this is your code
>>     after
>>      > I changed the object names (basically 'x' and 'd' by 'z') to
>>     adapt them
>>      > to the names of my dataset:
>>      > f <- function(z){
>>      > zrle <- rle(z$lig == 0)
>>      > if(zrle$values[1]){
>>      > idusk <- sum(zrle$lengths[1:2]) + 1
>>      > idawn <- zrle$lengths[1] + 1
>>      > z$dusk <- z$dtime[ idusk ]
>>      > z$dawn <- z$dtime[ idawn ]
>>      > }else{
>>      > idusk <- zrle$lengths[1] + 1
>>      > z$dusk <- z$dtime[ idusk ]
>>      > z$dawn <- NA
>>      > }
>>      > z
>>      > }
>>      >
>>      > do.call(rbind, by(z, z$date, f))
>>      > Again, I attached a dput() with the object z which contains my
>>     dataset.
>>      > Santi
>>      >
>>      >    *From:* Rui Barradas <[hidden email]
>>     <mailto:[hidden email]>>
>>      >    *To:* Santiago Guallar <[hidden email]
>>     <mailto:[hidden email]>>
>>      >    *Cc:* [hidden email] <mailto:[hidden email]>
>>      >    *Sent:* Tuesday, July 17, 2012 11:52 AM
>>      >    *Subject:* Re: [R] Imposing more than one condition to if
>>      >
>>      >
>>      >        Hello,
>>      >
>>      >        My code couldn't find the right input columns because your
>>     real
>>      >        data has
>>      >        different names, it could only find the example dataset's
>>     names.
>>      >
>>      >        And there's another problem, my code would give correct
>>     answers
>>      >        with a
>>      >        limted number of possible inputs and fail with real data.
>>      >
>>      >        Corrected:
>>      >
>>      >
>>      >        f <- function(x){
>>      >              zrle <- rle(x$lig == 0)
>>      >              if(zrle$values[1]){
>>      >                  idusk <- sum(zrle$lengths[1:2]) + 1
>>      >                  idawn <- zrle$lengths[1] + 1
>>      >                  x$dusk <- x$dtime[ idusk ]
>>      >                  x$dawn <- x$dtime[ idawn ]
>>      >              }else{
>>      >                  idusk <- zrle$lengths[1] + 1
>>      >                  x$dusk <- x$dtime[ idusk ]
>>      >                  x$dawn <- NA
>>      >              }
>>      >              x
>>      >        }
>>      >
>>      >        do.call(rbind, by(d, d$date, f))
>>      >
>>      >
>>      >        One more thing, you are reading your dataset into a data.frame
>>      >        forgetting that character strings become factors. Try
>>     str(d) to
>>      >        see it.
>>      >        ('d' is the data.frame.) You could/should coerce the date/time
>>      >       values to
>>      >        appropriate classes, something like
>>      >
>>      >
>>      >        d$time <- as.character(d$time)
>>      >        d$time <- as.POSIXct(d$time, format="%d/%m/%y %H:%M:%S")
>>      >        d$date <- as.character(d$date)
>>      >        d$date <- as.Date(d$date, format="%d/%m/%y")
>>      >
>>      >
>>      >        Hope this helps,
>>      >
>>      >        Rui Barradas
>>      >
>>      >        Em 17-07-2012 07:14, Santiago Guallar escreveu:
>>      >          > Thank you Rui,
>>      >          >
>>      >          > When applied to my original data, your code goes through
>>      >        although it
>>      >          > does not produce the correct results: for dusk gives the
>>      >        first time
>>      >          > value of next day, for dawn it gives NA. It seems that the
>>      >        function f
>>      >          > doesn't find the right input columns.
>>      >          > A, Ilso had to push up the memory size.
>>      >          > Attached a file (containing just 3000 of the original c.
>>      >        45000 rows)
>>      >          > after dput().
>>      >          >
>>      >          > Santi
>>      >          >
>>      >          >
>>      >          >
>>      >
>>     ------------------------------------------------------------------------
>>      >          >    *From:* Rui Barradas <[hidden email]
>>     <mailto:[hidden email]>
>>      >        <mailto:[hidden email] <mailto:[hidden email]>>>
>>      >          >    *To:* Santiago Guallar <[hidden email]
>>     <mailto:[hidden email]>
>>      >        <mailto:[hidden email] <mailto:[hidden email]>>>
>>      >          >    *Cc:* [hidden email]
>>     <mailto:[hidden email]> <mailto:[hidden email]
>>     <mailto:[hidden email]>>
>>      >          >    *Sent:* Sunday, July 15, 2012 7:21 PM
>>      >          >    *Subject:* Re: [R] Imposing more than one condition
>>     to if
>>      >          >
>>      >          > Hello,
>>      >          >
>>      >          >    There are obvious bugs in your code, you are
>>     testing for
>>      >        light > 2 or
>>      >          >    ligth < 2 but this would mean that dusk and dawn are
>>      >        undetermined for
>>      >          >    light == 2 and that they happen at light == 1.
>>      >          >
>>      > >    Without loops or compound logical conditions:
>>      >          >
>>      >          >
>>      >          >    f <- function(x){
>>      >          >          x$dawn <- x$time[ which.min(x$light) ]
>>      >          >          x$dusk <- x$time[ max(which(x$light == 0)) + 1 ]
>>      >          >          x
>>      >          >    }
>>      >          >
>>      >          >    do.call(rbind, by(d, d$day, f))
>>      >          >
>>      >          >    Hope this helps,
>>      >          >
>>      >         >    Rui Barradas
>>      >          >
>>      >          >    Em 15-07-2012 17:32, Santiago Guallar escreveu:
>>      >          >      > Hi,
>>      >          >      >
>>      >          >  > I have a dataset which contains several time
>>     records for
>>      >        a number
>>      >          >    of days, plus a variable (light) that allows to
>>     determine
>>      >        night time
>>      >          >    (lihgt= 0) and daytime (light> 0). I need to obtain get
>>      >        dusk time
>>      >          >    and dawn time for each day and place them in two
>>     columns.
>>      >          >      >
>>      >          >      > This is the starting point (d):
>>      >          >      > day time light
>>      >          >      > 1    1      20
>>      >          >      > 1    12    10
>>      >          >      > 1    11    6
>>      >          >      > 1    9      0
>>      >          >      > 1    6      0
>>      >          >      > 1    12    0
>>      > >      > ...
>>      >          > > 30    8    0
>>      >          >      > 30    3    0
>>      >          >      > 30    8    0
>>      >          >      > 30    3    0
>>      >          >      > 30    8    8
>>      >          >      > 30    9    20
>>      >          >      >
>>      >          >      >
>>      >          >      > And this what I want to get:
>>      >          >      > day time light dusk dawn
>>      >          >      > 1    1      20    11    10
>>      >          >      > 1    12    10    11    10
>>      >          >      > 1    11    6      11    10
>>      >          >      > 1    9      0      11 10
>>      >          >      > 1    6      0      11    10
>>      >          >      > 1    12    0      11    10
>>      >          >      > ...
>>      >         >      > 30    8    0      9    5
>>      >          >      > 30    3    0      9    5
>>      >          >      > 30    8    0      9    5
>>      >          >      > 30    3    0      9    5
>>      >          >      > 30    8    8      9    5
>>      >          >      > 30    9    20    9    5
>>      >          >      >
>>      >          >   > This is the code for data frame d:
>>      >          >      > day= rep(1:30, each=10)
>>      >          >      > n= length(dia); x= c(1:24)
>>      >          >      > time= sample(x, 300, replace= T)
>>      >          >      > light= rep(c(20,10,6,0,0,0,0,0,8,20), 30)
>>      >          >      > d=data.frame(day,time,light)
>>      >          >      >
>>      >          >      > I'd need to impose a double condition like the next
>>      >        but if does
>>      >          >    not take more than one:
>>      >          >      > attach(d)
>>      >          >      > for (i in 1: n){
>>      >          >      > if (light[i-1]>2 & light[i]<2){
>>      >          >      > d$dusk<- time[i-1]
>>      >          >      > }
>>      >          >      > if (light[i-1]<2 & light[i]>2){
>>      >          >      > d$dawn<- time[i]
>>      >          >      > }
>>      >          >      > }
>>      >          >    > detach(d)
>>      >          >      > d
>>      >          > >
>>      >          >      > Thank you for your help
>>      >          >      >    [[alternative HTML version deleted]]
>>      >          >      >
>>      >          >      >
>>      >          >      >
>>      >          >      > ______________________________________________
>>      >          >      > [hidden email]
>>     <mailto:[hidden email]> <mailto:[hidden email]
>>     <mailto:[hidden email]>>
>>      >        <mailto:[hidden email] <mailto:[hidden email]>
>>     <mailto:[hidden email] <mailto:[hidden email]>>>
>>      >        mailing list
>>      >          >      > https://stat.ethz.ch/mailman/listinfo/r-help
>>      >          >      > PLEASE do read the posting guide
>>      >          > http://www.R-project.org/posting-guide.html
>>     <http://www.r-project.org/posting-guide.html>
>>      >        <http://www.r-project.org/posting-guide.html>
>>      >          >      > and provide commented, minimal, self-contained,
>>      >        reproducible code.
>>      >          >      >
>>      >          >
>>      >          >
>>      >          >
>>      >
>>      >
>>      >
>>      >
>>      >
>>
>>
>>
>
>
>
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Loading...