struggling with apply

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

struggling with apply

Michael Ashton-3
Hi -

I have a matrix of n rows and 4 columns.

I want to cap the value in each column by a different upper bound. So, suppose my matrix is

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> somematrix
     [,1] [,2] [,3] [,4]
[1,]    1    6   12    7
[2,]    4    3    8   11
[3,]    3    9    5   11

Now I want to have the maximum value in each column described by
UB=c(2.5, 5.5, 8.5, 10.5)

So that the right answer will look like:
     [,1]      [,2]    [,3]   [,4]
[1,]    1      5.5     8.5    7
[2,]    2.5    3        8     10.5
[3,]    2.5   5.5      5    10.5

I've tried a few things, like:
newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))

but I can't figure out to apply the relevant element of the UB list to the right element of the matrix. When I run the above, for example, it takes min(UB,x) over all UB, so I get:

newmatrix
     [,1] [,2] [,3] [,4]
[1,]  1.0  2.5  2.5  2.5
[2,]  2.5  2.5  2.5  2.5
[3,]  2.5  2.5  2.5  2.5

I'm sure there's a simple and elegant solution but I don't know what it is!

Thanks in advance,

Mike

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Jeff Newmiller
Use the pmin function, not min, for this purpose.

On May 27, 2020 10:46:13 AM PDT, Michael Ashton <[hidden email]> wrote:

>Hi -
>
>I have a matrix of n rows and 4 columns.
>
>I want to cap the value in each column by a different upper bound. So,
>suppose my matrix is
>
>somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>     [,1] [,2] [,3] [,4]
>[1,]    1    6   12    7
>[2,]    4    3    8   11
>[3,]    3    9    5   11
>
>Now I want to have the maximum value in each column described by
>UB=c(2.5, 5.5, 8.5, 10.5)
>
>So that the right answer will look like:
>     [,1]      [,2]    [,3]   [,4]
>[1,]    1      5.5     8.5    7
>[2,]    2.5    3        8     10.5
>[3,]    2.5   5.5      5    10.5
>
>I've tried a few things, like:
>newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
>but I can't figure out to apply the relevant element of the UB list to
>the right element of the matrix. When I run the above, for example, it
>takes min(UB,x) over all UB, so I get:
>
>newmatrix
>     [,1] [,2] [,3] [,4]
>[1,]  1.0  2.5  2.5  2.5
>[2,]  2.5  2.5  2.5  2.5
>[3,]  2.5  2.5  2.5  2.5
>
>I'm sure there's a simple and elegant solution but I don't know what it
>is!
>
>Thanks in advance,
>
>Mike
>
>Michael Ashton, CFA
>Managing Principal
>
>Enduring Investments LLC
>W: 973.457.4602
>C: 551.655.8006
>Schedule a Call: https://calendly.com/m-ashton
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Rui Barradas
In reply to this post by Michael Ashton-3
Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
#     [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:

> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound. So, suppose my matrix is
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>       [,1] [,2] [,3] [,4]
> [1,]    1    6   12    7
> [2,]    4    3    8   11
> [3,]    3    9    5   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>       [,1]      [,2]    [,3]   [,4]
> [1,]    1      5.5     8.5    7
> [2,]    2.5    3        8     10.5
> [3,]    2.5   5.5      5    10.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list to the right element of the matrix. When I run the above, for example, it takes min(UB,x) over all UB, so I get:
>
> newmatrix
>       [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know what it is!
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Michael Ashton-3
That's it! Thanks. Learn something new every day!

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


-----Original Message-----
From: Rui Barradas [mailto:[hidden email]]
Sent: Wednesday, May 27, 2020 1:51 PM
To: Michael Ashton; [hidden email]
Subject: Re: [R] struggling with apply

Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
#     [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:

> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound. So, suppose my matrix is
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>       [,1] [,2] [,3] [,4]
> [1,]    1    6   12    7
> [2,]    4    3    8   11
> [3,]    3    9    5   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>       [,1]      [,2]    [,3]   [,4]
> [1,]    1      5.5     8.5    7
> [2,]    2.5    3        8     10.5
> [3,]    2.5   5.5      5    10.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list to the right element of the matrix. When I run the above, for example, it takes min(UB,x) over all UB, so I get:
>
> newmatrix
>       [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know what it is!
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] struggling with apply

Richard M. Heiberger
In reply to this post by Michael Ashton-3
sapply(1:4, FUN=function(i, x, UB=c(2.5, 5.5, 8.5, 10.5)) {result <-
x[,i]; result[result > UB[i]] <- UB[i]; result}, x=somematrix)

On Wed, May 27, 2020 at 1:46 PM Michael Ashton
<[hidden email]> wrote:

>
> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound. So, suppose my matrix is
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> > somematrix
>      [,1] [,2] [,3] [,4]
> [1,]    1    6   12    7
> [2,]    4    3    8   11
> [3,]    3    9    5   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>      [,1]      [,2]    [,3]   [,4]
> [1,]    1      5.5     8.5    7
> [2,]    2.5    3        8     10.5
> [3,]    2.5   5.5      5    10.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list to the right element of the matrix. When I run the above, for example, it takes min(UB,x) over all UB, so I get:
>
> newmatrix
>      [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know what it is!
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Bert Gunter-2
In reply to this post by Rui Barradas
Better, I think (no indexing):

t(apply(somematrix,1,function(x)pmin(x,UB)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Try pmin. And loop by column/UB index with sapply/seq_along.
>
>
> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
> #     [,1] [,2] [,3] [,4]
> #[1,]  1.0  5.5  8.5  7.0
> #[2,]  2.5  3.0  8.0 10.5
> #[3,]  2.5  5.5  5.0 10.5
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 18:46 de 27/05/20, Michael Ashton escreveu:
> > Hi -
> >
> > I have a matrix of n rows and 4 columns.
> >
> > I want to cap the value in each column by a different upper bound. So,
> suppose my matrix is
> >
> > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> >> somematrix
> >       [,1] [,2] [,3] [,4]
> > [1,]    1    6   12    7
> > [2,]    4    3    8   11
> > [3,]    3    9    5   11
> >
> > Now I want to have the maximum value in each column described by
> > UB=c(2.5, 5.5, 8.5, 10.5)
> >
> > So that the right answer will look like:
> >       [,1]      [,2]    [,3]   [,4]
> > [1,]    1      5.5     8.5    7
> > [2,]    2.5    3        8     10.5
> > [3,]    2.5   5.5      5    10.5
> >
> > I've tried a few things, like:
> > newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
> >
> > but I can't figure out to apply the relevant element of the UB list to
> the right element of the matrix. When I run the above, for example, it
> takes min(UB,x) over all UB, so I get:
> >
> > newmatrix
> >       [,1] [,2] [,3] [,4]
> > [1,]  1.0  2.5  2.5  2.5
> > [2,]  2.5  2.5  2.5  2.5
> > [3,]  2.5  2.5  2.5  2.5
> >
> > I'm sure there's a simple and elegant solution but I don't know what it
> is!
> >
> > Thanks in advance,
> >
> > Mike
> >
> > Michael Ashton, CFA
> > Managing Principal
> >
> > Enduring Investments LLC
> > W: 973.457.4602
> > C: 551.655.8006
> > Schedule a Call: https://calendly.com/m-ashton
> >
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Michael Ashton-3
Always amazes me how many ways there are to do these things, none of which I was able to find myself. Thanks! I think the key here was ‘pmin,’ which I didn’t know before.

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton

From: Bert Gunter [mailto:[hidden email]]
Sent: Wednesday, May 27, 2020 2:22 PM
To: Rui Barradas
Cc: Michael Ashton; [hidden email]
Subject: Re: [R] struggling with apply

Better, I think (no indexing):

t(apply(somematrix,1,function(x)pmin(x,UB)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]<mailto:[hidden email]>> wrote:
Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
#     [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:

> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound. So, suppose my matrix is
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>       [,1] [,2] [,3] [,4]
> [1,]    1    6   12    7
> [2,]    4    3    8   11
> [3,]    3    9    5   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>       [,1]      [,2]    [,3]   [,4]
> [1,]    1      5.5     8.5    7
> [2,]    2.5    3        8     10.5
> [3,]    2.5   5.5      5    10.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list to the right element of the matrix. When I run the above, for example, it takes min(UB,x) over all UB, so I get:
>
> newmatrix
>       [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know what it is!
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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.
>

______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Jeff Newmiller
In reply to this post by Bert Gunter-2
Sigh. Transpose?

apply( somematrix, 2, function( x ) pmin( x, UB ) )

On May 27, 2020 11:22:06 AM PDT, Bert Gunter <[hidden email]> wrote:

>Better, I think (no indexing):
>
>t(apply(somematrix,1,function(x)pmin(x,UB)))
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and
>sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]>
>wrote:
>
>> Hello,
>>
>> Try pmin. And loop by column/UB index with sapply/seq_along.
>>
>>
>> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
>> #     [,1] [,2] [,3] [,4]
>> #[1,]  1.0  5.5  8.5  7.0
>> #[2,]  2.5  3.0  8.0 10.5
>> #[3,]  2.5  5.5  5.0 10.5
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> Às 18:46 de 27/05/20, Michael Ashton escreveu:
>> > Hi -
>> >
>> > I have a matrix of n rows and 4 columns.
>> >
>> > I want to cap the value in each column by a different upper bound.
>So,
>> suppose my matrix is
>> >
>> > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> >> somematrix
>> >       [,1] [,2] [,3] [,4]
>> > [1,]    1    6   12    7
>> > [2,]    4    3    8   11
>> > [3,]    3    9    5   11
>> >
>> > Now I want to have the maximum value in each column described by
>> > UB=c(2.5, 5.5, 8.5, 10.5)
>> >
>> > So that the right answer will look like:
>> >       [,1]      [,2]    [,3]   [,4]
>> > [1,]    1      5.5     8.5    7
>> > [2,]    2.5    3        8     10.5
>> > [3,]    2.5   5.5      5    10.5
>> >
>> > I've tried a few things, like:
>> > newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>> >
>> > but I can't figure out to apply the relevant element of the UB list
>to
>> the right element of the matrix. When I run the above, for example,
>it
>> takes min(UB,x) over all UB, so I get:
>> >
>> > newmatrix
>> >       [,1] [,2] [,3] [,4]
>> > [1,]  1.0  2.5  2.5  2.5
>> > [2,]  2.5  2.5  2.5  2.5
>> > [3,]  2.5  2.5  2.5  2.5
>> >
>> > I'm sure there's a simple and elegant solution but I don't know
>what it
>> is!
>> >
>> > Thanks in advance,
>> >
>> > Mike
>> >
>> > Michael Ashton, CFA
>> > Managing Principal
>> >
>> > Enduring Investments LLC
>> > W: 973.457.4602
>> > C: 551.655.8006
>> > Schedule a Call: https://calendly.com/m-ashton
>> >
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > [hidden email] 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.
>> >
>>
>> ______________________________________________
>> [hidden email] 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]]
>
>______________________________________________
>[hidden email] 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.

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Bert Gunter-2
Jeff: Check it!

> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> UB=c(2.5, 5.5, 8.5, 10.5)
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
     [,1] [,2] [,3] [,4]
[1,]    1  2.5  2.5  2.5
[2,]    4  3.0  5.5  5.5
[3,]    3  8.5  5.0  8.5
[4,]    1  6.0 10.5  7.0

Not what was wanted.
Am I missing something?

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller <[hidden email]>
wrote:

> Sigh. Transpose?
>
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>
> On May 27, 2020 11:22:06 AM PDT, Bert Gunter <[hidden email]>
> wrote:
> >Better, I think (no indexing):
> >
> >t(apply(somematrix,1,function(x)pmin(x,UB)))
> >
> >
> >Bert Gunter
> >
> >"The trouble with having an open mind is that people keep coming along
> >and
> >sticking things into it."
> >-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> >On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]>
> >wrote:
> >
> >> Hello,
> >>
> >> Try pmin. And loop by column/UB index with sapply/seq_along.
> >>
> >>
> >> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
> >> #     [,1] [,2] [,3] [,4]
> >> #[1,]  1.0  5.5  8.5  7.0
> >> #[2,]  2.5  3.0  8.0 10.5
> >> #[3,]  2.5  5.5  5.0 10.5
> >>
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >>
> >> Às 18:46 de 27/05/20, Michael Ashton escreveu:
> >> > Hi -
> >> >
> >> > I have a matrix of n rows and 4 columns.
> >> >
> >> > I want to cap the value in each column by a different upper bound.
> >So,
> >> suppose my matrix is
> >> >
> >> > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> >> >> somematrix
> >> >       [,1] [,2] [,3] [,4]
> >> > [1,]    1    6   12    7
> >> > [2,]    4    3    8   11
> >> > [3,]    3    9    5   11
> >> >
> >> > Now I want to have the maximum value in each column described by
> >> > UB=c(2.5, 5.5, 8.5, 10.5)
> >> >
> >> > So that the right answer will look like:
> >> >       [,1]      [,2]    [,3]   [,4]
> >> > [1,]    1      5.5     8.5    7
> >> > [2,]    2.5    3        8     10.5
> >> > [3,]    2.5   5.5      5    10.5
> >> >
> >> > I've tried a few things, like:
> >> > newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
> >> >
> >> > but I can't figure out to apply the relevant element of the UB list
> >to
> >> the right element of the matrix. When I run the above, for example,
> >it
> >> takes min(UB,x) over all UB, so I get:
> >> >
> >> > newmatrix
> >> >       [,1] [,2] [,3] [,4]
> >> > [1,]  1.0  2.5  2.5  2.5
> >> > [2,]  2.5  2.5  2.5  2.5
> >> > [3,]  2.5  2.5  2.5  2.5
> >> >
> >> > I'm sure there's a simple and elegant solution but I don't know
> >what it
> >> is!
> >> >
> >> > Thanks in advance,
> >> >
> >> > Mike
> >> >
> >> > Michael Ashton, CFA
> >> > Managing Principal
> >> >
> >> > Enduring Investments LLC
> >> > W: 973.457.4602
> >> > C: 551.655.8006
> >> > Schedule a Call: https://calendly.com/m-ashton
> >> >
> >> >
> >> >       [[alternative HTML version deleted]]
> >> >
> >> > ______________________________________________
> >> > [hidden email] 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.
> >> >
> >>
> >> ______________________________________________
> >> [hidden email] 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]]
> >
> >______________________________________________
> >[hidden email] 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.
>
> --
> Sent from my phone. Please excuse my brevity.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Mathew Guilfoyle
A bit quicker:

t(pmin(t(somematrix), UB))



> On 27 May 2020, at 20:56, Bert Gunter <[hidden email]> wrote:
>
> Jeff: Check it!
>
>> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> UB=c(2.5, 5.5, 8.5, 10.5)
>> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>     [,1] [,2] [,3] [,4]
> [1,]    1  2.5  2.5  2.5
> [2,]    4  3.0  5.5  5.5
> [3,]    3  8.5  5.0  8.5
> [4,]    1  6.0 10.5  7.0
>
> Not what was wanted.
> Am I missing something?
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller <[hidden email] <mailto:[hidden email]>>
> wrote:
>
>> Sigh. Transpose?
>>
>> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>>
>> On May 27, 2020 11:22:06 AM PDT, Bert Gunter <[hidden email]>
>> wrote:
>>> Better, I think (no indexing):
>>>
>>> t(apply(somematrix,1,function(x)pmin(x,UB)))
>>>
>>>
>>> Bert Gunter
>>>
>>> "The trouble with having an open mind is that people keep coming along
>>> and
>>> sticking things into it."
>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>>
>>>
>>> On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]>
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> Try pmin. And loop by column/UB index with sapply/seq_along.
>>>>
>>>>
>>>> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
>>>> #     [,1] [,2] [,3] [,4]
>>>> #[1,]  1.0  5.5  8.5  7.0
>>>> #[2,]  2.5  3.0  8.0 10.5
>>>> #[3,]  2.5  5.5  5.0 10.5
>>>>
>>>>
>>>> Hope this helps,
>>>>
>>>> Rui Barradas
>>>>
>>>>
>>>> Às 18:46 de 27/05/20, Michael Ashton escreveu:
>>>>> Hi -
>>>>>
>>>>> I have a matrix of n rows and 4 columns.
>>>>>
>>>>> I want to cap the value in each column by a different upper bound.
>>> So,
>>>> suppose my matrix is
>>>>>
>>>>> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>>>>>> somematrix
>>>>>      [,1] [,2] [,3] [,4]
>>>>> [1,]    1    6   12    7
>>>>> [2,]    4    3    8   11
>>>>> [3,]    3    9    5   11
>>>>>
>>>>> Now I want to have the maximum value in each column described by
>>>>> UB=c(2.5, 5.5, 8.5, 10.5)
>>>>>
>>>>> So that the right answer will look like:
>>>>>      [,1]      [,2]    [,3]   [,4]
>>>>> [1,]    1      5.5     8.5    7
>>>>> [2,]    2.5    3        8     10.5
>>>>> [3,]    2.5   5.5      5    10.5
>>>>>
>>>>> I've tried a few things, like:
>>>>> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>>>>>
>>>>> but I can't figure out to apply the relevant element of the UB list
>>> to
>>>> the right element of the matrix. When I run the above, for example,
>>> it
>>>> takes min(UB,x) over all UB, so I get:
>>>>>
>>>>> newmatrix
>>>>>      [,1] [,2] [,3] [,4]
>>>>> [1,]  1.0  2.5  2.5  2.5
>>>>> [2,]  2.5  2.5  2.5  2.5
>>>>> [3,]  2.5  2.5  2.5  2.5
>>>>>
>>>>> I'm sure there's a simple and elegant solution but I don't know
>>> what it
>>>> is!
>>>>>
>>>>> Thanks in advance,
>>>>>
>>>>> Mike
>>>>>
>>>>> Michael Ashton, CFA
>>>>> Managing Principal
>>>>>
>>>>> Enduring Investments LLC
>>>>> W: 973.457.4602
>>>>> C: 551.655.8006
>>>>> Schedule a Call: https://calendly.com/m-ashton
>>>>>
>>>>>
>>>>>      [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>>>>
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] <mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Michael Ashton-3
This is like "Name that Tune." Can anyone do it in FEWER characters? :-)

