Skip to content

Imposing more than one condition to if

4 messages · Santiago Guallar, John Kane, Rui Barradas +1 more

#
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
____________________________________________________________
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
#
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:
#
try this:
+     # create new dataframe with the values
+     cbind(.day[, c('day', 'time', 'light')]
+         , dawn = .day$time[.day$dawn]
+         , dusk = .day$time[.day$dusk]
+         )
+ }))
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 <sguallar at yahoo.com> wrote: