Re: [R] by group

2015-10-20 Thread Val
Hi All,

Thank you very  much for your suggestion to use ddply function for my data
set. I used the function like the following

mydata <- read.table(header=TRUE, text='
 cntry  state city gender
   1 1 1   1
   1 1 1   2
   1 1 1   1
   1 1 2   2
   1 2 2   2
   1 2 3   2
   1 2 3   1
')

 cndata <- ddply(mydata, c("cntry","gender"), summarise,
N= length(gender)
  )
 cndata

cntry gender N
   1  1  3
   1  2  4

My question now is,  i want mdata to  be as follows
cntry  sex1(F) sex2 (M)
 1  3 4

Then I want And I want merge cndata with  the original data,  mydata. How
do I do that?

Thank you oin advance.






On Mon, Oct 12, 2015 at 5:27 PM, MCGUIRE, Rhydwyn <
rm...@doh.health.nsw.gov.au> wrote:

> ddply() is what you are looking for. Here is an example
> http://www.inside-r.org/packages/cran/plyr/docs/ddply
>
> Cheers,
> Rhydwyn
>
>
> Rhydwyn McGuire
> Senior Biostatistician | Health Statistics NSW
> Level 7, 73 Miller St, North Sydney 2060
> Tel 02 9391 9781 | rm...@doh.health.nsw.gov.au
> www.health.nsw.gov.au
>
>
>
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Val
> Sent: Tuesday, 13 October 2015 4:06 AM
> To: r-help@r-project.org
> Subject: [R] by group
>
> Hi all,
>
>
> Assume that I have the following data set :
>
>  cntry  state city Gender (1=F and 2=M)
>
>  1 1 1   1
>  1 1 1   2
>  1 1 1   1
>  1 1 2   2
>  1 2 2   2
>  1 2 3   2
>  1 2 3   1
>
> I want to calculate the number of Females and Males, total (F+M) and
> percentage (F/M) by country, state and city.
>
> Here is the  sample of the output file  that I would like to have in a
> file.
>
> cntry state City Gender
> Cntry_F Cntry_MCntry_total 100 (Cntry_F/Cntry_M)
> St_Fst_MSt_total  100*(   St_F/   St_M)
> City_F City_M City_total  100*( City_F/ City_M)
>
>  1  1  1  1   3  4  7  752  2  4  50   1 1 3  33
>  1  1  1  2   3  4  7  752  2  4  50   1 1 3  33
>  1  1  1  1   3  4  7  752  2  4  50   1 1 3  33
>  1  1  2  2   3  4  7  752  2  4  50   2 0 2 100
>  1  2  2  2   3  4  7  752  1  3  67   2 0 2 100
>  1  2  3  2   3  4  7  752  1  3  67   1 1 2  50
>  1  2  3  1   3  4  7  752  1  3  67   1 1 2  50
>
> Your help is highly appreciated in advance
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
> __
> This email has been scanned for the NSW Ministry of Health by the Websense
> Hosted Email Security System.
> Emails and attachments are monitored to ensure compliance with the NSW
> Ministry of health's Electronic Messaging Policy.
>
> __
>
> ___
> Disclaimer: This message is intended for the addressee named and may
> contain confidential information.
> If you are not the intended recipient, please delete it and notify the
> sender.
> Views expressed in this message are those of the individual sender, and
> are not necessarily the views of the NSW Ministry of Health.
>
> ___
> This email has been scanned for the NSW Ministry of Health by the Websense
> Hosted Email Security System.
> Emails and attachments are monitored to ensure compliance with the NSW
> Ministry of Health's Electronic Messaging Policy.
>
> ___
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Scope of Axes

2015-10-20 Thread Jim Lemon
Hi bgnumis,
I'm too lazy to try to work out what "Simulation" contains, but try this:

Simulation<-sin(seq(0,6*pi,length.out=144))*5000+
 2000*runif(144)+seq(8000,5000,length.out=144)
png("bb.png",width=800,height=400)
par(mfrow=c(1,2))
plot(Simulation,type="l",ylim=c(0,2))
abline(h = 0, lwd = 2, col = "black")
fhist<-hist(Simulation,breaks=seq(0,2,by=2000),plot=FALSE)
barplot(fhist$counts,axes=FALSE, space=0,horiz=TRUE,col="lightgray")
dev.off()

Jim


On Wed, Oct 21, 2015 at 3:27 AM, bgnumis bgnum  wrote:

> Hi all,
>
> I want to plot two graphs and I use this :
>
> par(mar=c(10,6,6,6))
> matplot(Simulation,type="l")
>
> abline(h = 0, lwd = 2, col = "black")
>
> fhist<-hist(Simulation,plot=FALSE)
> par(mar=c(6,0,6,6))
> barplot(fhist$counts,axes=FALSE, space=0,horiz=TRUE,col="lightgray")
>
>
> The question is, that the legth of the hist plot is not equilibrated with
> the main plot so the fhist is not achiving the goal I´m looking so that it
> happens what I attach in png, it seems that the right plot is central value
> is below the value of the simulation. How can I ensure that de max min in
> the left is the same that in the right (second) plot?
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Transfer a 3-dimensional array to a matrix in R

2015-10-20 Thread Dénes Tóth

Hi,

Bill was faster than me in suggesting aperm() instead of apply(), 
however, his solution is still suboptimal. Try to avoid array(), and

set the dimensions directly if possible.



fn1 <- function(x) {
apply(x, 3, t)
}


fn2 <- function(x) {
array(aperm(x, c(2, 1, 3)), c(prod(dim(x)[1:2]), dim(x)[3]))
}

fn3 <- function(x) {
x <- aperm(x, c(2, 1, 3))
dim(x) <- c(prod(dim(x)[1:2]), dim(x)[3])
x
}

# check that the functions return the same
x <- array(1:18, dim=c(3, 2, 3))
stopifnot(identical(fn1(x), fn2(x)))
stopifnot(identical(fn1(x), fn3(x)))

# create two larger arrays, play with the size of the 3rd dimension
x <- array(1:18e4, dim=c(3, 2e1, 3e3))
y <- array(1:18e4, dim=c(3e3, 2e1, 3))

# and the timing:
library(microbenchmark)
microbenchmark(fn1(x), fn2(x), fn3(x), fn1(y), fn2(y), fn3(y), times = 100L)

---

Conclusion:
fn3() is about 3x as fast as fn2(), and fn1() can be extremely 
inefficient if dim(x)[3] is large.



HTH,
  Denes




On 10/20/2015 08:48 PM, William Dunlap wrote:

Or use aperm() (array index permuation):
   > array(aperm(x, c(2,1,3)), c(6,3))
[,1] [,2] [,3]
   [1,]17   13
   [2,]4   10   16
   [3,]28   14
   [4,]5   11   17
   [5,]39   15
   [6,]6   12   18

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Oct 20, 2015 at 11:31 AM, John Laing  wrote:

x <- array(1:18, dim=c(3, 2, 3))
x

, , 1

  [,1] [,2]
[1,]14
[2,]25
[3,]36

, , 2

  [,1] [,2]
[1,]7   10
[2,]8   11
[3,]9   12

, , 3

  [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18


apply(x, 3, t)

  [,1] [,2] [,3]
[1,]17   13
[2,]4   10   16
[3,]28   14
[4,]5   11   17
[5,]39   15
[6,]6   12   18


On Tue, Oct 20, 2015 at 12:39 PM, Chunyu Dong 
wrote:


Hello!


Recently I am trying to transfer a large 3-dimensional array to a matrix.
For example, a array like:
, , 1
  [,1] [,2]
[1,]14
[2,]25
[3,]36
, , 2
  [,1] [,2]
[1,]7   10
[2,]8   11
[3,]9   12
, , 3
  [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18


I would like to transfer it to a matrix like:
17  13
41016
28  14
51117
39  15
61218


Could you tell me how to do it in R ? Thank you very much!


Best regards,
Chunyu





 [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



 [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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] Transfer a 3-dimensional array to a matrix in R

2015-10-20 Thread William Dunlap
Or use aperm() (array index permuation):
  > array(aperm(x, c(2,1,3)), c(6,3))
   [,1] [,2] [,3]
  [1,]17   13
  [2,]4   10   16
  [3,]28   14
  [4,]5   11   17
  [5,]39   15
  [6,]6   12   18

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Oct 20, 2015 at 11:31 AM, John Laing  wrote:
>> x <- array(1:18, dim=c(3, 2, 3))
>> x
> , , 1
>
>  [,1] [,2]
> [1,]14
> [2,]25
> [3,]36
>
> , , 2
>
>  [,1] [,2]
> [1,]7   10
> [2,]8   11
> [3,]9   12
>
> , , 3
>
>  [,1] [,2]
> [1,]   13   16
> [2,]   14   17
> [3,]   15   18
>
>> apply(x, 3, t)
>  [,1] [,2] [,3]
> [1,]17   13
> [2,]4   10   16
> [3,]28   14
> [4,]5   11   17
> [5,]39   15
> [6,]6   12   18
>
>
> On Tue, Oct 20, 2015 at 12:39 PM, Chunyu Dong 
> wrote:
>
>> Hello!
>>
>>
>> Recently I am trying to transfer a large 3-dimensional array to a matrix.
>> For example, a array like:
>> , , 1
>>  [,1] [,2]
>> [1,]14
>> [2,]25
>> [3,]36
>> , , 2
>>  [,1] [,2]
>> [1,]7   10
>> [2,]8   11
>> [3,]9   12
>> , , 3
>>  [,1] [,2]
>> [1,]   13   16
>> [2,]   14   17
>> [3,]   15   18
>>
>>
>> I would like to transfer it to a matrix like:
>> 17  13
>> 41016
>> 28  14
>> 51117
>> 39  15
>> 61218
>>
>>
>> Could you tell me how to do it in R ? Thank you very much!
>>
>>
>> Best regards,
>> Chunyu
>>
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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 S4 class with a slot as an array

2015-10-20 Thread Morgan, Martin


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Glenn
> Schultz
> Sent: Tuesday, October 20, 2015 10:36 AM
> To: R Help R
> Subject: [R] Creating S4 class with a slot as an array
> 
> Hello All,
> 
> I am trying to create an S4 class with a slot that is an array.  Below is the 
> code I
> tried but I have missed something.  Any suggestions are appreciated.
> 
> setClass("CashFlow",
> representation(
> CashFlowArray = "array"))

probably here you would also like to provide a prototype

 setClass("CashFlow",
  representation(CashFlowArray="array"),
  prototype=prototype(CashFlowArray=array(numeric(), c(0, 0, 0

> 
> setMethod("initialize",
> signature = "CashFlow",
> function(.Object,
> CashFlowArray = "array"
> ){

.Object is the prototype, so the next line...

> .Object@CashFlowArray

references the prototypes' default value for the slot, without modifying it, 
and then...

> return(.Object)

returns the unmodified object, never getting to the following line...

> callNextMethod(.Object,...)

which would have passed the prototype and all arguments that are _not_ 
CashFlowArray on to the default 'initialize' method.

Maybe you meant

setMethod("initialize", "CashFlow", function(.Object, ..., 
CashFlowArray=array()) {
callNextMethod(.Object, ..., CashFlowArray=CashFlowArray)
})

or, since the initialize method is now not doing anything, simply not defining 
it in the first place.

Martin Morgan

> })
> CashFlow <-function(CashFlowArray = array()){ new("CashFlow",
> CashFlowArray = CashFlowArray) }
> 
> CFTest <- array(data = 2, c(360,8))
> 
> Test <- CashFlow(CashFlowArray = CFTest)
> 
> Thanks,
> Glenn
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


This email message may contain legally privileged and/or confidential 
information.  If you are not the intended recipient(s), or the employee or 
agent responsible for the delivery of this message to the intended 
recipient(s), you are hereby notified that any disclosure, copying, 
distribution, or use of this email message is prohibited.  If you have received 
this message in error, please notify the sender immediately by e-mail and 
delete this email message from your computer. Thank you.
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Transfer a 3-dimensional array to a matrix in R

2015-10-20 Thread John Laing
> x <- array(1:18, dim=c(3, 2, 3))
> x
, , 1

 [,1] [,2]
[1,]14
[2,]25
[3,]36

, , 2

 [,1] [,2]
[1,]7   10
[2,]8   11
[3,]9   12

, , 3

 [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18

> apply(x, 3, t)
 [,1] [,2] [,3]
[1,]17   13
[2,]4   10   16
[3,]28   14
[4,]5   11   17
[5,]39   15
[6,]6   12   18


On Tue, Oct 20, 2015 at 12:39 PM, Chunyu Dong 
wrote:

> Hello!
>
>
> Recently I am trying to transfer a large 3-dimensional array to a matrix.
> For example, a array like:
> , , 1
>  [,1] [,2]
> [1,]14
> [2,]25
> [3,]36
> , , 2
>  [,1] [,2]
> [1,]7   10
> [2,]8   11
> [3,]9   12
> , , 3
>  [,1] [,2]
> [1,]   13   16
> [2,]   14   17
> [3,]   15   18
>
>
> I would like to transfer it to a matrix like:
> 17  13
> 41016
> 28  14
> 51117
> 39  15
> 61218
>
>
> Could you tell me how to do it in R ? Thank you very much!
>
>
> Best regards,
> Chunyu
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Transfer a 3-dimensional array to a matrix in R

2015-10-20 Thread Chunyu Dong
Hello!


Recently I am trying to transfer a large 3-dimensional array to a matrix. For 
example, a array like:
, , 1
 [,1] [,2]
[1,]14
[2,]25
[3,]36
, , 2
 [,1] [,2]
[1,]7   10
[2,]8   11
[3,]9   12
, , 3
 [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18


I would like to transfer it to a matrix like:
17  13
41016
28  14
51117
39  15
61218


Could you tell me how to do it in R ? Thank you very much!


Best regards,
Chunyu





[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Gabor Grothendieck
Correction.

Yes, it's the projection of S onto the subspace orthogonal to B which is:

X <- S - (B%o%B) %*% S/ sum(B*B)

and is also implied by Duncan's solution since that is what the residuals
of linear regression are.

On Tue, Oct 20, 2015 at 1:11 PM, Gabor Grothendieck  wrote:

> Yes, it's the projection of S onto the subspace orthogonal to B which is:
>
> X <- S - B%*%B / sum(B*B)
>
> and is also implied by Duncan's solution since that is what the residuals
> of linear regression are.
>
> On Tue, Oct 20, 2015 at 1:00 PM, Paul Smith  wrote:
>
>> On Tue, Oct 20, 2015 at 11:58 AM, Andy Yuan  wrote:
>> >
>> > Please could you help me to select the most appropriate/fastest
>> function to use for the following constraint optimisation issue?
>> >
>> > Objective function:
>> >
>> > Min: Sum( (X[i] - S[i] )^2)
>> >
>> > Subject to constraint :
>> >
>> > Sum (B[i] x X[i]) =0
>> >
>> > where i=1…n and S[i] and B[i] are real numbers
>> >
>> > Need to solve for X
>> >
>> > Example:
>> >
>> > Assume n=3
>> >
>> > S <- c(-0.5, 7.8, 2.3)
>> > B <- c(0.42, 1.12, 0.78)
>> >
>> > Many thanks
>>
>> I believe you can solve *analytically* your optimization problem, with
>> the Lagrange multipliers method, Andy. By doing so, you can derive
>> clean and closed-form expression for the optimal solution.
>>
>> Paul
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>



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

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Gabor Grothendieck
Yes, it's the projection of S onto the subspace orthogonal to B which is:

X <- S - B%*%B / sum(B*B)

and is also implied by Duncan's solution since that is what the residuals
of linear regression are.

On Tue, Oct 20, 2015 at 1:00 PM, Paul Smith  wrote:

> On Tue, Oct 20, 2015 at 11:58 AM, Andy Yuan  wrote:
> >
> > Please could you help me to select the most appropriate/fastest function
> to use for the following constraint optimisation issue?
> >
> > Objective function:
> >
> > Min: Sum( (X[i] - S[i] )^2)
> >
> > Subject to constraint :
> >
> > Sum (B[i] x X[i]) =0
> >
> > where i=1…n and S[i] and B[i] are real numbers
> >
> > Need to solve for X
> >
> > Example:
> >
> > Assume n=3
> >
> > S <- c(-0.5, 7.8, 2.3)
> > B <- c(0.42, 1.12, 0.78)
> >
> > Many thanks
>
> I believe you can solve *analytically* your optimization problem, with
> the Lagrange multipliers method, Andy. By doing so, you can derive
> clean and closed-form expression for the optimal solution.
>
> Paul
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



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

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Paul Smith
On Tue, Oct 20, 2015 at 11:58 AM, Andy Yuan  wrote:
>
> Please could you help me to select the most appropriate/fastest function to 
> use for the following constraint optimisation issue?
>
> Objective function:
>
> Min: Sum( (X[i] - S[i] )^2)
>
> Subject to constraint :
>
> Sum (B[i] x X[i]) =0
>
> where i=1…n and S[i] and B[i] are real numbers
>
> Need to solve for X
>
> Example:
>
> Assume n=3
>
> S <- c(-0.5, 7.8, 2.3)
> B <- c(0.42, 1.12, 0.78)
>
> Many thanks

I believe you can solve *analytically* your optimization problem, with
the Lagrange multipliers method, Andy. By doing so, you can derive
clean and closed-form expression for the optimal solution.

Paul

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Error in rep.int() invalid 'times' value

2015-10-20 Thread Maram SAlem
Yes Indeed William. f1() works perfectly well and took only 30 secs to
execute f1(25,15), but I wonder if there is anyway to speed up the
execution of the rest of my code (almost seven hours now) ?

Thanks for helping.

Maram Salem

On 20 October 2015 at 18:11, William Dunlap  wrote:

> f0 is essentially your original code put into a function, so
> expect it to fail in the same way your original code did.
> f1 should give the same answer as f0, but it should use
> less memory and time.
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Tue, Oct 20, 2015 at 2:05 AM, Maram SAlem 
> wrote:
> > Thanks William. I've tried the first code, ( with f0() ), but still for
> > n=25, m=15 , I got this:
> >
> >> s<-f0(25,15)
> > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >   invalid 'times' value
> > In addition: Warning message:
> > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >   NAs introduced by coercion to integer range
> >
> >
> > I don't know if this is related to the memory limits of my laptop, or it
> > doesn't have to do with the memory.
> >
> > Any help on how to fix this error will be greatly appreciated.
> >
> > Thanks All.
> >
> > Maram Salem
> >
> > On 15 October 2015 at 17:52, William Dunlap  wrote:
> >>
> >> Doing enumerative combinatorics with rejection methods rarely
> >> works well. Try mapping your problem to the problem of choosing
> >> m-1 items from n-1.  E.g., your code was
> >>
> >> f0 <- function(n, m) {
> >>stopifnot(n > m)
> >>D<-matrix(0,nrow=n-m+1,ncol=m-1)
> >>for (i in 1:m-1){
> >>   D[,i]<-seq(0,n-m,1)
> >>}
> >>ED <- do.call(`expand.grid`,as.data.frame(D))
> >>ED<-unname(as.matrix(ED))
> >>lk<-which(rowSums(ED)<=(n-m))
> >>ED[lk,]
> >> }
> >>
> >> and I think the following does the same thing in much less space by
> >> transforming the output of combn().
> >>
> >> f1 <- function(n, m) {
> >>stopifnot(n > m)
> >>r0 <- t(diff(combn(n-1, m-1)) - 1L)
> >>r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1,
> >> len=n-m+1), m-2))
> >>cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0)
> >> }
> >>
> >> The code for adding the last column is a bit clumsy and could probably
> be
> >> improved.  Both f0 and f1 could also be cleaned up to work for m<=2.
> >>
> >> See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on
> >> this sort of thing.
> >>
> >>
> >>
> >> Bill Dunlap
> >> TIBCO Software
> >> wdunlap tibco.com
> >>
> >> On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem  >
> >> wrote:
> >>>
> >>> Dear All,
> >>>
> >>> I'm trying to do a simple task (which is in fact a tiny part of a
> larger
> >>> code).
> >>>
> >>> I want to create a matrix, D, each of its columns is a sequence from 0
> to
> >>> (n-m), by 1. Then, using D, I want to create another matrix ED, whose
> >>> rows
> >>> represent all the possible combinations of the elements of the columns
> of
> >>> D. Then from ED, I'll select only the rows whose sum is less than or
> >>> equal
> >>> to (n-m), which will be called the matrix s. I used the following code:
> >>>
> >>> > n=5
> >>> > m=3
> >>> > D<-matrix(0,nrow=n-m+1,ncol=m-1)
> >>> > for (i in 1:m-1)
> >>> +  {
> >>> + D[,i]<-seq(0,n-m,1)
> >>> +  }
> >>> > ED <- do.call(`expand.grid`,as.data.frame(D))
> >>> > ED<-as.matrix(ED)
> >>>
> >>> > lk<-which(rowSums(ED)<=(n-m))
> >>>
> >>> > s<-ED[lk,]
> >>>
> >>>
> >>> This works perfectly well. But for rather larger values of n and m
> (which
> >>> are not so large actually), the number of all possible combinations of
> >>> the
> >>> columns of D gets extremely large giving me this error (for n=25,
> m=15):
> >>>
> >>> > ED <- do.call(`expand.grid`,as.data.frame(D))
> >>> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >>>   invalid 'times' value
> >>> In addition: Warning message:
> >>> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >>>   NAs introduced by coercion to integer range
> >>>
> >>>
> >>> Any help or suggestions will be greatly appreciated.
> >>>
> >>> Thanks,
> >>>
> >>> Maram Salem
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> __
> >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> 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.
> >>
> >>
> >
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Scope of Axes

2015-10-20 Thread bgnumis bgnum
Hi all,

I want to plot two graphs and I use this :

par(mar=c(10,6,6,6))
matplot(Simulation,type="l")

abline(h = 0, lwd = 2, col = "black")

fhist<-hist(Simulation,plot=FALSE)
par(mar=c(6,0,6,6))
barplot(fhist$counts,axes=FALSE, space=0,horiz=TRUE,col="lightgray")


The question is, that the legth of the hist plot is not equilibrated with
the main plot so the fhist is not achiving the goal I´m looking so that it
happens what I attach in png, it seems that the right plot is central value
is below the value of the simulation. How can I ensure that de max min in
the left is the same that in the right (second) plot?
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] r-markdown - keeping figures

2015-10-20 Thread Witold E Wolski
I am running r-markdown from r-studio and can't work out how to keep
the figures.
I mean I have a few figures in the document and would like to have
them as separate pdf's too as I have been used to have them when using
Sweave.



best regards
Witold


-- 
Witold Eryk Wolski

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Error in rep.int() invalid 'times' value

2015-10-20 Thread William Dunlap
f0 is essentially your original code put into a function, so
expect it to fail in the same way your original code did.
f1 should give the same answer as f0, but it should use
less memory and time.
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Oct 20, 2015 at 2:05 AM, Maram SAlem  wrote:
> Thanks William. I've tried the first code, ( with f0() ), but still for
> n=25, m=15 , I got this:
>
>> s<-f0(25,15)
> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>   invalid 'times' value
> In addition: Warning message:
> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>   NAs introduced by coercion to integer range
>
>
> I don't know if this is related to the memory limits of my laptop, or it
> doesn't have to do with the memory.
>
> Any help on how to fix this error will be greatly appreciated.
>
> Thanks All.
>
> Maram Salem
>
> On 15 October 2015 at 17:52, William Dunlap  wrote:
>>
>> Doing enumerative combinatorics with rejection methods rarely
>> works well. Try mapping your problem to the problem of choosing
>> m-1 items from n-1.  E.g., your code was
>>
>> f0 <- function(n, m) {
>>stopifnot(n > m)
>>D<-matrix(0,nrow=n-m+1,ncol=m-1)
>>for (i in 1:m-1){
>>   D[,i]<-seq(0,n-m,1)
>>}
>>ED <- do.call(`expand.grid`,as.data.frame(D))
>>ED<-unname(as.matrix(ED))
>>lk<-which(rowSums(ED)<=(n-m))
>>ED[lk,]
>> }
>>
>> and I think the following does the same thing in much less space by
>> transforming the output of combn().
>>
>> f1 <- function(n, m) {
>>stopifnot(n > m)
>>r0 <- t(diff(combn(n-1, m-1)) - 1L)
>>r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1,
>> len=n-m+1), m-2))
>>cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0)
>> }
>>
>> The code for adding the last column is a bit clumsy and could probably be
>> improved.  Both f0 and f1 could also be cleaned up to work for m<=2.
>>
>> See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on
>> this sort of thing.
>>
>>
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem 
>> wrote:
>>>
>>> Dear All,
>>>
>>> I'm trying to do a simple task (which is in fact a tiny part of a larger
>>> code).
>>>
>>> I want to create a matrix, D, each of its columns is a sequence from 0 to
>>> (n-m), by 1. Then, using D, I want to create another matrix ED, whose
>>> rows
>>> represent all the possible combinations of the elements of the columns of
>>> D. Then from ED, I'll select only the rows whose sum is less than or
>>> equal
>>> to (n-m), which will be called the matrix s. I used the following code:
>>>
>>> > n=5
>>> > m=3
>>> > D<-matrix(0,nrow=n-m+1,ncol=m-1)
>>> > for (i in 1:m-1)
>>> +  {
>>> + D[,i]<-seq(0,n-m,1)
>>> +  }
>>> > ED <- do.call(`expand.grid`,as.data.frame(D))
>>> > ED<-as.matrix(ED)
>>>
>>> > lk<-which(rowSums(ED)<=(n-m))
>>>
>>> > s<-ED[lk,]
>>>
>>>
>>> This works perfectly well. But for rather larger values of n and m (which
>>> are not so large actually), the number of all possible combinations of
>>> the
>>> columns of D gets extremely large giving me this error (for n=25, m=15):
>>>
>>> > ED <- do.call(`expand.grid`,as.data.frame(D))
>>> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>>>   invalid 'times' value
>>> In addition: Warning message:
>>> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>>>   NAs introduced by coercion to integer range
>>>
>>>
>>> Any help or suggestions will be greatly appreciated.
>>>
>>> Thanks,
>>>
>>> Maram Salem
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> __
>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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 -- To UNSUBSCRIBE and more, see
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] Creating S4 class with a slot as an array

