Re: [R] spped up a function

2013-07-08 Thread Santiago Guallar
Hi Petr, yes the function basically consists on merging two time series with 
different time intervals: one regular 'GPS' and one irregular 'xact' (the 
latter containing the binomial variable 'wd' that I want to add to 'GPS'.
Apparently my attachments did not go through. Here you have the dputs you 
requested plus the desired result based on them:

head(xact)
Ringjul   timepos   actwd
6106933 15135 2011-06-10 04:36:15  3822 dry
6106933 15135 2011-06-10 05:39:57    27 wet
6106933 15135 2011-06-10 05:40:24    60 dry
6106933 15135 2011-06-10 05:41:24     6 wet
6106933 15135 2011-06-10 05:41:30   753 dry
6106933 15135 2011-06-10 05:54:03    78 wet
6106933 15135 2011-06-10 05:55:21    15 dry
6106933 15135 2011-06-10 05:55:36    18 wet

head(GPS1, 16) and desired result (added column wd) 

      Ring   jul             timeposwd
5  6106933 15135 2011-06-10 04:39:00dry
6  6106933 15135 2011-06-10 04:44:00dry
7  6106933 15135 2011-06-10 04:49:00dry
8  6106933 15135 2011-06-10 04:54:00dry
9  6106933 15135 2011-06-10 04:59:00dry
10 6106933 15135 2011-06-10 05:04:00dry
11 6106933 15135 2011-06-10 05:09:00dry
12 6106933 15135 2011-06-10 05:13:00dry
13 6106933 15135 2011-06-10 05:18:00dry
14 6106933 15135 2011-06-10 05:23:00dry
15 6106933 15135 2011-06-10 05:28:00dry
16 6106933 15135 2011-06-10 05:33:00dry
17 6106933 15135 2011-06-10 05:38:00dry
18 6106933 15135 2011-06-10 05:43:00dry
19 6106933 15135 2011-06-10 05:48:00dry
20 6106933 15135 2011-06-10 05:53:00dry

Santi


>________
> From: PIKAL Petr 
>To: Santiago Guallar ; r-help  
>Sent: Monday, July 8, 2013 11:34 AM
>Subject: RE: [R] spped up a function
> 
>
>Hi
>
>It seems to me, that you basically want merge, but I can miss the point. Try 
>post
>
>dput(head(xact))
>dput(head(GPS))
>
>and what shall be desired result based on those 2 datasets.
>
>Regards
>Petr
>
>
>> -Original Message-
>> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
>> project.org] On Behalf Of Santiago Guallar
>> Sent: Tuesday, July 02, 2013 7:47 PM
>> To: r-help
>> Subject: [R] spped up a function
>> 
>> Hi,
>> 
>> I have written a function to assign the values of a certain variable
>> 'wd' from a dataset to another dataset. Both contain data from the
>> same time period but differ in the length of their time intervals:
>> 'GPS' has regular 10-minute intervals whereas 'xact' has
 irregular
>> intervals. I attached simplified text versions from write.table. You
>> can also get a dput of 'xact' in this address:
>> http://www.megafileupload.com/en/file/431569/xact-dput.html).
>> The original objects are large and the function takes almost one hour
>> to finish.
>> Here's the function:
>> 
>> fxG= function(xact, GPS){
>> l <- rep( 'A', nrow(GPS) )
>> v <- unique(GPS$Ring) # the process is carried out for several
>> individuals identified by 'Ring'
>> for(k in 1:length(v) ){
>> I = v[k]
>> df <- xact[xact$Ring == I,]
>> for(i in 1:nrow(GPS)){
>> if(GPS[i,]$Ring== I){# the code runs along the whole data.frame for
>> each i; it'd save time to make it stop with the last record of each i
>> instead u <- df$timepos <= GPS[i,]$timepos # fill vector l for each
>> interval t from xact <= each interval from GPS (take the max if
 there's
>> > 1 interval) l[i] <- df[max( which(u == TRUE) ),]$wd } } } return(l)}
>> 
>> vwd <- fxG(xact, GPS)
>> 
>> 
>> My question is: how can I speed up (optimize) this function?
>> 
>> Thank you for your help
>
>
>__
R-help@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.


Re: [R] speed up a function

2013-07-15 Thread Santiago Guallar
Dear Petr,

Sorry for the delay. I've been out.
Unfortunately, your code doesn't work either even when using fromLast = T.
Thank you for your help and your time.

Santi



