[R] Bar plot in R with more than 2 factors

2014-10-10 Thread Franklin Mairura
Please help, it possible to make an r means barplot in R using base, where 
there are more than 2 factors and not with lattice, Franklin. 

[[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] how to break the loop using sapply?

2014-10-10 Thread PO SU

OK,  it  seems that i misunderstand something,  i forget how and when i pick up 
the monition in my mind that " as possible as avoid using for loop".
TKS for all your suggestions!
But i still want the way to break sapply, if not exsits now, create it . 
such as:
 sapply<-function(...){
out<-FALSE
.
if(out==TRUE) return
}
sapply(1:10, function(i){
if(i=5)  out<-TRUE 
}
)
That means to rewrite sapply, and create a virable in it , let called OUT, then 
mayebe in sapply(1:10,FUN)
FUN can use OUT. because i think in sapply,  FUN is an inner function,so it can 
use OUT.
when it let OUT=TRUE. sapply should be break..







--

PO SU
mail: desolato...@163.com 
Majored in Statistics from SJTU




At 2014-10-10 14:44:47, "Hervé Pagès"  wrote:
>Hi,
>
>On 10/09/2014 11:12 PM, PO SU wrote:
>>
>>
>> Is that mean while may be more effient than  for in R? as i know, while and 
>> for  are all just functions in R.
>> Tks for your suggestion to not use apply that way, but i want to know, if 
>> possible, is there any way to break it ?
>
>As Jeff said, you cannot break the loop that happens inside
>the sapply() call. Also it is *not* true that for or while are
>less efficient than sapply() or lapply():
>
> > a <- numeric(10)
>
> > system.time(for (i in 1:10) {a[i] <- i * (i - 1) / 2})
>user  system elapsed
>   0.148   0.000   0.147
>
> > system.time(b <- sapply(1:10, function(i) {i * (i - 1) / 2}))
>user  system elapsed
>   0.194   0.007   0.201
>
> > identical(a, b)
>[1] TRUE
>
> > system.time(c <- unlist(lapply(1:10, function(i) {i * (i - 1) / 2})))
>user  system elapsed
>   0.116   0.000   0.119
>
> > identical(a, c)
>[1] TRUE
>
>OK lapply() is maybe slightly faster but not significantly. And the
>more work you need to do inside the loop, the less significant this
>difference will be.
>
>> Actually, there is a additional question:
>>x<- c(3,4,5,6,9)
>>   sapply(x ,function(i){
>> foo(i)  #do something to each value in x,how can i know the i's index in x?
>> )}
>
>You can't. Inside the anonymous function, you only have access to 'i'
>which is an element of 'x', not its index in 'x'.
>
>> In my way , i always
>> sapply(seq(x),function(i){
>> foo(x[i])
>> })
>
>Yes, if you want to loop on the index instead of the elements, you
>need to do something like that. Using seq_along(x) is probably
>cleaner than seq(x) for this.
>
>Cheers,
>H.
>
>> or
>> Map( function(i,index){
>> foo(i)  # through index to know the i's index in x
>> },x ,seq(x))
>>
>> How you solve the problem? I mean just use apply functions.
>>
>>
>>
>> --
>>
>> PO SU
>> mail: desolato...@163.com
>> Majored in Statistics from SJTU
>>
>>
>>
>>
>> At 2014-10-10 13:58:29, "Jeff Newmiller"  wrote:
>>> Don't use apply functions if you want to do what you describe. They don't 
>>> work that way. Use a while control structure.
>>>
>>> ---
>>> Jeff NewmillerThe .   .  Go Live...
>>> DCN:Basics: ##.#.   ##.#.  Live Go...
>>>   Live:   OO#.. Dead: OO#..  Playing
>>> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
>>> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
>>> ---
>>> Sent from my phone. Please excuse my brevity.
>>>
>>> On October 9, 2014 10:24:49 PM PDT, PO SU  wrote:

 Dear expeRts,
 i  use sapply for loop, and i want to break it when i needed, how to
 do that?  e.g.

 sapply( 1:10, function(i) {
 if(i==5) break and jump out of the function sapply
 } )

 I want to do it because i have to loop 100 times, but i don't know
 when it will break, that means, it may need break at i=5 or at i=5,
 for the possible of the  last case, i don't use for loop, because it
 slow(is it right?).
 So,if you happen to know it ,may you help me?


 --

 PO SU
 mail: desolato...@163.com
 Majored in Statistics from SJTU
 __
 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.
>>
>
>-- 
>Hervé Pagès
>
>Program in Computational Biology
>Division of Public Health Sciences
>Fred Hutchinson Cancer Research Center
>1100 Fairview Ave. N, M1-B514
>P.O. Box 19024
>Seattle, WA 98109-1024
>
>E-mail: hpa...@fhcrc.org
>Phone:  (206) 667-5791
>Fax:(206) 66

Re: [R] Bar plot in R with more than 2 factors

2014-10-10 Thread Jim Lemon
On Thu, 9 Oct 2014 11:18:43 PM Franklin Mairura wrote:
> Please help, it possible to make an r means barplot in R using base, 
where
> there are more than 2 factors and not with lattice, Franklin.
> 
Hi Franklin,
Have a look at the barNest function in the plotrix package.

Jim

__
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] Changing date format

2014-10-10 Thread PIKAL Petr
Hi

But It is not doing what you wanted.  It does not extend 365 days year to 366 
days. Instead it will incorrectly rename all days after 28th February in non 
leap years

> format(strptime(59, format = "%j"), "%d-%b")
[1] "28-II"
> format(strptime(60, format = "%j"), "%d-%b")
[1] "01-III"
> format(strptime(61, format = "%j"), "%d-%b")
[1] "02-III"
> day(59)
[1] "28-II"
> day(60)
[1] "29 Feb"
> day(61)
[1] "01-III"