2015-10-20 Thread Glenn Schultz

Hello All,

I am trying to create an S4 class with a slot that is an array.  Below is the 
code I tried but I have missed something.  Any suggestions are appreciated.

setClass("CashFlow",
representation(
CashFlowArray = "array"))

setMethod("initialize",
signature = "CashFlow",
function(.Object,
CashFlowArray = "array"
){
.Object@CashFlowArray
return(.Object)
callNextMethod(.Object,...)
})
CashFlow <-function(CashFlowArray = array()){
new("CashFlow",
CashFlowArray = CashFlowArray)
}

CFTest <- array(data = 2, c(360,8))

Test <- CashFlow(CashFlowArray = CFTest)

Thanks,
Glenn
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Error in rep.int() invalid 'times' value

2015-10-20 Thread Maram SAlem
Thanks a lot Petr.

I did exactly what you did and found that f1() works for n=25 and m=15. But
I just wanted to figure out how f1() works, as I used its output for this
larger code and It has been running for almost 5 hours now.

s<-f1(25,15)

simpfun<- function (x,n,m,p,alpha,beta)

  {

  a<-factorial(n-m)/(prod((factorial(x)))*(factorial((n-m)- sum(x

  b<- vector(mode = "numeric", length = m-1)

  for ( i in 1:m-1)

   {

   b[i]<- (m-i)

   }

  c<- a*((p)^(sum(x)))*((1-p)^(((m-1)*(n-m))-sum(b%*%x)))

  d <-vector(mode = "numeric", length = m-1)

   for (i in 1:m-1)

   {

   d[i]<- n- (sum(x[(1):(i)])) - i

   }

  e<- n*(prod(d))*c

LD<-list()

   for (i in 1:(m-1))

   {

   LD[[i]]<-seq(0,x[i],1)

   }

   LD[[m]]<-seq(0,(n-m-sum(x)),1)

   LED<-expand.grid (LD)

   LED<-as.matrix(LED)

   store1<-vector(mode= "numeric",length=nrow(LED))

h<- vector(mode= "numeric", length= (m-1))

lm<- vector(mode= "numeric", length= (m-1))

 for (j in 1:length(store1) )

 {

incomb<-function(x,alpha,beta) {


 g<-((-1)^(sum(LED[j,])))*(gamma((1/beta)+1))*((alpha)^(-(1/beta)))

  for (i in 1:(m-1))

  {

   h[i]<- choose(x[i],LED[j,i])

   }

 ik<-prod(h)*choose((n-m-sum(x)),LED[j,m])

for (i in 1:(m-1))

 {

   lm[i]<-(sum(LED[j,1:i])) + i

 }

plm<-prod(lm)

   gil<-g*ik/(plm)

   hlm<-vector(mode= "numeric",length=(sum(LED[j,])+(m-1)))

 dsa<-length(hlm)

  for (i in 1:dsa)

{

 ppp<- sum(LED[j,])+(m-1)

  hlm[i]<-
 (choose(ppp,i))*((-1)^(i))*((i+1)^((-1)*((1/beta)+1)))

 }

  shl<-gil*(sum(hlm)+1)

  return (shl)

  }

   store1[j]<-incomb(x,alpha=0.2,beta=2)

  }


 val1<- sum(store1)*e

return(val1)

}

va<-apply(s,1,simpfun,n=25,m=15,p=0.3,alpha=0.2,beta=2)

EXP<-sum(va)



 I don't know if something is wrong or this is normal, but I've used R
before with looping codes but it never took too long.

Any suggestions please?
Thanks in advance.

Maram Salem

On 20 October 2015 at 15:27, PIKAL Petr  wrote:

> Hi
>
>
>
> Why are you confused?
>
>
>
> > f0(5,3)
>
>  [,1] [,2]
>
> [1,]00
>
> [2,]10
>
> [3,]20
>
> [4,]01
>
> [5,]11
>
> [6,]02
>
> > f1(5,3)
>
>  [,1] [,2]
>
> [1,]00
>
> [2,]10
>
> [3,]20
>
> [4,]01
>
> [5,]11
>
> [6,]02
>
> >
>
>
>
> seems to give the same result.
>
>
>
> > res0<-f0(25,15)
>
> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>
>   invalid 'times' value
>
> In addition: Warning message:
>
> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>
>   NAs introduced by coercion to integer range
>
>
>
> > res1<-f1(25,15)
>
>
>
> So f1 works for required m,n.
>
>
>
> William just used different approach to get the same result, which is
> indeed quite common in R.
>
>
>
> Cheers
>
> Petr
>
>
>
> *From:* Maram SAlem [mailto:marammagdysa...@gmail.com]
> *Sent:* Tuesday, October 20, 2015 1:50 PM
> *To:* PIKAL Petr
> *Cc:* r-help@r-project.org
>
> *Subject:* Re: [R] Error in rep.int() invalid 'times' value
>
>
>
> Thanks for the hint Petr.
>
>
>
> I'm just a little bit confused with the function f1(). could you please
> help me and insert comments within f1() to be able to relate it with f0()?
>
>
>
> Thanks a lot.
>
>
>
> Maram Salem
>
>
>
> On 20 October 2015 at 11:29, PIKAL Petr  wrote:
>
> Hi
>
> What about using the second function f1 which William suggested?
>
> Cheers
> Petr
>
>
>
> > -Original Message-
> > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Maram
> > SAlem
> > Sent: Tuesday, October 20, 2015 11:06 AM
> > To: William Dunlap; r-help@r-project.org
> > Subject: Re: [R] Error in rep.int() invalid 'times' value
> >
> > Thanks William. I've tried the first code, ( with f0() ), but still for
> > n=25, m=15 , I got this:
> >
> > > s<-f0(25,15)
> > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >   invalid 'times' value
> > In addition: Warning message:
> > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >   NAs introduced by coercion to integer range
> >
> >
> > I don't know if this is related to the memory limits of my laptop, or
> > it doesn't have to do with the memory.
> >
> > Any help on how to fix this error will be greatly appreciated.
> >
> > Thanks All.
> >
> > Maram Salem
> >
> > On 15 October 2015 at 17:52, William Dunlap  wrote:
> >
> > > Doing enumerative combinatorics with rejection methods rarely works
> > > well. Try mapping your problem to the problem of choosing
> > > m-1 items from n-1.  E.g., your code was
> > >
> > > f0 <- function(n, m) {
> > >stopifnot(n > m)
> > >D<-matrix(0,nrow=n-m+1,ncol=m-1)
> 

Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Berend Hasselman

> On 20 Oct 2015, at 15:35, Duncan Murdoch  wrote:
> 
> On 20/10/2015 6:58 AM, Andy Yuan wrote:
>> Hello
>> 
>> Please could you help me to select the most appropriate/fastest function to 
>> use for the following constraint optimisation issue?
> 
> Just project S into the space orthogonal to B, i.e. compute the
> residuals when you regress S on B (with no intercept).  For example,
> 
> X <- lsfit(B, S, intercept=FALSE)$residuals
> 

And  you can use an alternative like this with package nloptr using functions

library(nloptr)

f1 <- function(x) sum(crossprod(x-S))

heq <- function(x) sum(B * x)

# Check lsfit solution
f1(X)
heq(X)

slsqp(c(0,0,0),fn=f1,heq=heq)


Berend

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Duncan Murdoch
On 20/10/2015 6:58 AM, Andy Yuan wrote:
> Hello
>  
> Please could you help me to select the most appropriate/fastest function to 
> use for the following constraint optimisation issue?

Just project S into the space orthogonal to B, i.e. compute the
residuals when you regress S on B (with no intercept).  For example,

X <- lsfit(B, S, intercept=FALSE)$residuals

>  
> Objective function:
>  
> Min: Sum( (X[i] - S[i] )^2) 
>  
> Subject to constraint :
>  
> Sum (B[i] x X[i]) =0
>  
> where i=1��n and S[i] and B[i] are real numbers
>  
> Need to solve for X
>  
> Example:
>  
> Assume n=3
>  
> S <- c(-0.5, 7.8, 2.3)
> B <- c(0.42, 1.12, 0.78)
>  
> Many thanks
> AY
> 
> 
> 
>   [[alternative HTML version deleted]]
> 
> 
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] seq POSIXct and fill in data by Tagmarie

2015-10-20 Thread PIKAL Petr
Hi

Thanks for the data.

There is function na.locf in zoo package.

> library(zoo)

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

as.Date, as.Date.numeric

new$Event <- na.locf(new$Event)
  [1] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
 [19] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
 [37] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
 [55] 0.1 0.1 0.1 0.1 0.1 0.1 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
 [73] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
 [91] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
[109] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 1.2
Levels: 0.1 0.5 1.2

Beware that Event is factor variable.

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Dagmar
> Sent: Tuesday, October 20, 2015 12:37 PM
> To: r-help@r-project.org
> Subject: [R] seq POSIXct and fill in data by Tagmarie
>
> Hello,
> This must be a bloody R-beginner question but I just can't find an
> answer in my beginner books:
> I have a dataframe like this:
>
> myframe <- data.frame (Timestamp=c("24.09.2012 09:00:00", "24.09.2012
> 10:00:00", "24.09.2012 11:00:00"), Event=c("0.1","0.5","1.2") )
> myframe$Timestamp <- as.POSIXct(strptime(myframe$Timestamp,
> format="%d.%m.%Y %H:%M:%S"))
> expand<-data.frame(Timestamp=seq(myframe[1,1], myframe[3,1], by="1
> mins"))
> head(expand)
> new <- merge(expand, myframe, all=T)
> head(new)
>
> I do not only want to expand the Dates but also I want to fill in the
> "Events" (i.e. 0,1 untill the date were 0.5 starts).
> How do I do this?
> Thanks in advance!
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


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 of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person repres

Re: [R] Error in rep.int() invalid 'times' value

2015-10-20 Thread PIKAL Petr
Hi

Why are you confused?

> f0(5,3)
 [,1] [,2]
[1,]00
[2,]10
[3,]20
[4,]01
[5,]11
[6,]02
> f1(5,3)
 [,1] [,2]
[1,]00
[2,]10
[3,]20
[4,]01
[5,]11
[6,]02
>

seems to give the same result.

> res0<-f0(25,15)
Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  invalid 'times' value
In addition: Warning message:
In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  NAs introduced by coercion to integer range

> res1<-f1(25,15)

So f1 works for required m,n.

William just used different approach to get the same result, which is indeed 
quite common in R.

Cheers
Petr

From: Maram SAlem [mailto:marammagdysa...@gmail.com]
Sent: Tuesday, October 20, 2015 1:50 PM
To: PIKAL Petr
Cc: r-help@r-project.org
Subject: Re: [R] Error in rep.int() invalid 'times' value

Thanks for the hint Petr.

I'm just a little bit confused with the function f1(). could you please help me 
and insert comments within f1() to be able to relate it with f0()?

Thanks a lot.

Maram Salem

On 20 October 2015 at 11:29, PIKAL Petr 
mailto:petr.pi...@precheza.cz>> wrote:
Hi

What about using the second function f1 which William suggested?

Cheers
Petr


> -Original Message-
> From: R-help 
> [mailto:r-help-boun...@r-project.org] On 
> Behalf Of Maram
> SAlem
> Sent: Tuesday, October 20, 2015 11:06 AM
> To: William Dunlap; r-help@r-project.org
> Subject: Re: [R] Error in rep.int() invalid 'times' value
>
> Thanks William. I've tried the first code, ( with f0() ), but still for
> n=25, m=15 , I got this:
>
> > s<-f0(25,15)
> Error in rep.int(rep.int(seq_len(nx), 
> rep.int(rep.fac, nx)), orep) :
>   invalid 'times' value
> In addition: Warning message:
> In rep.int(rep.int(seq_len(nx), 
> rep.int(rep.fac, nx)), orep) :
>   NAs introduced by coercion to integer range
>
>
> I don't know if this is related to the memory limits of my laptop, or
> it doesn't have to do with the memory.
>
> Any help on how to fix this error will be greatly appreciated.
>
> Thanks All.
>
> Maram Salem
>
> On 15 October 2015 at 17:52, William Dunlap 
> mailto:wdun...@tibco.com>> wrote:
>
> > Doing enumerative combinatorics with rejection methods rarely works
> > well. Try mapping your problem to the problem of choosing
> > m-1 items from n-1.  E.g., your code was
> >
> > f0 <- function(n, m) {
> >stopifnot(n > m)
> >D<-matrix(0,nrow=n-m+1,ncol=m-1)
> >for (i in 1:m-1){
> >   D[,i]<-seq(0,n-m,1)
> >}
> >ED <- do.call(`expand.grid`,as.data.frame(D))
> >ED<-unname(as.matrix(ED))
> >lk<-which(rowSums(ED)<=(n-m))
> >ED[lk,]
> > }
> >
> > and I think the following does the same thing in much less space by
> > transforming the output of combn().
> >
> > f1 <- function(n, m) {
> >stopifnot(n > m)
> >r0 <- t(diff(combn(n-1, m-1)) - 1L)
> >r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1,
> > len=n-m+1), m-2))
> >cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) }
> >
> > The code for adding the last column is a bit clumsy and could
> probably
> > be improved.  Both f0 and f1 could also be cleaned up to work for
> m<=2.
> >
> > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more
> > on this sort of thing.
> >
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem
> > mailto:marammagdysa...@gmail.com>>
> > wrote:
> >
> >> Dear All,
> >>
> >> I'm trying to do a simple task (which is in fact a tiny part of a
> >> larger code).
> >>
> >> I want to create a matrix, D, each of its columns is a sequence from
> >> 0 to (n-m), by 1. Then, using D, I want to create another matrix ED,
> >> whose rows represent all the possible combinations of the elements
> of
> >> the columns of D. Then from ED, I'll select only the rows whose sum
> >> is less than or equal to (n-m), which will be called the matrix s. I
> used the following code:
> >>
> >> > n=5
> >> > m=3
> >> > D<-matrix(0,nrow=n-m+1,ncol=m-1)
> >> > for (i in 1:m-1)
> >> +  {
> >> + D[,i]<-seq(0,n-m,1)
> >> +  }
> >> > ED <- do.call(`expand.grid`,as.data.frame(D))
> >> > ED<-as.matrix(ED)
> >>
> >> > lk<-which(rowSums(ED)<=(n-m))
> >>
> >> > s<-ED[lk,]
> >>
> >>
> >> This works perfectly well. But for rather larger values of n and m
> >> (which are not so large actually), the number of all possible
> >> combinations of the columns of D gets extremely large giving me this
> error (for n=25, m=15):
> >>
> >> > ED <- do.call(`expand.grid`,as.data.frame(D))
> >> Error in rep.int(rep.int(seq_len(nx), 
> >> rep.int(rep.fac, nx)), orep) :
> >>   invalid 'times' value
> >> In addition: Warning message:
> >> In rep.int

[R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Andy Yuan
Hello
 
Please could you help me to select the most appropriate/fastest function to use 
for the following constraint optimisation issue?
 
Objective function:
 
Min: Sum( (X[i] - S[i] )^2) 
 
Subject to constraint :
 
Sum (B[i] x X[i]) =0
 
where i=1��n and S[i] and B[i] are real numbers
 
Need to solve for X
 
Example:
 
Assume n=3
 
S <- c(-0.5, 7.8, 2.3)
B <- c(0.42, 1.12, 0.78)
 
Many thanks
AY



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] warning on generic function when building R package

2015-10-20 Thread carol white via R-help
Jim's solution works.
Thank you

Carol 


 On Monday, October 19, 2015 11:53 PM, Jim Lemon  
wrote:
   

 I think what you may have done is simply changed x.init= to x=x.init. x.init 
may or may not be there when the function is called, and that is what the 
warning is saying. While you have satisfied the restriction that the first 
argument must be "x", but then set the default value to something that R is 
unable to resolve to a value. What you may want to do is something like this:
plot.func<-function(x,y,...) { if(missing(x)) stop("Must have an x value") 
if(missing(y)) stop("Must have a y value") ...}
Jim

On Tue, Oct 20, 2015 at 7:32 AM, carol white via R-help  
wrote:

In effect, this works
but whether I use x or x.init, y or y.init in plot.func, I get

no visible binding for global variable ‘x.init’no visible binding for global 
variable ‘y.init’
Regards,

     On Monday, October 19, 2015 9:59 PM, Duncan Murdoch 
 wrote:


 On 19/10/2015 3:50 PM, carol white wrote:
> Thanks Murdoch.
>
> defining
> plot.func<- function(x=x.init, y=y.init, arg3, arg4, "title", col, arg5)
>
> and if plot doesn't take the exact parameters of plot.func but modified
> of these parameters
> plot(x=x.pt,y=y.pt,xlim = c(0, 10), ylim = c(0,1), xlab= "xlab",
> ylab="ylab", main = "title", col = col,type = "l")
>
> then, how to define and invoke to be consisent?

I don't really understand your question, but this is all about the
function header for plot.func, not the call you make to plot().  You
need to name the first argument as "x", you need to include "..." as an
open argument, and you need a legal header.  So this would be okay:


plot.func<- function(x=x.init, y=y.init, arg3, arg4,
                    main = "title", # can't skip the arg name
                    col, arg5,
                    ...)  {          # can't skip the dots

Duncan Murdoch

>
> Regards,
>
> On Monday, October 19, 2015 7:45 PM, Duncan Murdoch
>  wrote:
>
>
> On 19/10/2015 1:29 PM, carol white via R-help wrote:
>
>> Hi,I have invoked plot in a function (plot.func) as follows but when I
> check the built package, I get a warning:
>> plot(x.pt,y.pt,xlim = c(0, 10), ylim = c(0,1), xlab= "xlab",
> ylab="ylab", main = "title", col = col,type = "l")
>> R CMD check my.package
>> checking S3 generic/method consistency ... WARNING
>> plot:
>>  function(x, ...)
>> plot.func:
>>  function(x.pt, y.pt, arg3, arg4, "title", col, arg5)
>>
>> See section ‘Generic functions and methods’ in the ‘Writing R
>> Extensions’ manual.
>> Which plot argument is illegitimate or missing and how to eliminate
> the warning?
>
>
> The first argument to plot.func needs to be called "x" if you want to
> use it as a method.  Method signatures need to be consistent with the
> generic signature.
>
> Duncan Murdoch
>
>
>
>




        [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Error in rep.int() invalid 'times' value

2015-10-20 Thread Maram SAlem
Thanks for the hint Petr.

I'm just a little bit confused with the function f1(). could you please
help me and insert comments within f1() to be able to relate it with f0()?

Thanks a lot.

Maram Salem

On 20 October 2015 at 11:29, PIKAL Petr  wrote:

> Hi
>
> What about using the second function f1 which William suggested?
>
> Cheers
> Petr
>
>
> > -Original Message-
> > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Maram
> > SAlem
> > Sent: Tuesday, October 20, 2015 11:06 AM
> > To: William Dunlap; r-help@r-project.org
> > Subject: Re: [R] Error in rep.int() invalid 'times' value
> >
> > Thanks William. I've tried the first code, ( with f0() ), but still for
> > n=25, m=15 , I got this:
> >
> > > s<-f0(25,15)
> > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >   invalid 'times' value
> > In addition: Warning message:
> > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >   NAs introduced by coercion to integer range
> >
> >
> > I don't know if this is related to the memory limits of my laptop, or
> > it doesn't have to do with the memory.
> >
> > Any help on how to fix this error will be greatly appreciated.
> >
> > Thanks All.
> >
> > Maram Salem
> >
> > On 15 October 2015 at 17:52, William Dunlap  wrote:
> >
> > > Doing enumerative combinatorics with rejection methods rarely works
> > > well. Try mapping your problem to the problem of choosing
> > > m-1 items from n-1.  E.g., your code was
> > >
> > > f0 <- function(n, m) {
> > >stopifnot(n > m)
> > >D<-matrix(0,nrow=n-m+1,ncol=m-1)
> > >for (i in 1:m-1){
> > >   D[,i]<-seq(0,n-m,1)
> > >}
> > >ED <- do.call(`expand.grid`,as.data.frame(D))
> > >ED<-unname(as.matrix(ED))
> > >lk<-which(rowSums(ED)<=(n-m))
> > >ED[lk,]
> > > }
> > >
> > > and I think the following does the same thing in much less space by
> > > transforming the output of combn().
> > >
> > > f1 <- function(n, m) {
> > >stopifnot(n > m)
> > >r0 <- t(diff(combn(n-1, m-1)) - 1L)
> > >r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1,
> > > len=n-m+1), m-2))
> > >cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) }
> > >
> > > The code for adding the last column is a bit clumsy and could
> > probably
> > > be improved.  Both f0 and f1 could also be cleaned up to work for
> > m<=2.
> > >
> > > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more
> > > on this sort of thing.
> > >
> > >
> > >
> > > Bill Dunlap
> > > TIBCO Software
> > > wdunlap tibco.com
> > >
> > > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem
> > > 
> > > wrote:
> > >
> > >> Dear All,
> > >>
> > >> I'm trying to do a simple task (which is in fact a tiny part of a
> > >> larger code).
> > >>
> > >> I want to create a matrix, D, each of its columns is a sequence from
> > >> 0 to (n-m), by 1. Then, using D, I want to create another matrix ED,
> > >> whose rows represent all the possible combinations of the elements
> > of
> > >> the columns of D. Then from ED, I'll select only the rows whose sum
> > >> is less than or equal to (n-m), which will be called the matrix s. I
> > used the following code:
> > >>
> > >> > n=5
> > >> > m=3
> > >> > D<-matrix(0,nrow=n-m+1,ncol=m-1)
> > >> > for (i in 1:m-1)
> > >> +  {
> > >> + D[,i]<-seq(0,n-m,1)
> > >> +  }
> > >> > ED <- do.call(`expand.grid`,as.data.frame(D))
> > >> > ED<-as.matrix(ED)
> > >>
> > >> > lk<-which(rowSums(ED)<=(n-m))
> > >>
> > >> > s<-ED[lk,]
> > >>
> > >>
> > >> This works perfectly well. But for rather larger values of n and m
> > >> (which are not so large actually), the number of all possible
> > >> combinations of the columns of D gets extremely large giving me this
> > error (for n=25, m=15):
> > >>
> > >> > ED <- do.call(`expand.grid`,as.data.frame(D))
> > >> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> > >>   invalid 'times' value
> > >> In addition: Warning message:
> > >> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> > >>   NAs introduced by coercion to integer range
> > >>
> > >>
> > >> Any help or suggestions will be greatly appreciated.
> > >>
> > >> Thanks,
> > >>
> > >> Maram Salem
> > >>
> > >> [[alternative HTML version deleted]]
> > >>
> > >> __
> > >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >> 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.
> > >>
> > >
> > >
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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, rep