>
> From: PIKAL Petr 
>To: Santiago Guallar  
>Cc: r-help  
>Sent: Wednesday, July 10, 2013 8:35 AM
>Subject: RE: [R] spped up a function
> 
>
>
> 
>Hi Santiago
> 
>Keep conversation in list. Others can have better ideas.
> 
>I am still messing the reasoning
> 
>Merge seems to me the solution but I am lost in your resoning what to keep and 
>what to discard from resulting object.
> 
>After merge I have this
> 
>result <- structure(list(Ring = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>1L), .Label = c("6106933", "6134701", "6140497", "6140719", "6140756",
>"6140855", "6143070", "6143090", "6143093", "6175711", "6175726",
>"6175730", "6175769", "6175776", "6175784", "6188609", "6188705",
>"6195159", "6195171", "6198153", "6198154", "6198156", "6198157",
>"6198172"), class = "factor"), jul = c(15135, 15135, 15135, 15135,
>15135, 15135, 15135, 15135, 15135, 15135, 15135, 15135, 15135,
>15135, 15135, 15135, 15135, 15135, 15135, 15135, 15135, 15135,
>15135, 15135), timepos = structure(c(1307680575, 1307680740,
>1307681040, 1307681340, 1307681640, 1307681940, 1307682240, 1307682540,
>1307682780, 1307683080, 1307683380, 1307683680, 1307683980, 1307684280,
>1307684397, 1307684424, 1307684484, 1307684490, 1307684580, 1307684880,
>1307685180, 1307685243, 1307685321, 1307685336), class = c("POSIXct",
>"POSIXt"), tzone = "GMT"), act = c(3822L, NA, NA, NA, NA, NA,
>NA, NA, NA, NA, NA, NA, NA, NA, 27L, 60L, 6L, 753L, NA, NA, NA,
>78L, 15L, 18L), wd = c("dry", NA, NA, NA, NA, NA, NA, NA, NA,
>NA, NA, NA, NA, NA, "wet", "dry", "wet", "dry", NA, NA, NA, "wet",
>"dry", "wet")), .Names = c("Ring", "jul", "timepos", "act", "wd"
>), row.names = c(NA, -24L), class = "data.frame")
> 
>> result
>      Ring   jul             timepos  act   wd
>1  6106933 15135 2011-06-10 04:36:15 3822  dry
>2  6106933 15135 2011-06-10 04:39:00   NA 
>3  6106933 15135 2011-06-10 04:44:00   NA 
>4  6106933 15135 2011-06-10 04:49:00   NA 
>5  6106933 15135 2011-06-10 04:54:00   NA 
>6  6106933 15135 2011-06-10 04:59:00   NA 
>7  6106933 15135 2011-06-10 05:04:00   NA 
>8  6106933 15135 2011-06-10 05:09:00   NA 
>9  6106933 15135 2011-06-10 05:13:00   NA 
>10 6106933 15135 2011-06-10 05:18:00   NA 
>11 6106933 15135 2011-06-10 05:23:00   NA 
>12 6106933 15135 2011-06-10 05:28:00   NA 
>13 6106933 15135 2011-06-10 05:33:00   NA 
>14 6106933 15135 2011-06-10 05:38:00   NA 
>15 6106933 15135 2011-06-10 05:39:57   27  wet
>16 6106933 15135 2011-06-10 05:40:24   60  dry
>17 6106933 15135 2011-06-10 05:41:24    6  wet
>18 6106933 15135 2011-06-10 05:41:30  753  dry
>19 6106933 15135 2011-06-10 05:43:00   NA 
>20 6106933 15135 2011-06-10 05:48:00   NA 
>21 6106933 15135 2011-06-10 05:53:00   NA 
>22 6106933 15135 2011-06-10 05:54:03   78  wet
>23 6106933 15135 2011-06-10 05:55:21   15  dry
>24 6106933 15135 2011-06-10 05:55:36   18  wet
> 
>I understand you want to keep only time values from GPL data.frame. OK this 
>can be done in the last step. But I am a bit lost in the logic for discarding 
>lines 15-18. Anyway, this can be what you want
> 
>library(zoo)
>result$wd<-na.locf(result$wd)
>final<-result[is.na(result$act),]
>> final
>      Ring   jul             timepos act  wd
>2  6106933 15135 2011-06-10 04:39:00  NA dry
>3  6106933 15135 2011-06-10 04:44:00  NA dry
>4  6106933 15135 2011-06-10 04:49:00  NA dry
>5  6106933 15135 2011-06-10 04:54:00  NA dry
>6  6106933 15135 2011-06-10 04:59:00  NA dry
>7  6106933 15135 2011-06-10 05:04:00  NA dry
>8  6106933 15135 2011-06-10 05:09:00  NA dry
>9  6106933 15135 2011-06-10 05:13:00  NA dry
>10 6106933 15135 2011-06-10 05:18:00  NA dry
>11 6106933 15135 2011-06-10 05:23:00  NA dry
>12 6106933 15135 2011-06-10 05:28:00  NA dry
>13 6106933 15135 2011-06-10 05:33:00  NA dry
>14 6106933 15135 2011-06-10 05:38:00  NA dry
>19 6106933 15135 2011-06-10 05:43:00  NA dry
>20 6106933 15135 2011-06-10 05:48:00Â

[R] Cramer von Mises test for a discrete distribution

2013-02-19 Thread Santiago Guallar
Hi,
 
I'm trying to carry out Cramer von Mises tests between pairs of vectors 
belonging to a discrete distribution (concretely frequencies from 0 to 200). 
However, the program crashes in the attempt. The problem seems to be that these 
vectors only have positive integer numbers (+ zero). When I add a random very 
small positive decimal to the non-decimal part everything works fine (files 
prm1 & prpmr1). I attach two of these vectors so you can run the following 
code. I've also thought to divide both vectors by a real constant such as pi. 
Do you think these two approaches are acceptable?
 
setwd("")
require(CvM2SL2Test)
prm = scan('prm.txt')
prpmr = scan('prpmr.txt')
ct1 = cvmts.test(prm, prpmr) # here R crashes
ct1
cvmts.pval( ct1, length(prm), length(prpmr) )
 
 
Thank you for your help,
 
Santi199.09  
193.59  
199.99  
173.89  
179.99  
200.89  
198.89  
200.09  
186.59  
171.79  
118.79  
44.19   
155.79  
200.49  
201.29  
199.99  
201.09  
201.19  
200.39  
200.59  
201.19  
200.79  
201.09  
201.29  
200.79  
200.99  
201.19  
200.99  
201.49  
201.39  
200.99  
199.99  
201.29  
201.39  
201.19  
164.89  
200.79  
200.29  
201.49  
200.99  
198.99  
198.29  
200.09  
201.09  
194.29  
189.49  
170.29  
173.99  
23.99   
200.39  
200.49  
200.79  
151.19  
201.09  
131.79  
25.39   
0.29
0.69
1.39
170.49  
200.59  
201.89  
200.89  
201.19  
201.29  
200.99  
197.89  
200.89  
185.09  
166.69  
172.59  
185.99  
201.59  
201.59  
184.49  
200.99  
201.99  
200.19  
200.69  
201.19  
201.89  
197.29  
201.29  
200.39  
201.69  
200.39  
140.99  
200.69  
200.49  
201.69  
197.39  
198.79  
201.09  
200.39  
201.09  
200.79  
201.39  
200.89  
201.29  
201.39  
201.49  
201.29  
150.39  
178.29  
199.29  
201.49  
200.69  
201.29  
200.69  
182.29  
200.99  
201.19  
200.99  
201.59  
136.69  
200.59  
200.89  
201.89  
200.39  
201.49  
184.29  
185.49  
187.09  
124.29  
187.19  
64.19   
65.19   
201.39  
201.09  
201.39  
201.89  
175.39  
200.19  
200.79  
200.99  
200.19  
201.19  
201.19  
201.19  
201.59  
200.69  
200.79  
201.29  
201.69  
193.49  
197.99  
118.19  
115.89  
95.29   
92.89   
201.29  
200.99  
201.09  
201.59  
201.49  
193.99  
179.59  
198.39  
139.09  
36.09   
0.99
1.49
141.39  
174.89  
169.19  
128.59  
3.59
18.19   
0.89
0.69
0.49
1.39
59.89   
1.69
135.09  
186.99  
185.49  
190.19  
19.79   
5.79
1.19
1.19
1.49
102.09  
200.79  
201.39  
201.09  
193.19  
201.49  
180.59  
0.69
189.09  
98.79   
123.69  
119.09  
148.19  
179.69  
185.29  
199.09  
195.59  
197.99  
200.09  
172.19  
198.59  
199.99  
164.69  
201.39  
200.59  
198.39  
175.69  
200.59  
192.09  
143.49  
142.39  
196.79  
191.69  
200.99  
200.79  
200.89  
193.39  
193.39  
201.59  
201.39  
200.39  
200.89  
200.79  
195.39  
200.49  
200.99  
200.69  
201.49  
201.49  
200.59  
201.29  
200.59  
200.79  
200.79  
201.69  
200.99  
201.39  
201.09  
201.39  
200.49  
201.49  
201.09  
201.29  
200.3