On May 27, 2020, at 4:32 PM, Mathew Guilfoyle <[hidden email]> wrote:

 A bit quicker:

t(pmin(t(somematrix), UB))



On 27 May 2020, at 20:56, Bert Gunter <[hidden email]<mailto:[hidden email]>> wrote:

Jeff: Check it!

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
UB=c(2.5, 5.5, 8.5, 10.5)
apply( somematrix, 2, function( x ) pmin( x, UB ) )
    [,1] [,2] [,3] [,4]
[1,]    1  2.5  2.5  2.5
[2,]    4  3.0  5.5  5.5
[3,]    3  8.5  5.0  8.5
[4,]    1  6.0 10.5  7.0

Not what was wanted.
Am I missing something?

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller <[hidden email]<mailto:[hidden email]>>
wrote:

Sigh. Transpose?

apply( somematrix, 2, function( x ) pmin( x, UB ) )

On May 27, 2020 11:22:06 AM PDT, Bert Gunter <[hidden email]<mailto:[hidden email]>>
wrote:
Better, I think (no indexing):

t(apply(somematrix,1,function(x)pmin(x,UB)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]<mailto:[hidden email]>>
wrote:

Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
#     [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:
Hi -

I have a matrix of n rows and 4 columns.

I want to cap the value in each column by a different upper bound.
So,
suppose my matrix is

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
somematrix
     [,1] [,2] [,3] [,4]
[1,]    1    6   12    7
[2,]    4    3    8   11
[3,]    3    9    5   11

Now I want to have the maximum value in each column described by
UB=c(2.5, 5.5, 8.5, 10.5)

So that the right answer will look like:
     [,1]      [,2]    [,3]   [,4]
[1,]    1      5.5     8.5    7
[2,]    2.5    3        8     10.5
[3,]    2.5   5.5      5    10.5

I've tried a few things, like:
newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))

