[R] Imposing more than one condition to if

John Kane jrkrideau at inbox.com
Sun Jul 15 19:16:44 CEST 2012


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: sguallar at yahoo.com
> Sent: Sun, 15 Jul 2012 09:32:33 -0700 (PDT)
> To: r-help at r-project.org
> 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]]
> 
> ______________________________________________
> 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.

____________________________________________________________
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



More information about the R-help mailing list