Re: [R] Cramer von Mises test for a discrete distribution

2013-02-20 Thread Santiago Guallar
Thanks Barry,
 
Following your list order
1) It pops up a window saying R for windows GUI front-end crashed. Below three 
options: look for on-line solutions; shut down the program; debug the program 
(I'm translating from Spanish)
2) The processor of my laptop is an Intel Core duo 1,60GHz with ram= 4 GB, 32 
bits. The R version I have installed is 2.15.2 (2012-10-26)
3) I read the posting-guide. Ok, it may be basic statistics. Question withdrawn
 
I made an additional mistake: I attached the wrong files. Please run the code 
with these, and you'll see the problem.
 
Santi
 
 

From: Barry Rowlingson 
>To: Santiago Guallar  
>Cc: "r-help@r-project.org"  
>Sent: Tuesday, February 19, 2013 6:20 PM
>Subject: Re: [R] Cramer von Mises test for a discrete distribution
>
>On Tue, Feb 19, 2013 at 2:49 PM, Santiago Guallar  wrote:
>> Hi,
>>
>> I'm trying to carry out Cramer von Mises tests between pairs of vectors 
>> belonging to a discrete distribution (concretely frequencies from 0 to 200). 
>> However, the program crashes in the attempt. The problem seems to be that 
>> these vectors only have positive integer numbers (+ zero). When I add a 
>> random very small positive decimal to the non-decimal part everything works 
>> fine (files prm1 & prpmr1). I attach two of these vectors so you can run the 
>> following code. I've also thought to divide both vectors by a real constant 
>> such as pi. Do you think these two approaches are acceptable?
>>
>> setwd("")
>> require(CvM2SL2Test)
>> prm = scan('prm.txt')
>> prpmr = scan('prpmr.txt')
>> ct1 = cvmts.test(prm, prpmr) # here R crashes
>
>For you maybe. For me, works fine, and:
>
>> ct1
>
>[1] 30.20509
>
>> cvmts.pval( ct1, length(prm), length(prpmr) )
>
>- this is taking a bit longer. I gave up and killed it. Maybe it
>would have eventually "crashed R", but you said the other function
>call crashed R.
>
>Your two mistakes are:
>
>1. Saying "R crashes" without showing us any kind of crash report or
>error message.
>2. Not listing your system and package versions.
>
>Ah, your three mistakes are...
>
>3. Not reading http://www.r-project.org/posting-guide.html
>
>
>Barry
>
>
>198
193
200
173
179
200
198
199
185
171
118
44
154
200
200
199
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
163
200
200
200
200
198
197
200
200
193
189
170
173
23
200
200
200
150
200
131
25
0
0
0
169
200
200
200
200
200
200
197
200
184
166
172
185
200
200
184
200
200
200
200
200
200
197
200
200
200
200
140
200
200
200
197
198
200
200
200
200
200
200
200
200
200
200
150
177
198
200
200
200
200
181
200
200
200
200
136
200
200
200
200
200
183
185
186
123
186
63
64
200
200
200
200
174
200
200
200
200
200
200
200
200
200
200
200
200
193
197
117
115
94
92
200
200
200
200
200
193
179
198
138
35
0
0
141
174
168
127
3
17
0
0
0
0
59
0
134
186
184
190
18
5
0
0
0
101
200
200
200
192
200
180
0
188
98
123
118
147
179
184
198
194
197
199
171
198
199
163
200
199
198
175
200
191
142
141
196
191
200
200
200
192
193
200
200
200
200
200
194
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
196
200
196
200
200
21
9
85
11
4
25
76
200
195
74
77
7
176
183
173
150
41
186
200
200
196
4
0
2
4
0
0
40
200
200
200
200
200
200
200
139
72
200
200
200
200
192
200
200
200
200
200
161
200
200
186
200
200
191
191
183
111
7
0
200
171
20
200
200
200
200
194
187
191
181
94
0
200
195
199
200
200
193
191
200
200
199
200
199
200
200
192
165
186
177
200
200
192
3
0
0
200
168
200
200
195
199
200
200
200
200
200
200
200
200
200
200
197
200
200
200
200
200
200
200
165
199
162
145
123
168
200
200
200
200
200
93
1
95
0
0
0
171
18
0
0
0
188
200
26
200
200
67
184
160
53
0
0
0
0
0
200
200
166
119
95
63
0
0
0
1
106
41
200
200
200
200
200
198
200
200
200
200
200
93
176
195
128
142
19
63
200
200
152
0
0
1
188
188
198
189
174
196
200
70
0
0
0
0
40
2
200
168
19
0
0
0
0
0
0
0
0
0
0
0
165
190
135
199
200
12
0
0
126
200
200
200
200
200
200
200
197
200
175
0
179
0
129
193
184
161
176
177
0
0
0
0
176
163
186
140
183
171
178
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
194
139
31
197
106
161
68
0
0
0
0
0
200
200
193
167
200
200
200
199
200
200
196
200
__
R-help@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.