[R] seq POSIXct and fill in data by Tagmarie

2015-10-20 Thread Dagmar

Hello,
This must be a bloody R-beginner question but I just can't find an 
answer in my beginner books:

I have a dataframe like this:

myframe <- data.frame (Timestamp=c("24.09.2012 09:00:00", "24.09.2012 
10:00:00", "24.09.2012 11:00:00"), Event=c("0.1","0.5","1.2") )
myframe$Timestamp <- as.POSIXct(strptime(myframe$Timestamp, 
format="%d.%m.%Y %H:%M:%S"))

expand<-data.frame(Timestamp=seq(myframe[1,1], myframe[3,1], by="1 mins"))
head(expand)
new <- merge(expand, myframe, all=T)
head(new)

I do not only want to expand the Dates but also I want to fill in the 
"Events" (i.e. 0,1 untill the date were 0.5 starts).

How do I do this?
Thanks in advance!

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Error in rep.int() invalid 'times' value

2015-10-20 Thread PIKAL Petr
Hi

What about using the second function f1 which William suggested?

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Maram
> SAlem
> Sent: Tuesday, October 20, 2015 11:06 AM
> To: William Dunlap; r-help@r-project.org
> Subject: Re: [R] Error in rep.int() invalid 'times' value
>
> Thanks William. I've tried the first code, ( with f0() ), but still for
> n=25, m=15 , I got this:
>
> > s<-f0(25,15)
> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>   invalid 'times' value
> In addition: Warning message:
> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>   NAs introduced by coercion to integer range
>
>
> I don't know if this is related to the memory limits of my laptop, or
> it doesn't have to do with the memory.
>
> Any help on how to fix this error will be greatly appreciated.
>
> Thanks All.
>
> Maram Salem
>
> On 15 October 2015 at 17:52, William Dunlap  wrote:
>
> > Doing enumerative combinatorics with rejection methods rarely works
> > well. Try mapping your problem to the problem of choosing
> > m-1 items from n-1.  E.g., your code was
> >
> > f0 <- function(n, m) {
> >stopifnot(n > m)
> >D<-matrix(0,nrow=n-m+1,ncol=m-1)
> >for (i in 1:m-1){
> >   D[,i]<-seq(0,n-m,1)
> >}
> >ED <- do.call(`expand.grid`,as.data.frame(D))
> >ED<-unname(as.matrix(ED))
> >lk<-which(rowSums(ED)<=(n-m))
> >ED[lk,]
> > }
> >
> > and I think the following does the same thing in much less space by
> > transforming the output of combn().
> >
> > f1 <- function(n, m) {
> >stopifnot(n > m)
> >r0 <- t(diff(combn(n-1, m-1)) - 1L)
> >r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1,
> > len=n-m+1), m-2))
> >cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) }
> >
> > The code for adding the last column is a bit clumsy and could
> probably
> > be improved.  Both f0 and f1 could also be cleaned up to work for
> m<=2.
> >
> > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more
> > on this sort of thing.
> >
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem
> > 
> > wrote:
> >
> >> Dear All,
> >>
> >> I'm trying to do a simple task (which is in fact a tiny part of a
> >> larger code).
> >>
> >> I want to create a matrix, D, each of its columns is a sequence from
> >> 0 to (n-m), by 1. Then, using D, I want to create another matrix ED,
> >> whose rows represent all the possible combinations of the elements
> of
> >> the columns of D. Then from ED, I'll select only the rows whose sum
> >> is less than or equal to (n-m), which will be called the matrix s. I
> used the following code:
> >>
> >> > n=5
> >> > m=3
> >> > D<-matrix(0,nrow=n-m+1,ncol=m-1)
> >> > for (i in 1:m-1)
> >> +  {
> >> + D[,i]<-seq(0,n-m,1)
> >> +  }
> >> > ED <- do.call(`expand.grid`,as.data.frame(D))
> >> > ED<-as.matrix(ED)
> >>
> >> > lk<-which(rowSums(ED)<=(n-m))
> >>
> >> > s<-ED[lk,]
> >>
> >>
> >> This works perfectly well. But for rather larger values of n and m
> >> (which are not so large actually), the number of all possible
> >> combinations of the columns of D gets extremely large giving me this
> error (for n=25, m=15):
> >>
> >> > ED <- do.call(`expand.grid`,as.data.frame(D))
> >> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >>   invalid 'times' value
> >> In addition: Warning message:
> >> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
> >>   NAs introduced by coercion to integer range
> >>
> >>
> >> Any help or suggestions will be greatly appreciated.
> >>
> >> Thanks,
> >>
> >> Maram Salem
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> __
> >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >>
> >
> >
>
>   [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


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řen

Re: [R] Error in rep.int() invalid 'times' value

2015-10-20 Thread Maram SAlem
Thanks William. I've tried the first code, ( with f0() ), but still for
n=25, m=15 , I got this:

> s<-f0(25,15)
Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  invalid 'times' value
In addition: Warning message:
In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  NAs introduced by coercion to integer range


I don't know if this is related to the memory limits of my laptop, or it
doesn't have to do with the memory.

Any help on how to fix this error will be greatly appreciated.

Thanks All.

Maram Salem

On 15 October 2015 at 17:52, William Dunlap  wrote:

> Doing enumerative combinatorics with rejection methods rarely
> works well. Try mapping your problem to the problem of choosing
> m-1 items from n-1.  E.g., your code was
>
> f0 <- function(n, m) {
>stopifnot(n > m)
>D<-matrix(0,nrow=n-m+1,ncol=m-1)
>for (i in 1:m-1){
>   D[,i]<-seq(0,n-m,1)
>}
>ED <- do.call(`expand.grid`,as.data.frame(D))
>ED<-unname(as.matrix(ED))
>lk<-which(rowSums(ED)<=(n-m))
>ED[lk,]
> }
>
> and I think the following does the same thing in much less space by
> transforming the output of combn().
>
> f1 <- function(n, m) {
>stopifnot(n > m)
>r0 <- t(diff(combn(n-1, m-1)) - 1L)
>r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1,
> len=n-m+1), m-2))
>cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0)
> }
>
> The code for adding the last column is a bit clumsy and could probably be
> improved.  Both f0 and f1 could also be cleaned up to work for m<=2.
>
> See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on
> this sort of thing.
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem 
> wrote:
>
>> Dear All,
>>
>> I'm trying to do a simple task (which is in fact a tiny part of a larger
>> code).
>>
>> I want to create a matrix, D, each of its columns is a sequence from 0 to
>> (n-m), by 1. Then, using D, I want to create another matrix ED, whose rows
>> represent all the possible combinations of the elements of the columns of
>> D. Then from ED, I'll select only the rows whose sum is less than or equal
>> to (n-m), which will be called the matrix s. I used the following code:
>>
>> > n=5
>> > m=3
>> > D<-matrix(0,nrow=n-m+1,ncol=m-1)
>> > for (i in 1:m-1)
>> +  {
>> + D[,i]<-seq(0,n-m,1)
>> +  }
>> > ED <- do.call(`expand.grid`,as.data.frame(D))
>> > ED<-as.matrix(ED)
>>
>> > lk<-which(rowSums(ED)<=(n-m))
>>
>> > s<-ED[lk,]
>>
>>
>> This works perfectly well. But for rather larger values of n and m (which
>> are not so large actually), the number of all possible combinations of the
>> columns of D gets extremely large giving me this error (for n=25, m=15):
>>
>> > ED <- do.call(`expand.grid`,as.data.frame(D))
>> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>>   invalid 'times' value
>> In addition: Warning message:
>> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>>   NAs introduced by coercion to integer range
>>
>>
>> Any help or suggestions will be greatly appreciated.
>>
>> Thanks,
>>
>> Maram Salem
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] leveneTest between 2-levels of 3-level factor in R?