So the effect is that day 61 in 2014 which actually was 2nd of March after your 
function will be named 1st of March. As a result it will remove 31.December in 
non leap year as in that case you do not have 366th day.

I did not follow whole thread but my vague memory reminds me that you want 
change number of days for aligning with some data. I do not believe that 
anybody violates calendar in such a way that all years have 366 days.

I may be mistaken but isn’t ?merge function what you really want?

Petr


From: Frederic Ntirenganya [mailto:ntfr...@gmail.com]
Sent: Friday, October 10, 2014 8:33 AM
To: Duncan Murdoch
Cc: PIKAL Petr; r-help@r-project.org
Subject: Re: [R] Changing date format

Dear All,
The following function gives me what I wanted.
The input of function is one value and this push me to use sapply function to 
call it.

Is there a better way of doing this?
#
day <- function(x){

 if(x== 60) {
   y = "29 Feb"
 }

 if(x < 60){
y =  format(strptime(x, format = "%j"), format ="%d-%b")
}

if(x > 60){
y =  format(strptime(x - 1, format = "%j"), format ="%d-%b")
   }
y
}

sapply(Samaru$Start,day)

 [1] "17-Apr" "27-Apr" "24-Apr" "04-Jun" "25-Apr" "13-May" "22-Apr" "05-May" 
"27-Apr" "13-May" "27-Apr"
#===

On Thu, Oct 9, 2014 at 3:48 PM, Duncan Murdoch 
mailto:murdoch.dun...@gmail.com>> wrote:
On 09/10/2014, 8:27 AM, PIKAL Petr wrote:
> Hi
>
>> -Original Message-
>> From: r-help-boun...@r-project.org 
>> [mailto:r-help-bounces@r-
>> project.org] On Behalf Of Frederic Ntirenganya
>> Sent: Thursday, October 09, 2014 1:32 PM
>> To: Jim Lemon
>> Cc: r-help@r-project.org
>> Subject: Re: [R] Changing date format
>>
>> This idea substract 1 for the all column which is not what i want.
>>
>> We know that the leap years have 366 days. I don't need to change
>> anything on them
>>
>> the non-leap years have 365 days. This is what i want to change to be
>> 366 days rather than 365 days.
>
> But this is a question of calendar reform not R. I must admit it would be a 
> tough job as leap day is needed to stay in tune with solar year. Your 
> suggestion would quickly result in out of sync with the Sun.

R Core is powerful, but I doubt if we could reform the calendar in this
way.

And I'd be against it in any case:  I like to swim on my summer
vacations, but if August slipped into the winter, the lakes around here
would be frozen.

Duncan Murdoch



--
Frederic Ntirenganya
Maseno University,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient o

[R] Count number of Fridays in a month

2014-10-10 Thread Abhinaba Roy
Hi R helpers,

I want to write a function which will

1. Count the number of fridays in the current month ( to extract month from
given date) and also the number of fridays in the preceeding month

2. Calculate the ratio of the number of fridays in current month to the
number of fridays in the precceding month

3. Return a integer value calculated as
ifelse(ratio>1,1,ifesle(ration<1,-1),0)

The date which is passed is in the format *'31-may-2014'*

So, given the date '31-may-2014'

Number of fridays in May2014 = 5
Number of fridays in Apr2014 = 4

Ratio = 5/4 >1
Hence, the function will return a value 1

I want to call the function by passing '31-may-2014' as an argument

How can this be done in R?

Any help will be appreciated

Thanks and regards,
Abhinaba

[[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] Count number of Fridays in a month

2014-10-10 Thread Duncan Murdoch
On 10/10/2014, 7:28 AM, Abhinaba Roy wrote:
> Hi R helpers,
> 
> I want to write a function which will
> 
> 1. Count the number of fridays in the current month ( to extract month from
> given date) and also the number of fridays in the preceeding month
> 
> 2. Calculate the ratio of the number of fridays in current month to the
> number of fridays in the precceding month
> 
> 3. Return a integer value calculated as
> ifelse(ratio>1,1,ifesle(ration<1,-1),0)
> 
> The date which is passed is in the format *'31-may-2014'*
> 
> So, given the date '31-may-2014'
> 
> Number of fridays in May2014 = 5
> Number of fridays in Apr2014 = 4
> 
> Ratio = 5/4 >1
> Hence, the function will return a value 1
> 
> I want to call the function by passing '31-may-2014' as an argument
> 
> How can this be done in R?
> 
> Any help will be appreciated

Convert your string to a POSIXlt object using as.POSIXlt.  Then you can
extract year, month and weekday from the result, and go from there.
(The only unobvious part is figuring out how many days are in each
month, but there are questions online giving various ways to do this.)

Duncan Murdoch

__
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] how to break the loop using sapply?

2014-10-10 Thread Jeff Newmiller
Doing as much as possible with vectors instead of loops of a good thing. 
Fooling yourself that apply functions are vectorized is, well, not a good thing.