Re: [R] Cramer von Mises test for a discrete distribution

2013-02-20 Thread Santiago Guallar
Great Barry!
Thanks for your time. I will e-mail the package maintainers.
 
Santi

From: Barry Rowlingson 
>To: Santiago Guallar  
>Cc: "r-help@r-project.org"  
>Sent: Wednesday, February 20, 2013 1:36 PM
>Subject: Re: [R] Cramer von Mises test for a discrete distribution
>
>On Wed, Feb 20, 2013 at 10:03 AM, Santiago Guallar  wrote:
>> Thanks Barry,
>>
>> Following your list order
>> 1) It pops up a window saying R for windows GUI front-end crashed. Below
>> three options: look for on-line solutions; shut down the program; debug the
>> program (I'm translating from Spanish)
>
>That's good - often people say "crash" when all they have seen is a
>"stop" from R.
>
>> 2) The processor of my laptop is an Intel Core duo 1,60GHz with ram= 4 GB,
>> 32 bits. The R version I have installed is 2.15.2 (2012-10-26)
>
>Nicely up to date...
>
>> 3) I read the posting-guide. Ok, it may be basic statistics. Question
>> withdrawn
>
>Oh don't do that! You're not asking how to do basic statistics, you
>are trying to do it yourself and getting a crash. Fair question for
>starters...
>
>> I made an additional mistake: I attached the wrong files. Please run the
>> code with these, and you'll see the problem.
>
>Will I, will I, will I
>
>> ct1 = cvmts.test(prm, prpmr) # here R crashes
>
>*** caught segfault ***
>address 0x5620e458, cause 'memory not mapped'
>
>Traceback:
>1: .C("CvMTestStat", as.double(x), as.integer(length(x)),
>as.double(y),    as.integer(length(y)), testscore = double(1))
>2: cvmts.test(prm, prpmr)
>
>Possible actions:
>1: abort (with core dump, if enabled)
>2: normal R exit
>3: exit R without saving workspace
>4: exit R saving workspace
>Selection:
>
>Yes! This looks like a bug in that package function, a bit of
>investigation seems to blame it on when you have repeated values in
>the vectors:
>
>> cvmts.test(1:10,1:10)
>[1] 0.025
>> cvmts.test(rep(1,10),rep(1,10))
>[1] 0.955
>> cvmts.test(rep(1,10),rep(2,10))
>
>*** caught segfault ***
>address 0x514daba8, cause 'memory not mapped'
>
>Traceback:
>1: .C("CvMTestStat", as.double(x), as.integer(length(x)),
>as.double(y),    as.integer(length(y)), testscore = double(1))
>2: cvmts.test(rep(1, 10), rep(2, 10))
>
>Possible actions:
>1: abort (with core dump, if enabled)
>2: normal R exit
>3: exit R without saving workspace
>4: exit R saving workspace
>
>Functions shouldn't crash like this - so time for you to email the maintainer:
>
>> packageDescription("CvM2SL2Test")$Maintainer
>[1] "Yuanhui Xiao "
>
>The function disappears into C code, but I suspect its dividing by
>zero somewhere...
>
>Barry
>
>
>
[[alternative HTML version deleted]]

__
R-help@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.


[R] modify and append new rows in a dataframe

2013-06-08 Thread Santiago Guallar
My data frame shows changes on the variable act which records the consecutive 
duration (in seconds) of two states (wet-dry) over a few days for several 
individuals (identified by Ring). Since I want to work with daytime (i.e. from 
dawn till dusk) and night time (i.e. from dusk till next dawn), I have to split 
act in two: from time[i] till dusk and from dusk until time[i+1], and from 
time[k] till dawn and from dawn until time[k+1].
Example:

ith row: 01-01-2000 20:55:00 act= 360 seconds

i+1th row: 01-01-2000 21:01:00 act= 30 seconds # say that dusk= 21:00

i+2th row: 01-01-2000 21:01:30 act= 30 seconds

.

.
.
My goal is to get:

ith row: 01-01-2000 20:55:00 act= 300 seconds # modified row

i+1th row: 01-01-2000 21:00:00 act= 60 seconds # new row

i+2th row: 01-01-2000 00:01:00 act= 30 seconds # previously row i+1th

i+3th row: 01-01-2000 00:01:30 act= 30 seconds # previously row i+2th

.

.
.
I attach a dput with a selection of my data. Here's a piece of code that I am 
trying to run only for the daytime/night time change:

  xandn <- ddply( xan, .(Ring), function(df1){
  # índex of daytime/night time changes
  ind <- c( FALSE, diff( as.POSIXlt( df1$timepos, df1$dusk ) ) > 0 )
  add <- df1[ind,]
  add$timepos <- add$timepos - add$dusk
  # append and arrange rows
  df1 <- rbind( df1, add )
  df1 <- df1[order(df1$timepos),]
  # recalculation of act
  df1$act2 <- c( diff( as.numeric(df1$timepos) ), NA )
  df1} )

I get the following error message:
"Error in diff(as.POSIXlt(df1$timepos, df1$dusk)): error in evaluating the 
argument 'x' in selecting a method for function 'diff': Error in 
as.POSIXlt.POSIXct (df1$timepos, df1$dusk):   invalid 'tz' value"

Thank you for your hep,

Santi__
R-help@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.


[R] modify and append new rows to a data.frame using ddply

2013-06-10 Thread Santiago Guallar
Hi,

I have a data.frame that contains a variable act which records the duration (in 
seconds) of two states (wet-dry) for several individuals (identified by Ring) 
over a period of time. Since I want to work with daytime (i.e. from sunrise 
till sunset) and night time (i.e. from sunset till next sunrise), I have to 
split act from time[i] till sunset and from sunset until time[i+1], and from 
time[k] till sunrise and from sunrise until time[k+1].