2015-10-20 Thread Dr. Leigh S. Sutherland
Thanks Rolf,

Sorry, I was trying to be coherent, but obviously failed abysmally!
I am new to this list, I will give a reproducible example next time, and
more of the relevant script.

I managed to do what I wanted to with subset:

leveneTest(subset(results,ADHES == 'Poly'|ADHES == 'Cryst')[,5],
 subset(results,ADHES == 'Poly'|ADHES == 'Cryst')[,3])

which gives the same results as your suggestion, which I can now look into
how it works

many thanks.

Leigh

On 19 October 2015 at 22:13, Rolf Turner  wrote:

> On 20/10/15 01:02, Dr. Leigh S. Sutherland wrote:
>
>> Still feeling my way around using R...
>>
>> What I have done so far:
>>
>> I have a data.frame 'results' with response 'Fail', and three factors
>> 'PREP', 'CLEAN' & 'ADHES'. ADHES has 3 levels: Crest Cryst Poly
>>
>> I calculated the variances:
>>
>> sigma..k=tapply(Fail,ADHES,var)
>> print(sqrt(sigma..k)):
>>
>> CrestCryst Poly 17.56668 41.64679 39.42669
>>
>> then used leveneTest to test for constance of variance:
>>
>> print(leveneTest(Fail~ADHES))
>>
>
> How does leveneTest() find the variables "Fail" and "ADHES" given that you
> do not provide it with a "data" argument?
>
> Levene's Test for Homogeneity of Variance (center = median)
>>Df F value  Pr(>F)
>> group  2   3.929 0.02588 *
>>51
>>
>> The Question:
>> Now I want to use Levene's to test for constance of variance between only
>> the Cryst & Poly levels of the factor ADHES, but I cant work out the
>> syntax
>> to do this in R.
>>
>> Could anyone help, please?
>>
>
> I am not at all sure that I understand your somewhat incoherently
> expressed question, but I *think* that what you want to do might be done by
> setting
>
>ok <- with(results,ADHES%in%c("Cryst","Poly"))
>leveneTest(Fail ~ ADHES, data=results[ok,])
>
> It's hard to say, since you do not provide a reproducible example.
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>



-- 
Leigh Sutherland

Centre for Marine Technology and Ocean Engineering (CENTEC)
Instituto Superior Técnico
Av. Rovisco Pais
1049-001 LISBOA
PORTUGAL

Tel: +351 218 417 947

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.