If you want to write a function to use instead of sapply, fine, but don't call 
it *apply because those functions always give you one result for each input 
item you start with, and anyone who reads your code will be confused if you 
rename a standard function. I think you need to use ?while.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On October 10, 2014 1:21:23 AM PDT, PO SU  wrote:
>
>OK,  it  seems that i misunderstand something,  i forget how and when i
>pick up the monition in my mind that " as possible as avoid using for
>loop".
>TKS for all your suggestions!
>But i still want the way to break sapply, if not exsits now, create it
>. 
>such as:
> sapply<-function(...){
>out<-FALSE
>.
>if(out==TRUE) return
>}
>sapply(1:10, function(i){
>if(i=5)  out<-TRUE 
>}
>)
>That means to rewrite sapply, and create a virable in it , let called
>OUT, then mayebe in sapply(1:10,FUN)
>FUN can use OUT. because i think in sapply,  FUN is an inner
>function,so it can use OUT.
>when it let OUT=TRUE. sapply should be break..
>
>
>
>
>
>
>
>--
>
>PO SU
>mail: desolato...@163.com 
>Majored in Statistics from SJTU
>
>
>
>
>At 2014-10-10 14:44:47, "Hervé Pagès"  wrote:
>>Hi,
>>
>>On 10/09/2014 11:12 PM, PO SU wrote:
>>>
>>>
>>> Is that mean while may be more effient than  for in R? as i know,
>while and for  are all just functions in R.
>>> Tks for your suggestion to not use apply that way, but i want to
>know, if possible, is there any way to break it ?
>>
>>As Jeff said, you cannot break the loop that happens inside
>>the sapply() call. Also it is *not* true that for or while are
>>less efficient than sapply() or lapply():
>>
>> > a <- numeric(10)
>>
>> > system.time(for (i in 1:10) {a[i] <- i * (i - 1) / 2})
>>user  system elapsed
>>   0.148   0.000   0.147
>>
>> > system.time(b <- sapply(1:10, function(i) {i * (i - 1) / 2}))
>>user  system elapsed
>>   0.194   0.007   0.201
>>
>> > identical(a, b)
>>[1] TRUE
>>
>> > system.time(c <- unlist(lapply(1:10, function(i) {i * (i - 1) /
>2})))
>>user  system elapsed
>>   0.116   0.000   0.119
>>
>> > identical(a, c)
>>[1] TRUE
>>
>>OK lapply() is maybe slightly faster but not significantly. And the
>>more work you need to do inside the loop, the less significant this
>>difference will be.
>>
>>> Actually, there is a additional question:
>>>x<- c(3,4,5,6,9)
>>>   sapply(x ,function(i){
>>> foo(i)  #do something to each value in x,how can i know the i's
>index in x?
>>> )}
>>
>>You can't. Inside the anonymous function, you only have access to 'i'
>>which is an element of 'x', not its index in 'x'.
>>
>>> In my way , i always
>>> sapply(seq(x),function(i){
>>> foo(x[i])
>>> })
>>
>>Yes, if you want to loop on the index instead of the elements, you
>>need to do something like that. Using seq_along(x) is probably
>>cleaner than seq(x) for this.
>>
>>Cheers,
>>H.
>>
>>> or
>>> Map( function(i,index){
>>> foo(i)  # through index to know the i's index in x
>>> },x ,seq(x))
>>>
>>> How you solve the problem? I mean just use apply functions.
>>>
>>>
>>>
>>> --
>>>
>>> PO SU
>>> mail: desolato...@163.com
>>> Majored in Statistics from SJTU
>>>
>>>
>>>
>>>
>>> At 2014-10-10 13:58:29, "Jeff Newmiller" 
>wrote:
 Don't use apply functions if you want to do what you describe. They
>don't work that way. Use a while control structure.


>---
 Jeff NewmillerThe .   .  Go
>Live...
 DCN:Basics: ##.#.   ##.#. 
>Live Go...
   Live:   OO#.. Dead: OO#.. 
>Playing
 Research Engineer (Solar/BatteriesO.O#.   #.O#. 
>with
 /Software/Embedded Controllers)   .OO#.   .OO#. 
>rocks...1k

>---
 Sent from my phone. Please excuse my brevity.

 On October 9, 2014 10:24:49 PM PDT, PO SU 
>wrote:
>
> Dear expeRts,
> i  use sapply for loop, and i want to break it when i needed,
>how to
> do that?  e.g.
>
> sapply( 1:10, function(i) {
> if(i==5) break and jump out of the function sapply
> } )
>
> I want to do it because i have to loop 100 times, but i don't
>know
> when 

Re: [R] Count number of Fridays in a month

2014-10-10 Thread Abhinaba Roy
Hi Duncan,

I have converted the string to a POSIXIt object using

> strptime('31-may-2014',format="%d-%b-%Y")

But could not figure out the way forward.

Could you please elaborate a bit?

On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch 
wrote:

> On 10/10/2014, 7:28 AM, Abhinaba Roy wrote:
> > Hi R helpers,
> >
> > I want to write a function which will
> >
> > 1. Count the number of fridays in the current month ( to extract month
> from
> > given date) and also the number of fridays in the preceeding month
> >
> > 2. Calculate the ratio of the number of fridays in current month to the
> > number of fridays in the precceding month
> >
> > 3. Return a integer value calculated as
> > ifelse(ratio>1,1,ifesle(ration<1,-1),0)
> >
> > The date which is passed is in the format *'31-may-2014'*
> >
> > So, given the date '31-may-2014'
> >
> > Number of fridays in May2014 = 5
> > Number of fridays in Apr2014 = 4
> >
> > Ratio = 5/4 >1
> > Hence, the function will return a value 1
> >
> > I want to call the function by passing '31-may-2014' as an argument
> >
> > How can this be done in R?
> >
> > Any help will be appreciated
>
> Convert your string to a POSIXlt object using as.POSIXlt.  Then you can
> extract year, month and weekday from the result, and go from there.
> (The only unobvious part is figuring out how many days are in each
> month, but there are questions online giving various ways to do this.)
>
> Duncan Murdoch
>
>