Here is an example with time and act separated by a comma:

[i] 01-01-2000 20:55:00 , 360 
[i+1] 01-01-2000 21:01:00 , 30 # let's say that sunset is at 01-01-2000 21:00:00

[i+2] 01-01-2000 21:01:30 , 30

.
.
.
My goal is to get:
[i] 01-01-2000 20:55:00 , 300 # act is modified

[i+1] 01-01-2000 21:00:00 , 60 # new row with time=sunset

[i+2] 01-01-2000 21:01:00 , 30 # previously row i+1th

[i+3] 01-01-2000 21:01:30 , 30 # previously row i+2th

.
.
.
I attach a dput with a selection of my data.frame. Here is a piece of existing 
code that I am trying to adapt just for the daytime/night time change:

  require(plyr)

  xandaynight <- ddply( xan, .(Ring), function(df1){
  # index of day/night changes
  ind <- c( FALSE, diff(df$dif) == 1 )
  add <- df1[ind,]
  add$timepos <- add$dusk
  # rearrangement
  df1 <- rbind( df1, add )
  df1 <- df1[order(df1$timepos),]
  # recalculation of act
  df1$act2 <- c( diff( as.numeric(df1$timepos) ), NA )
  df1} )

This code produces an error message:
"Error en diff(df$dif): error in evaluating the argument 'x' in selecting a 
method for function 'diff': Error en df$dif: object of type 'closure' is not a 
subset"

Thank you for your help,

Santi__
R-help@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.


[R] spped up a function

2013-07-02 Thread Santiago Guallar
Hi,

I have written a function to assign the values of a certain variable 'wd' from 
a dataset to another dataset. Both contain data from the same time period but 
differ in the length of their time intervals: 'GPS' has regular 10-minute 
intervals whereas 'xact' has irregular intervals. I attached simplified text 
versions from write.table. You can also get a dput of 'xact' in this address: 
http://www.megafileupload.com/en/file/431569/xact-dput.html).
The original objects are large and the function takes almost one hour to finish.
Here's the function:

fxG= function(xact, GPS){
l <- rep( 'A', nrow(GPS) )
v <- unique(GPS$Ring) # the process is carried out for several individuals 
identified by 'Ring'
for(k in 1:length(v) ){
I = v[k]
df <- xact[xact$Ring == I,]
for(i in 1:nrow(GPS)){
if(GPS[i,]$Ring== I){# the code runs along the whole data.frame for each i; 
it'd save time to make it stop with the last record of each i instead
u <- df$timepos <= GPS[i,]$timepos
# fill vector l for each interval t from xact <= each interval from GPS (take 
the max if there's > 1 interval)
l[i] <- df[max( which(u == TRUE) ),]$wd
}
}
}
return(l)}

vwd <- fxG(xact, GPS)


My question is: how can I speed up (optimize) this function?

Thank you for your help__
R-help@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.


[R] inserting jpg

2012-12-07 Thread Santiago Guallar
I’m trying to insert 8 jpg files with the main moon phases in a plot using 
read.image. My code:
 
## Images
 
library(ReadImages)
c1 = read.jpeg( '1c.jpg' )
c2 = read.jpeg( '2c.jpg' )
c3 = read.jpeg( '3c.jpg' ) 
d1 = read.jpeg( '1d.jpg' ) 
d2 = read.jpeg( '2d.jpg' ) 
d3 = read.jpeg( '3d.jpg' ) 
f = read.jpeg( 'f.jpg')
n = read.jpeg( 'n.jpg' ) 
 
## Conditions: I want to insert the images given these conditions, and then 
center each image for h== 0 (that is inserting only one image for midnight, 
otherwise I'd get 24 images together)
 
fri1 <- function(x){ plot( ifelse( I2$image == "f" && I2$h == 0, f, NA )) }
fri2 <- function(x){ plot( ifelse( I2$image == "n" && I2$h == 0, n, NA )) }
fri3 <- function(x){ plot( ifelse( I2$image == "2c" && I2$h == 0, c2, NA )) }
fri4 <- function(x){ plot( ifelse( I2$image == "2d" && I2$h == 0, d2, NA )) }
fri5 <- function(x){ plot( ifelse( I2$image == "3c" && I2$h == 0, c3, NA )) }
fri6 <- function(x){ plot( ifelse( I2$image == "3d" && I2$h == 0, d3, NA )) }
fri7 <- function(x){ plot( ifelse( I2$image == "1d" && I2$h == 0, d1, NA )) }
fri8 <- function(x){ plot( ifelse( I2$image == "1c" && I2$h == 0, c1, NA )) }
 
## Combined plot
 
dev.new( width = 16, height = 8 )
par( fig = c( 0, .95, 0.625, 1), cex= 0.9 )
plot( I2$act, type="n", xlab = "", ylab = "", ylim = 10 )
I4 <- apply( r1, 1, fri1 )
I5 <- apply( r1, 1, fri2 )
I6 <- apply( r1, 1, fri3 )
I7 <- apply( r1, 1, fri4 )
I8 <- apply( r1, 1, fri5 )
I9 <- apply( r1, 1, fri6 )
I10 <- apply( r1, 1, fri7 )
I11 <- apply( r1, 1, fri8 )
par( fig = c( 0, .95, 0, 0.875), new = T )
plot( I2$act, type = "n", ylab = "activity", xlab = "day" )
lines( I2$act, col = "red", lwd = 1.5 )
 
R returns errors stating that finite values of ylim are needed, also returning 
warnings on min(x) and max(x). Is it just a problem of the size of the inserted 
jpg file relative to the window frame?
 
Attached a dput with my data frame (I2) and the jpg files.
 
Thank you for your help,
 
Santi Guallar__
R-help@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.


[R] zero-inflated negative binomial model with random effects

2014-01-04 Thread Santiago Guallar
Hi,
I'd like to incorporate a random intercept in a zero-inflated negative binomial 
model. Package pscl is great but does not allow random effects, and package 
MCMCglmm doesn't support the negative binomial. I'm aware that package glmmADMB 
supported both random effects and the negative binomial distribution but is not 
available anylonger.

Any suggestions?

Santi Guallar
[[alternative HTML version deleted]]

__
R-help@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.


[R] Automated generation of combinations

2011-09-12 Thread Santiago Guallar
Hello,
 
I'd like to generate automatically all the possible combinations of a set of 8 
variables (there are 535, too many to do it by hand). For example:
 
input: varA, varB, varC
output: varA+varB+varC
varA+varB
varA+varC
varB+varC
varA
varB
varC
Is there any function that produces this option?
 
Thank you
[[alternative HTML version deleted]]

__
R-help@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.


[R] Imposing more than one condition to if

2012-07-15 Thread 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 1210 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@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.


Re: [R] Imposing more than one condition to if

2012-07-17 Thread 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 
>To: Santiago Guallar  
>Cc: r-help@r-project.org 
>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 
>>>    *To:* Santiago Guallar 
>>>    *Cc:* r-help@r-project.org
>>>    *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):
>>&