but I can't figure out to apply the relevant element of the UB list
to
the right element of the matrix. When I run the above, for example,
it
takes min(UB,x) over all UB, so I get:

newmatrix
     [,1] [,2] [,3] [,4]
[1,]  1.0  2.5  2.5  2.5
[2,]  2.5  2.5  2.5  2.5
[3,]  2.5  2.5  2.5  2.5

I'm sure there's a simple and elegant solution but I don't know
what it
is!

Thanks in advance,

Mike

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


     [[alternative HTML version deleted]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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.


______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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.

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


[[alternative HTML version deleted]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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<http://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: struggling with apply

Bert Gunter-2
In reply to this post by Mathew Guilfoyle
Yes, that's better --- no looping at the interpreted level.

Another version without transposing is:

nr <- 3
matrix(pmin(c(somematrix),rep(UB, e = nr)), nrow = nr)

Both treat the matrix as a vector stored in column major order.

Cheers,
Bert


On Wed, May 27, 2020 at 1:32 PM Mathew Guilfoyle <[hidden email]>
wrote:

> A bit quicker:
>
> t(pmin(t(somematrix), UB))
>
>
>
> On 27 May 2020, at 20:56, Bert Gunter <[hidden email]> wrote:
>
> Jeff: Check it!
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> UB=c(2.5, 5.5, 8.5, 10.5)
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>
>     [,1] [,2] [,3] [,4]
> [1,]    1  2.5  2.5  2.5
> [2,]    4  3.0  5.5  5.5
> [3,]    3  8.5  5.0  8.5
> [4,]    1  6.0 10.5  7.0
>
> Not what was wanted.
> Am I missing something?
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller <[hidden email]>
> wrote:
>
> Sigh. Transpose?
>
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>
> On May 27, 2020 11:22:06 AM PDT, Bert Gunter <[hidden email]>
> wrote:
>
> Better, I think (no indexing):
>
> t(apply(somematrix,1,function(x)pmin(x,UB)))
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, May 27, 2020 at 10:56 AM Rui Barradas <[hidden email]>
> wrote:
>
> Hello,
>
> Try pmin. And loop by column/UB index with sapply/seq_along.
>
>
> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
> #     [,1] [,2] [,3] [,4]
> #[1,]  1.0  5.5  8.5  7.0
> #[2,]  2.5  3.0  8.0 10.5
> #[3,]  2.5  5.5  5.0 10.5
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 18:46 de 27/05/20, Michael Ashton escreveu:
>
> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound.
>
> So,
>
> suppose my matrix is
>
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>
> somematrix
>
>      [,1] [,2] [,3] [,4]
> [1,]    1    6   12    7
> [2,]    4    3    8   11
> [3,]    3    9    5   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>      [,1]      [,2]    [,3]   [,4]
> [1,]    1      5.5     8.5    7
> [2,]    2.5    3        8     10.5
> [3,]    2.5   5.5      5    10.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list
>
> to
>
> the right element of the matrix. When I run the above, for example,
>
> it
>
> takes min(UB,x) over all UB, so I get:
>
>
> newmatrix
>      [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know
>
> what it
>
> is!
>
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
>      [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.
>
>
> --
> Sent from my phone. Please excuse my brevity.
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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
> <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.