[[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] Count number of Fridays in a month

2014-10-10 Thread Duncan Murdoch

On 10/10/2014 8:10 AM, Abhinaba Roy wrote:

Hi Duncan,

I have converted the string to a POSIXIt object using

> strptime('31-may-2014',format="%d-%b-%Y")

But could not figure out the way forward.

Could you please elaborate a bit?


Try this:

?POSIXlt

Duncan Murdoch



On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch 
mailto:murdoch.dun...@gmail.com>> wrote:


On 10/10/2014, 7:28 AM, Abhinaba Roy wrote:
> Hi R helpers,
>
> I want to write a function which will
>
> 1. Count the number of fridays in the current month ( to extract
month from
> given date) and also the number of fridays in the preceeding month
>
> 2. Calculate the ratio of the number of fridays in current month
to the
> number of fridays in the precceding month
>
> 3. Return a integer value calculated as
> ifelse(ratio>1,1,ifesle(ration<1,-1),0)
>
> The date which is passed is in the format *'31-may-2014'*
>
> So, given the date '31-may-2014'
>
> Number of fridays in May2014 = 5
> Number of fridays in Apr2014 = 4
>
> Ratio = 5/4 >1
> Hence, the function will return a value 1
>
> I want to call the function by passing '31-may-2014' as an argument
>
> How can this be done in R?
>
> Any help will be appreciated

Convert your string to a POSIXlt object using as.POSIXlt.  Then
you can
extract year, month and weekday from the result, and go from there.
(The only unobvious part is figuring out how many days are in each
month, but there are questions online giving various ways to do this.)

Duncan Murdoch




__
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] Count number of Fridays in a month

2014-10-10 Thread Barry Rowlingson
Or try this:

fridays <- function(the_day){
require(lubridate)
day(the_day) = 1
the_month = seq(the_day, the_day+months(1)-days(1),1)
sum(wday(the_month) == 6)
}

That returns the number of Fridays in the month of a Date object given as arg:

> fridays(as.Date("2012-01-01"))
[1] 4
> fridays(as.Date("2012-02-01"))
[1] 4
> fridays(as.Date("2012-03-01"))
[1] 5
> fridays(as.Date("2012-04-01"))
[1] 4

Then you can build a function to compute the friday count ratio
between the month of a date and the previous month, given a data in
that string format you specified (d-m-y)

friday_ratio<- function(dayX){
require(lubridate)
the_day = as.Date(dmy(dayX))
day(the_day)=1
the_prev_month = the_day
month(the_prev_month) = month(the_day)-1
nf_current = fridays(the_day)
nf_prev = fridays(the_prev_month)
nf_current/nf_prev
}

> friday_ratio("31-may-2014")
[1] 1.25
> friday_ratio("1-may-2014")
[1] 1.25
> friday_ratio("1-jun-2014")
[1] 0.8
> friday_ratio("1-jul-2014")
[1] 1
> friday_ratio("1-aug-2014")
[1] 1.25

it is left as an exercise to convert this to the +1/0/-1 value - I
think the abs function may help...

Also, an exercise is to vectorise this over a vector of string dates.




On Fri, Oct 10, 2014 at 1:16 PM, Duncan Murdoch
 wrote:
> On 10/10/2014 8:10 AM, Abhinaba Roy wrote:
>> Hi Duncan,
>>
>> I have converted the string to a POSIXIt object using
>>
>> > strptime('31-may-2014',format="%d-%b-%Y")
>>
>> But could not figure out the way forward.
>>
>> Could you please elaborate a bit?
>
> Try this:
>
> ?POSIXlt
>
> Duncan Murdoch
>
>>
>> On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch
>> mailto:murdoch.dun...@gmail.com>> wrote:
>>
>> On 10/10/2014, 7:28 AM, Abhinaba Roy wrote:
>> > Hi R helpers,
>> >
>> > I want to write a function which will
>> >
>> > 1. Count the number of fridays in the current month ( to extract
>> month from
>> > given date) and also the number of fridays in the preceeding month
>> >
>> > 2. Calculate the ratio of the number of fridays in current month
>> to the
>> > number of fridays in the precceding month
>> >
>> > 3. Return a integer value calculated as
>> > ifelse(ratio>1,1,ifesle(ration<1,-1),0)
>> >
>> > The date which is passed is in the format *'31-may-2014'*
>> >
>> > So, given the date '31-may-2014'
>> >
>> > Number of fridays in May2014 = 5
>> > Number of fridays in Apr2014 = 4
>> >
>> > Ratio = 5/4 >1
>> > Hence, the function will return a value 1
>> >
>> > I want to call the function by passing '31-may-2014' as an argument
>> >
>> > How can this be done in R?
>> >
>> > Any help will be appreciated
>>
>> Convert your string to a POSIXlt object using as.POSIXlt.  Then
>> you can
>> extract year, month and weekday from the result, and go from there.
>> (The only unobvious part is figuring out how many days are in each
>> month, but there are questions online giving various ways to do this.)
>>
>> Duncan Murdoch
>>
>>
>
> __
> 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] maximum likelihood estimation