Re: [R] Imposing more than one condition to if

2012-07-17 Thread 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 
>To: Santiago Guallar  
>Cc: r-help@r-project.org 
>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 
>>    *To:* Santiago Guallar 
>>    *Cc:* r-help@r-project.org
>>    *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  

Re: [R] Imposing more than one condition to if

2012-07-18 Thread 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 17:00:00) {zrle <- rle(x$lig == 0)}
 
 
Santi

From: Rui Barradas 
>To: Santiago Guallar  
>Cc: r-help@r-project.org 
>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 
>>    *To:* Santiago Guallar 
>>    *Cc:* r-help@r-project.org
>>    *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 

Re: [R] Imposing more than one condition to if

2012-07-18 Thread Santiago Guallar
Great! Thank you ever so much Rui.

Santi



>
> From: Rui Barradas 
>To: Santiago Guallar  
>Cc: r-help@r-project.org 
>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 17:00:00) {zrle <- rle(x$lig == 0)}
>> Santi
>>
>>     *From:* Rui Barradas 
>>     *To:* Santiago Guallar 
>>     *Cc:* r-help@r-project.org
>>     *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
>

[R] Importing files

2012-03-18 Thread Santiago Guallar
Hello,
I'm trying to import into R files that contain data downloaded from logger 
devices as files with the following formats:
.act
.lig
.trj
.trn
These files are essentially text files but use both tabs and commas as 
separators.
I've tried the function scan:

1) scan("filename.act", what=character(0)) returns only two columns from the 
original 5
2) scan("copia.act", what=character(0),sep=",") returns three columns but puts 
the original fifth one in the next row

Attached a sample file with five fields. How can I get one field per column?

Thank you for your help
__
R-help@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.


Re: [R] Subtract days to dates in POSIXct format

2012-04-30 Thread Santiago Guallar
Thanks a lot, Jeff

Yes, I applied colsplit in package reshape. Sorry, I forgot to state it in the 
piece of code I attached.
Actually, these are archival data from a geolocator attached to a seabird. The 
origin lies on Gran Canaria and data were gathered all along its migratory 
journey for one full year, from the Canary Islands all the way to the South 
Atlantic Ocean and back.
Variable lig, which stands for light, indicates daylight (aprox lig>2) or its 
absence (lig<2), so I know when it is nighttime or daytime for sure. However, 
I'd like to compare it with the output of the maptools functions. 
If I understood well maptools functions use astronomical algorithms to 
calculate sunrise and sunset based on one location and one POSIXct time. If I 
changed your invented coordinates and time for the real ones in my dataset 
would do the job correctly? When I put the aproximate coordinates it yields an 
incorrect output:
site <- SpatialPoints( matrix(c(27.82, -15.77), 
nrow=1),proj4string=CRS("+proj=longlat +datum=WGS84")) # didn't touch the rest 
of your code

But I'm probably doing something wrong.

Santi 




>____
> From: Jeff Newmiller 
>To: Santiago Guallar  
>Cc: "r-help@r-project.org"  
>Sent: Monday, April 30, 2012 5:51 PM
>Subject: Re: [R] Subtract days to dates in POSIXct format
> 
>On Mon, 30 Apr 2012, Santiago Guallar wrote:
>
>> Hello,
>> 
>> I'm having problems working with date values in POSIXct format.
>
>Indeed you are.
>
>> Here is what I got (eg.lig attached):
>> 
>> x <- read.table("eg.txt", sep = ',', col.names=c("ok","time","secs","lig")) 
>> # it gives time as factor
>> z <- cbind(x,colsplit(x$time, split="\\s", names=c("date", "clock")))
>
>colsplit is not defined in base R, and it is redefined in a couple of 
>packages.  I am guessing you mean the one that is in the reshape package.
>
>> zh<-cbind(z,colsplit(z$clock,split=":",names=c("h","m","s")))
>> zn <- subset(zh, zh$lig<= 5 & zh$h<8 | zh$h>=22) # nighttime
>> zn$timepos<-as.POSIXct(zn$time,tz="GMT",format="%d/%m/%y %H:%M:%S")
>
>> I want to assign timepos to its ?natural? night, that is, the night that 
>> goes from sunset of day x to sunrise of day x+1 corresponds to day x:
>
>You are working here with GMT, but there are many other possible timezones 
>that this data could correspond to, and if the input time really is in GMT, 
>then the interval of night could be anytime.
>
>> 
>> zn$night<-ifelse(zn$h>=0 & zn$h<9,zn$timepos ??oneday?,zn$timepos)  #doesn?t 
>> work
>> 
>> How can I subtract one day (24 hours) to zn$timepos and obtain a POSIXct 
>> output?
>
>Well, a literal answer to your question is:
>
>zn$timepos - as.difftime( 1, units="days" )
>
>but in response to your stated goal perhaps you should study the following:
>
>library(maptools)
>Sys.setenv( TZ="GMT" ) # set to the local timezone for column "time"
>dta <- read.table( "eg.txt"
>                 , sep=','
>                 , col.names=c("ok","time","secs","lig")
>                 , stringsAsFactors=FALSE)
>dta$dtm <- as.POSIXct( dta$time, format="%d/%m/%y %T" ) # depends on TZ
># site is invented in GMT timezone for illustration
>site <- SpatialPoints( matrix( 0, 51.5, nrow=1 )
>                     , proj4string=CRS("+proj=longlat +datum=WGS84" ) )
>dta$rise <- sunriset( site
>                    , dta$dtm
>                    , direction="sunrise"
>                    , POSIXct.out=TRUE )$time
>dta$set <- sunriset( site
>                   , dta$dtm
>                   , direction="sunset"
>                   , POSIXct.out=TRUE )$time
>dta$isnight <- with( dta, !( rise < dtm & dtm < set ) )
>
>
>---
>Jeff Newmiller                        The     .       .  Go Live...
>DCN:        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
>Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>---
>
>
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Subtract days to dates in POSIXct format

2012-05-01 Thread Santiago Guallar
Yes, sorting longitude and latitude correctly solves the problem! However, this 
only works near the Canaries. When selecting the January data (probable 
location: South African waters) sunrise time (but not sunset time) for the 
given coordinates is about 4 hours later than the local sunrise.

Sys.setenv( TZ="GMT" ) # set to the local timezone for column "time"
dta 
<-read.table("eg.txt",sep=',',col.names=c("ok","time","secs","lig"),stringsAsFactors=FALSE)
dta$dtm <- as.POSIXct(dta$time, format="%d/%m/%y %T" ) # depends on TZ
site <- SpatialPoints( matrix(c(-15.77, 27.82), 
nrow=1),proj4string=CRS("+proj=longlat +datum=WGS84")) # Canary Is
dta$rise <- sunriset(site, dta$dtm, direction="sunrise",POSIXct.out=TRUE)$time
dta$set <- sunriset( site, dta$dtm, direction="sunset", POSIXct.out=TRUE )$time
dta$isnight <- with( dta, !( rise < dtm & dtm < set ) )

A=cbind(dta,colsplit(dta$time, split = "\\s", names = c("date", "clock")))
B=cbind(A,colsplit(A$date, split = "/", names = c("d", "m", "y")))
C<-subset(B,B$m==1) # January data; the bird was in South Atlantic waters
write.table(C)

Thank you very much for your help,

Santi