2014-10-10 Thread Arne Henningsen
On 10 October 2014 08:04, pari hesabi  wrote:
> Hello,As an example for Exponential distribution the MLE is got by this 
> structure:t <- rexp(100, 2)loglik <- function(theta){ log(theta) - theta*t}a 
> <- maxLik(loglik, start=1)print(a)Exponential distribution has a simple 
> loglikelihood function.  But if a new pdf has a more complicated form 
> like:pr(N(2)=n)= integral( ((2-x)^n)*(exp(ax-2))) - integral (((5-ax)^n)), 
> both integrals are defined over the interval(0,2) with respect to x.   I  
> also need to use the loglike of the form : [F log(pr(n))]=lnL  where F is the 
> vector of observations and (n) is the vector of input for  the defined pmf.  
> Do you think how I can insert (define) the  pr(n) in the loop below?  loglik 
> <- function(a) sum(F*(log(pr(n)))?> > n <- c(0,1,2,3,4,5,6,7,8)> > 
> F<-c(0,0,1,3,5,7,8,11,10)> > loglik <- function(a) sum(F*(log(pr(n)))??> 
> > re <- maxLik (loglik, start=.5)> > summary(re)I would be grateful if you 
> let me know your idea.Best Regards,pari

> [...]
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Your example is not reproducible:

> pr(N(2)=n)= integral( ((2-x)^n)*(exp(ax-2))) - integral (((5-ax)^n))
Error: unexpected '=' in "pr(N(2)="

Please format your email in a way that one can easily copy the R code
to the R console.

Best regards,
Arne

-- 
Arne Henningsen
http://www.arne-henningsen.name

__
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] (no subject)

2014-10-10 Thread Tasnuva Tabassum
I want to get rid of this thread. what to do?

[[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] (no subject)

2014-10-10 Thread Sven E. Templer
follow instructions on
https://stat.ethz.ch/mailman/listinfo/r-help
at
"To unsubscribe from R-help, get a password reminder, or change your
subscription options enter your subscription email address: "
...

On 10 October 2014 16:16, Tasnuva Tabassum  wrote:
> I want to get rid of this thread. what to do?
>
> [[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-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] Changing date format

2014-10-10 Thread John McKown
On Fri, Oct 10, 2014 at 1:32 AM, Frederic Ntirenganya  wrote:

Please try to not post in HTML. It is one of the forum "rules".

> Dear All,
>
> The following function gives me what I wanted.
> The input of function is one value and this push me to use sapply function
> to call it.
>
> Is there a better way of doing this?
>
> #
> day <- function(x){
>
>  if(x== 60) {
>y = "29 Feb"
>  }
>
>  if(x < 60){
> y =  format(strptime(x, format = "%j"), format ="%d-%b")
> }
>
> if(x > 60){
> y =  format(strptime(x - 1, format = "%j"), format ="%d-%b")
>}
> y
> }
>
> sapply(Samaru$Start,day)
>
>  [1] "17-Apr" "27-Apr" "24-Apr" "04-Jun" "25-Apr" "13-May" "22-Apr"
> "05-May" "27-Apr" "13-May" "27-Apr"
> #===

I make no comment about whether the above is "correct" or you, because
I don't know. There is much that I don't know. There is a way to
reformulate the function in a way that I personally think is better.
You might want to look at:

day <- function(x) { ifelse(x==60,"29
Feb",format(strptime(x-(x>60),format="%j"),format="%d-%b")); }

The "trick" is that x-(x>60) will subtract 1 from x when x is greater
than 60, but 0 if it is less than or equal to 60. And the ifelse takes
care of the case where x is equal to 60. As I said, I don't know that
this really gives you want, but it is functionally equivalent to the
code you posted. However, it is not as obvious. As we say about the
lottery: "You pay your money, you take your chance."


-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

__
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] Maximum likelihood Estimation

2014-10-10 Thread Parvin Dehghani


maximum likelihood estimation
pari hesabi  
6:04 AM 
To: r-help@r-project.org
Hello,
As an example for Exponential distribution the MLE is got by this structure:
t <- rexp(100, 2)
loglik <- function(theta){ log(theta) - theta*t}
a <- maxLik(loglik, start=1)
print(a)

Exponential distribution has a simple loglikelihood function.  But if a new pdf 
has a more complicated form like:
pr(N(2)=n)= integral( ((2-x)^n)*(exp(ax-2))) - integral (((5-ax)^n)), both 
integrals are defined over the interval(0,2) with respect to x.  
 I  also need to use the loglike of the form : [F log(pr(n))]=lnL  where F is 
the vector of observations and (n) is the vector of input for  the defined pmf. 
 Do you think how I can insert (define) the  pr(n) in the loop below? 
My question is related to:loglik <- function(a) sum(F*(log(pr(n)))?



> > n <- c(0,1,2,3,4,5,6,7,8)
> > F<-c(0,0,1,3,5,7,8,11,10)
> > loglik <- function(a) sum(F*(log(pr(n)))??
> > re <- maxLik (loglik, start=.5)
> > summary(re)

I would be grateful if you let me know your idea.
Best Regards,
pari

[[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] Count number of Fridays

2014-10-10 Thread jim holtman
Here is one way of doing it:


> require(lubridate)
> now <- as.Date('2014-10-10')  # some date
> # get first of month
> first_mon <- now - day(now) + 1
> # create sequence of days in the month so you can count Fridays
> x <- seq(first_mon, by = '1 day', length = days_in_month(first_mon))
> first_fri <- sum(wday(x) == 6)  # count fridays
> # first of previous month
> prev_mon <- first_mon - day(first_mon - 1)
> # create sequence of days in month
> x <- seq(prev_mon, by = '1 day', length = days_in_month(prev_mon))
> prev_fri <- sum(wday(x) == 6)
>
> cat('Fri this month:', first_fri, 'Fri last month:', prev_fri, '\n')
Fri this month: 5 Fri last month: 4
> sign(first_fri - prev_fri)  # will do the 'ifelse'-type test you want
[1] 1
>

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.


On Fri, Oct 10, 2014 at 7:31 AM, Abhinaba Roy  wrote:
> Hi Jim,
>
> Thanks for your previous solution. I am learning to work with dates now.
>
> I want to write a function which will
>
> 1. Count the number of fridays in the current month ( to extract month from
> given date) and also the number of fridays in the preceeding month
>
> 2. Calculate the ratio of the number of fridays in current month to the
> number of fridays in the precceding month
>
> 3. Return a integer value calculated as
> ifelse(ratio>1,1,ifesle(ration<1,-1),0)
>
> The date which is passed is in the format '31-may-2014'
>
> So, given the date '31-may-2014'
>
> Number of fridays in May2014 = 5
> Number of fridays in Apr2014 = 4
>
> Ratio = 5/4 >1
> Hence, the function will return a value 1
>
> I want to call the function by passing '31-may-2014' as an argument
>
> How can this be done in R?
>
> Any help will be appreciated
>
> Thanks and regards,
> Abhinaba
>
> On Mon, Aug 4, 2014 at 9:36 PM, jim holtman  wrote:
>>
>> Here is the solution using 'rle':
>>
>> > require(data.table)
>> > x <- read.table(text = "CASE_ID YEAR_MTH ATT_1
>> +  CB26A201302 1
>> +  CB26A201302 0
>> +  CB26A201302 0
>> +  CB26A201303 1
>> +  CB26A201303 1
>> +  CB26A201304 0
>> +  CB26A201305 1
>> +  CB26A201305 0
>> +  CB26A201306 1
>> +  CB27A201304 0
>> +  CB27A201304 0
>> +  CB27A201305 1
>> +  CB27A201306 1
>> +  CB27A201306 0
>> +  CB27A201307 0
>> +  CB27A201308 1", header = TRUE, as.is = TRUE)
>> > setDT(x)
>> > # convert to a Date object for comparison
>> > x[, MYD := as.Date(paste0(YEAR_MTH, '01'), format = "%Y%m%d")]
>> > # separate by CASE_ID and only keep the first 3 months
>> > x[
>> +  , {
>> +  # determine the end date as 3 months from the first date
>> +  endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
>> +  # now count the changes
>> +  list(nChanges = length(rle(ATT_1[(MYD >= MYD[1L]) & (MYD <=
>> endDate)])[[1L]]) - 1L)
>> +}
>> +  , by = CASE_ID
>> +  ]
>>CASE_ID nChanges
>> 1:   CB26A5
>> 2:   CB27A2
>>
>> 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.
>>
>>
>> On Mon, Aug 4, 2014 at 11:39 AM, Bert Gunter 
>> wrote:
>> > Or ?rle
>> >
>> > Bert
>> >
>> >
>> >
>> > Sent from my iPhone -- please excuse typos.
>> >
>> >> On Aug 4, 2014, at 8:28 AM, jim holtman  wrote:
>> >>
>> >> Try this, but I only get 2 changes for CB27A instead of you indicated
>> >> 3:
>> >>
>> >>> require(data.table)
>> >>> x <- read.table(text = "CASE_ID YEAR_MTH ATT_1
>> >> + CB26A201302 1
>> >> + CB26A201302 0
>> >> + CB26A201302 0
>> >> + CB26A201303 1
>> >> + CB26A201303 1
>> >> + CB26A201304 0
>> >> + CB26A201305 1
>> >> + CB26A201305 0
>> >> + CB26A201306 1
>> >> + CB27A201304 0
>> >> + CB27A201304 0
>> >> + CB27A201305 1
>> >> + CB27A201306 1
>> >> + CB27A201306 0
>> >> + CB27A201307 0
>> >> + CB27A201308 1", header = TRUE, as.is = TRUE)
>> >>> setDT(x)
>> >>> # convert to a Date object for comparison
>> >>> x[, MYD := as.Date(paste0(YEAR_MTH, '01'), format = "%Y%m%d")]
>> >>> # separate by CASE_ID and only keep the first 3 months
>> >>> x[
>> >> + , {
>> >> + # determine the end date as 3 months from the first date
>> >> + endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
>> >> + # extract what is changing
>> >> + changes <- ATT_1[(MYD >= MYD[1L]) & (MYD <= endDate)]
>> >> + # now count the changes
>> >> + list(nChanges = sum(head(changes, -1L) != tail(changes,
>> >> -1L)))
>> >> +   }
>> >> + , by = CASE_ID
>> >> + ]
>> >>   CASE_ID nChanges
>> >> 1:   CB26A5
>> >> 2

Re: [R] Count number of Fridays in a month

2014-10-10 Thread Gabor Grothendieck
On Fri, Oct 10, 2014 at 7:28 AM, Abhinaba Roy  wrote:
> Hi R helpers,
>
> I want to write a function which will
>
> 1. Count the number of fridays in the current month ( to extract month from
> given date) and also the number of fridays in the preceeding month
>
> 2. Calculate the ratio of the number of fridays in current month to the
> number of fridays in the precceding month
>
> 3. Return a integer value calculated as
> ifelse(ratio>1,1,ifesle(ration<1,-1),0)
>
> The date which is passed is in the format *'31-may-2014'*
>
> So, given the date '31-may-2014'
>
> Number of fridays in May2014 = 5
> Number of fridays in Apr2014 = 4
>
> Ratio = 5/4 >1
> Hence, the function will return a value 1
>
> I want to call the function by passing '31-may-2014' as an argument

If d is a "Date" class variable equal to a month end date, e.g.

   d <- as.Date("31-may-2014", format = "%d-%b-%Y")

then this gives the ratio of the number of Fridays in d's month
to the number of Fridays in the prior month:

   days <- seq(as.Date(cut(d - 32, "month")), d, "day")
   ratio <- exp(diff(log(tapply(format(days, "%w") == 5, format(days,
"%Y%m"), sum

Now apply the formula in your point 3 to ratio and put it all in a function.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
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] How to remove the second line generated by addHeader function in the rtf package

2014-10-10 Thread Zhiqiu Hu
Dear friends,

The addHeader function in the rtf package always generates two rows of texts
even if a subtitle was not specified. Is there any way to stop the function
from generating the second blank line?

FYI, I cannot use the other functions, such as the addParagram and the
addText, because the addHeader function is the only one allowing to set up
TOC levels for contents.

I appreciate your kind help with the matters.

Sincerely,

Zhiqiu

[[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] Count number of Fridays in a month

2014-10-10 Thread PO SU

In my Impression, there seems  exsits a function (let just call weekday) which 
can return a POSIXlt format date 's weekday. That means,  weekday( 31-may-2014 
) may return the right weekday maybe 5, so  a clumsy method is start to judge 
from 1-may-2014 to 31-may-2014 , you get  a vector x, sum(which(x==5)) should 
return the right number. so do the April-2014. 
FOR MORE EFFICIENT, may be you should look into the function weekday ( not the 
true name) to get how it work ,it may help you do less things to get what you 
want.
FOR weekday,sorry for my forgot of where it located, it may in ?POSIXlt  
?format  ? strptime and other related keywords's document.





--

PO SU
mail: desolato...@163.com 
Majored in Statistics from SJTU




At 2014-10-10 19:44:17, "Duncan Murdoch"  wrote:
>On 10/10/2014, 7:28 AM, Abhinaba Roy wrote:
>> Hi R helpers,
>> 
>> I want to write a function which will
>> 
>> 1. Count the number of fridays in the current month ( to extract month from
>> given date) and also the number of fridays in the preceeding month
>> 
>> 2. Calculate the ratio of the number of fridays in current month to the
>> number of fridays in the precceding month
>> 
>> 3. Return a integer value calculated as
>> ifelse(ratio>1,1,ifesle(ration<1,-1),0)
>> 
>> The date which is passed is in the format *'31-may-2014'*
>> 
>> So, given the date '31-may-2014'
>> 
>> Number of fridays in May2014 = 5
>> Number of fridays in Apr2014 = 4
>> 
>> Ratio = 5/4 >1
>> Hence, the function will return a value 1
>> 
>> I want to call the function by passing '31-may-2014' as an argument
>> 
>> How can this be done in R?
>> 
>> Any help will be appreciated
>
>Convert your string to a POSIXlt object using as.POSIXlt.  Then you can
>extract year, month and weekday from the result, and go from there.
>(The only unobvious part is figuring out how many days are in each
>month, but there are questions online giving various ways to do this.)
>
>Duncan Murdoch
>
>__
>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.


[R] errors in initial values in R2winBUGS

2014-10-10 Thread thanoon younis
Dear all R users
I am trying to find the bayesian analysis using R2winBUGS but i have errors
in initial values with two groups.
the R-code
#Initial values for the MCMC in WinBUGS

init1<-list(uby1=rep(0.0,10),lam1=c(0.0,0.0,0.0,0.0,0.0,0.0),
gam1=c(1.0,1.0,1.0,1.0,1.0,1.0),psd1=1.0,phi1=matrix(data=c(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),ncol=3,byrow=TRUE),uby2=rep(0.0,10),lam2=c(0.0,0.0,0.0,0.0,0.0,0.0),
gam2=c(1.0,1.0,1.0,1.0,1.0,1.0),psd2=1.0,phi2=matrix(data=c(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),ncol=3,byrow=TRUE),
xi1=matrix(data=rep(0.0,600),ncol=3),xi2=matrix(data=rep(0.0,600),ncol=3)),

init2<-list(uby1=rep(0.5,10),lam1=c(0.5,0.5,0.5,0.5,0.5,0.5),
gam1=c(0.0,0.0,0.0,0.0,0.0,0.0),psd1=0.6,phi1=matrix(data=c(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),ncol=3,byrow=TRUE),uby2=rep(0.5,10),lam2=c(0.5,0.5,0.5,0.5,0.5,0.5),
gam2=c(0.0,0.0,0.0,0.0,0.0,0.0),psd2=0.6,phi2=matrix(data=c(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),ncol=3,byrow=TRUE),
xi1=matrix(data=rep(0.0,600),ncol=3),xi2=matrix(data=rep(0.0,600),ncol=3)),


and the errors are

Error: unexpected ',' in:
"3,byrow=TRUE),uby2=rep(0.0,10),lam2=c(0.0,0.0,0.0,0.0,0.0,0.0),
gam2=c(1.0,1.0,1.0,1.0,1.0,1.0),psd2=1.0,phi2=matrix(data=c(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),ncol=3,byrow=TRUE),
xi1=matrix(data=rep(0.0,600),ncol=3),xi2=matrix(data=rep(0.0,600),ncol=3)),"

and

Error: unexpected ',' in:
"3,byrow=TRUE),uby2=rep(0.5,10),lam2=c(0.5,0.5,0.5,0.5,0.5,0.5),
gam2=c(0.0,0.0,0.0,0.0,0.0,0.0),psd2=0.6,phi2=matrix(data=c(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),ncol=3,byrow=TRUE),
xi1=matrix(data=rep(0.0,600),ncol=3),xi2=matrix(data=rep(0.0,600),ncol=3)),"



any help would be greatly appreciated


-- 
Thanoon Y. Thanoon
PhD Candidate
Department of Mathematical Sciences
Faculty of Science
University Technology Malaysia, UTM
E.Mail: thanoon.youni...@gmail.com
E.Mail: dawn_praye...@yahoo.com
Facebook:Thanoon Younis AL-Shakerchy
Twitter: Thanoon Alshakerchy
H.P:00601127550205

[[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] Count number of Fridays

2014-10-10 Thread Abhinaba Roy
Thanks Jim :)

Regards
Abhinaba

On Fri, Oct 10, 2014 at 9:02 PM, jim holtman  wrote:

> Here is one way of doing it:
>
>
> > require(lubridate)
> > now <- as.Date('2014-10-10')  # some date
> > # get first of month
> > first_mon <- now - day(now) + 1
> > # create sequence of days in the month so you can count Fridays
> > x <- seq(first_mon, by = '1 day', length = days_in_month(first_mon))
> > first_fri <- sum(wday(x) == 6)  # count fridays
> > # first of previous month
> > prev_mon <- first_mon - day(first_mon - 1)
> > # create sequence of days in month
> > x <- seq(prev_mon, by = '1 day', length = days_in_month(prev_mon))
> > prev_fri <- sum(wday(x) == 6)
> >
> > cat('Fri this month:', first_fri, 'Fri last month:', prev_fri, '\n')
> Fri this month: 5 Fri last month: 4
> > sign(first_fri - prev_fri)  # will do the 'ifelse'-type test you want
> [1] 1
> >
>
> 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.
>
>
> On Fri, Oct 10, 2014 at 7:31 AM, Abhinaba Roy 
> wrote:
> > Hi Jim,
> >
> > Thanks for your previous solution. I am learning to work with dates now.
> >
> > I want to write a function which will
> >
> > 1. Count the number of fridays in the current month ( to extract month
> from
> > given date) and also the number of fridays in the preceeding month
> >
> > 2. Calculate the ratio of the number of fridays in current month to the
> > number of fridays in the precceding month
> >
> > 3. Return a integer value calculated as
> > ifelse(ratio>1,1,ifesle(ration<1,-1),0)
> >
> > The date which is passed is in the format '31-may-2014'
> >
> > So, given the date '31-may-2014'
> >
> > Number of fridays in May2014 = 5
> > Number of fridays in Apr2014 = 4
> >
> > Ratio = 5/4 >1
> > Hence, the function will return a value 1
> >
> > I want to call the function by passing '31-may-2014' as an argument
> >
> > How can this be done in R?
> >
> > Any help will be appreciated
> >
> > Thanks and regards,
> > Abhinaba
> >
> > On Mon, Aug 4, 2014 at 9:36 PM, jim holtman  wrote:
> >>
> >> Here is the solution using 'rle':
> >>
> >> > require(data.table)
> >> > x <- read.table(text = "CASE_ID YEAR_MTH ATT_1
> >> +  CB26A201302 1
> >> +  CB26A201302 0
> >> +  CB26A201302 0
> >> +  CB26A201303 1
> >> +  CB26A201303 1
> >> +  CB26A201304 0
> >> +  CB26A201305 1
> >> +  CB26A201305 0
> >> +  CB26A201306 1
> >> +  CB27A201304 0
> >> +  CB27A201304 0
> >> +  CB27A201305 1
> >> +  CB27A201306 1
> >> +  CB27A201306 0
> >> +  CB27A201307 0
> >> +  CB27A201308 1", header = TRUE, as.is = TRUE)
> >> > setDT(x)
> >> > # convert to a Date object for comparison
> >> > x[, MYD := as.Date(paste0(YEAR_MTH, '01'), format = "%Y%m%d")]
> >> > # separate by CASE_ID and only keep the first 3 months
> >> > x[
> >> +  , {
> >> +  # determine the end date as 3 months from the first date
> >> +  endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
> >> +  # now count the changes
> >> +  list(nChanges = length(rle(ATT_1[(MYD >= MYD[1L]) & (MYD <=
> >> endDate)])[[1L]]) - 1L)
> >> +}
> >> +  , by = CASE_ID
> >> +  ]
> >>CASE_ID nChanges
> >> 1:   CB26A5
> >> 2:   CB27A2
> >>
> >> 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.
> >>
> >>
> >> On Mon, Aug 4, 2014 at 11:39 AM, Bert Gunter 
> >> wrote:
> >> > Or ?rle
> >> >
> >> > Bert
> >> >
> >> >
> >> >
> >> > Sent from my iPhone -- please excuse typos.
> >> >
> >> >> On Aug 4, 2014, at 8:28 AM, jim holtman  wrote:
> >> >>
> >> >> Try this, but I only get 2 changes for CB27A instead of you indicated
> >> >> 3:
> >> >>
> >> >>> require(data.table)
> >> >>> x <- read.table(text = "CASE_ID YEAR_MTH ATT_1
> >> >> + CB26A201302 1
> >> >> + CB26A201302 0
> >> >> + CB26A201302 0
> >> >> + CB26A201303 1
> >> >> + CB26A201303 1
> >> >> + CB26A201304 0
> >> >> + CB26A201305 1
> >> >> + CB26A201305 0
> >> >> + CB26A201306 1
> >> >> + CB27A201304 0
> >> >> + CB27A201304 0
> >> >> + CB27A201305 1
> >> >> + CB27A201306 1
> >> >> + CB27A201306 0
> >> >> + CB27A201307 0
> >> >> + CB27A201308 1", header = TRUE, as.is = TRUE)
> >> >>> setDT(x)
> >> >>> # convert to a Date object for comparison
> >> >>> x[, MYD := as.Date(paste0(YEAR_MTH, '01'), format = "%Y%m%d")]
> >> >>> # separate by CASE_ID and only keep the first 3 months
> >> >>> x[
> >> >> + , {
> >> >> + # determine the end date as 3 months from the first date
> >> >> + endDate <- seq(MYD[1L

[R] Problem Invoking System Commands from R

2014-10-10 Thread Matt Borkowski
Hello,
First please keep in mind I am not a programmer and know very little about R. I 
am running the 64bit version of R on a Windows 8.1 machine. I am trying to run 
a script (which I have successfully run in the past) to download some weather 
data from a NOAA ftp site.
When I attempt to run the following command:     system("wget -P data/raw 
ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2013/724620-23061-2013.gz";)

it returns status 127, which as I understand simply means the command will not 
run.
If I go directly to my command prompt in Windows, navigate to my working 
director, and run     wget -P data/raw 
ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2013/724620-23061-2013.gz
the command runs and the file downloads without a problem. 
Playing around, it seems I can't invoke any system commands from R. Even a 
simple      system("dir")
returns status 127.
I have moved to a new computer since I last successfully ran this script...I'm 
wondering if this might be a permissions issue or other security setting 
preventing me from invoking system commands.
Any ideas?
-Matt
[[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.