>
> From: Jeff Newmiller 
>To: Santiago Guallar  
>Cc: "r-help@r-project.org"  
>Sent: Tuesday, May 1, 2012 1:14 AM
>Subject: Re: [R] Subtract days to dates in POSIXct format
> 
>The sunrise and sunset are calculated for each time value on the input vector, 
>but all times are treated in the TZ timezone. I can see that the Canary 
>Islands are in the GMT timezone, so the example TZ is right.
>
>I can see that you entered the latitude and longitude backward (longitude is 
>"x"), but I am not able to look at your data at the moment so I don't know 
>whether that fixes your problem. Maptools assumes one location Depending on 
>where the bird went in the South Atlantic, the assumed latitude could be 
>several hours wrong, though. Also, there is dependence of length of day on 
>latitude (made more noticeable by crossing the equator). 
>---
>Jeff Newmiller                        The     .       .  Go Live...
>DCN:        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
>Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>--- 
>Sent from my phone. Please excuse my brevity.
>
>Santiago Guallar  wrote:
>
>>Thanks a lot, Jeff
>>
>>Yes, I applied colsplit in package reshape. Sorry, I forgot to state it
>>in the piece of code I attached.
>>Actually, these are archival data from a geolocator attached to a
>>seabird. The origin lies on Gran Canaria and data were gathered all
>>along its migratory journey for one full year, from the Canary Islands
>>all the way to the South Atlantic Ocean and back.
>>Variable lig, which stands for light, indicates daylight (aprox lig>2)
>>or its absence (lig<2), so I know when it is nighttime or daytime for
>>sure. However, I'd like to compare it with the output of the maptools
>>functions. 
>>If I understood well maptools functions use astronomical algorithms to
>>calculate sunrise and sunset based on one location and one POSIXct
>>time. If I changed your invented coordinates and time for the real ones
>>in my dataset would do the job correctly? When I put the aproximate
>>coordinates it yields an incorrect output:
>>site <- SpatialPoints( matrix(c(27.82, -15.77),
>>nrow=1),proj4string=CRS("+proj=longlat +datum=WGS84")) # didn't touch
>>the rest of your code
>>
>>But I'm probably doing something wrong.
>>
>>Santi 
>>
>>
>>
>>
>>>
>>> From: Jeff Newmiller 
>>>To: Santiago Guallar  
>>>Cc: "r-help@r-project.org"  
>>>Sent: Monday, April 30, 2012 5:51 PM
>>>Subject: Re: [R] Subtract days to dates in POSIXct format
>>> 
>>>On Mon, 30 Apr 2012, Santiago Guallar wrote:
>>>
>>>> Hello,
>>>> 
>>>> I'm having problems working with date values in POSIXct format.
>>>
>>>Indeed you are.
>>>
>>>> Here is what I got (eg.lig attached):
>>>> 
>>>> x <- read.table("eg.txt", sep = &

Re: [R] creating a new column assigning values of other columns

2012-05-06 Thread Santiago Guallar
Hi Michael,
Yes, I tried ifelse() before but this function returns a numeric value. When I 
try to convert it back to POSIXct I get a *. If I use if else I get a 
POSIXct output although it does not return a correct answer (it only returns 
y$timepos even when the condition "h<9" fails to be met).
Attached the outputs you suggested (thanks, by the way, I didn't know dput()). 
Hope they go through this time.
 
Thank you,
 
Santi
 
*niga$isnight<- as.POSIXct( niga$nit, tz="GMT", format="%Y-%m-%d %H:%M:%S", 
origin="2007-06-19" )
 


From: R. Michael Weylandt 
>To: Santiago Guallar  
>Cc: "r-help@r-project.org"  
>Sent: Sunday, May 6, 2012 2:18 AM
>Subject: Re: [R] creating a new column assigning values of other columns
>
>Ba -- far too much work to recreate (and I don't think you sent us
>the file "act.lig"): here's a much better route:
>
>Go to the step immediately before you're in trouble and use dput() on
>your data. R will print out a nice plaintext representation that we
>can copy and paste and reproduce *exactly* without having to do all
>that you show below.
>
>Incidentally, your warning message suggests you should be using
>ifelse() instead of if.
>
>To compare:
>
>x <- seq(-3, 3)
>abs.x.wrong <- if(x < 0) -x else x # Warning message gives some hint
>abs.x.right <- ifelse(x < 0, -x, x)
>
>Hope this helps,
>
>Michael
>
>On Sat, May 5, 2012 at 5:09 PM, Santiago Guallar  wrote:
>> Hello,
>>
>> I have to create a new column from the values of other columns of a data 
>> frame. The new column (y$n) is created imposing a condition (using a third 
>> variable y$h) that assigns the values of two time variables (y$b and 
>> y$timepos). Here's the piece of code to get there (using the attached files):
>>
>> xact <- read.table("act.lig", sep = ',', 
>> col.names=c("ok","time","secs","act"))
>> xlig <- read.table("lig.txt", sep = ',', 
>> col.names=c("ok","time","secs","lig"))
>> w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F)
>> require(reshape)
>> z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock")))
>> zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s")))
>> zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y")))
>> night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21)
>> night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y %H:%M:%S")
>> a=night$timepos - as.difftime( 1, units="days" )
>> nighta<-cbind(night,a)
>> y<- cbind(nighta, b=as.character(a, tz= "GMT", format= "%Y-%m-%d"))
>> y$n<-with(y, if (h>=0 & h<9) {b} else {timepos}) ## Missing warnings
>> In
>>  if (h >= 0 & h < 9) { :
>> condition
>>  has length > 1 and only the first element will be used
>>
>> How can I go around this problem and get the new column?
>>
>> Thank you,
>>
>> Santi
>> __
>> R-help@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.
>>
>
>
>

>
>__
R-help@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.


Re: [R] creating a new column assigning values of other columns

2012-05-06 Thread Santiago Guallar
It apparently works now. Something weird happens, though: the new column sums 1 
extra hour and assigns the day according to its own time. To solve it I 
subtracted 1 hour to this new column:
 niga$night<- as.POSIXct ( ifelse(niga$h>=0 & niga$h<9, niga$a, niga$timepos), 
origin="1970-01-01") # it works but adds 1 hour (!!!)
niga$isnight=niga$night - as.difftime ( 1, units= "hours" ) # subtraction of 1 
hour
Thank you very much for your time!
 
Santi

From: R. Michael Weylandt 
>To: Santiago Guallar  
>Cc: "r-help@r-project.org"  
>Sent: Sunday, May 6, 2012 3:13 PM
>Subject: Re: [R] creating a new column assigning values of other columns
>
>It looks like part of your problem is that some of your time/date
>variables are stored as factors rather than actual times / dates. Use
>str() to see which ones and try to convert those. You'll need this
>format string: format = "%d/%m/%y %H:%M:%S" for the ones that are
>currently factors.
>
>As regards getting POSIXct out of ifelse(), yes -- that seems to be an
>infelicity, but I'm  sure its easily remedied. You just need:
>
>as.POSIXct( ifelse( your_code_here) , origin = "1970-01-01")
>
>Don't add a format string since that refers to the format of the input
>(if you're giving a character input), not the output (which is
>standardized in the definition of POSIXct)
>
>Michael
>
>On Sun, May 6, 2012 at 5:13 AM, Santiago Guallar  wrote:
>> Hi Michael,
>> Yes, I tried ifelse() before but this function returns a numeric value. When
>> I try to convert it back to POSIXct I get a *. If I use if else I get a
>> POSIXct output although it does not return a correct answer (it only returns
>> y$timepos even when the condition "h<9" fails to be met).
>> Attached the outputs you suggested (thanks, by the way, I didn't know
>> dput()). Hope they go through this time.
>>
>> Thank you,
>>
>> Santi
>>
>> *niga$isnight<- as.POSIXct( niga$nit, tz="GMT", format="%Y-%m-%d %H:%M:%S",
>> origin="2007-06-19" )
>>
>>
>>
>> From: R. Michael Weylandt 
>> To: Santiago Guallar 
>> Cc: "r-help@r-project.org" 
>> Sent: Sunday, May 6, 2012 2:18 AM
>> Subject: Re: [R] creating a new column assigning values of other columns
>>
>> Ba -- far too much work to recreate (and I don't think you sent us
>> the file "act.lig"): here's a much better route:
>>
>> Go to the step immediately before you're in trouble and use dput() on
>> your data. R will print out a nice plaintext representation that we
>> can copy and paste and reproduce *exactly* without having to do all
>> that you show below.
>>
>> Incidentally, your warning message suggests you should be using
>> ifelse() instead of if.
>>
>> To compare:
>>
>> x <- seq(-3, 3)
>> abs.x.wrong <- if(x < 0) -x else x # Warning message gives some hint
>> abs.x.right <- ifelse(x < 0, -x, x)
>>
>> Hope this helps,
>>
>> Michael
>>
>> On Sat, May 5, 2012 at 5:09 PM, Santiago Guallar  wrote:
>>> Hello,
>>>
>>> I have to create a new column from the values of other columns of a data
>>> frame. The new column (y$n) is created imposing a condition (using a third
>>> variable y$h) that assigns the values of two time variables (y$b and
>>> y$timepos). Here's the piece of code to get there (using the attached
>>> files):
>>>
>>> xact <- read.table("act.lig", sep = ',',
>>> col.names=c("ok","time","secs","act"))
>>> xlig <- read.table("lig.txt", sep = ',',
>>> col.names=c("ok","time","secs","lig"))
>>> w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F)
>>> require(reshape)
>>> z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock")))
>>> zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s")))
>>> zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y")))
>>> night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21)
>>> night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y
>>> %H:%M:%S")
>>> a=night$timepos - as.difftime( 1, units="days" )
>>> nighta<-cbind(night,a)
>>>