Equivalent of gtools::mixedsort in R base

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

Equivalent of gtools::mixedsort in R base

Sebastien Bihorel
Hi,

Searching for functions that would order strings that mix characters and numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I found the mixedsort and mixedorder from the gtools package.

Problems:
1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does
2- gtools has not been updated in 2.5 years

Are you aware of an equivalent of this function in base R or a another contributed package (with correction of problem #1)?

Thanks

______________________________________________
[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: Equivalent of gtools::mixedsort in R base

Bert Gunter-2
???

> y <- sort( c("a1","a2","a10","a12","a100"))
> y
[1] "a1"   "a10"  "a100" "a12"  "a2"
> mixedsort(y)
[1] "a1"   "a2"   "a10"  "a12"  "a100"

**Please read the docs!** They say that mixedsort() and mixedorder()  both
take a **single vector**  as the argument to be sorted or ordered and, as
the above indicates, they perform exactly as advertised. **Unlike
order()**. So of course your do.call() construction fails.

So presumably you have a data frame with multiple columns of mixed alpha
and numerics?  (A reproducible example would be most helpful here.)

If this is the case, one **possibly dumb** approach (you have been warned!)
would be to turn each column into an ordered factor and then call order()
on the data frame of ordered factors via do.call() as above. i.e.

> y1 <- ordered(y,lev = mixedsort(y))
> y1
[1] a1   a10  a100 a12  a2
Levels: a1 < a2 < a10 < a12 < a100
> order(y1)
[1] 1 5 2 4 3

(this is just for 1 vector to show how the idea would work).

Of course, if this is **not** what you want, you'll need to clarify,
hopefully with a reprex. Or hope that someone else has better insight than
I.

Cheers,
Bert




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 Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel <
[hidden email]> wrote:

> Hi,
>
> Searching for functions that would order strings that mix characters and
> numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I
> found the mixedsort and mixedorder from the gtools package.
>
> Problems:
> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call
> like the order function does
> 2- gtools has not been updated in 2.5 years
>
> Are you aware of an equivalent of this function in base R or a another
> contributed package (with correction of problem #1)?
>
> Thanks
>
> ______________________________________________
> [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: Equivalent of gtools::mixedsort in R base

Jeff Newmiller
x <- c( "a1", "a10", "a2" )
y <- c( "b10", "b2", "a12", "ca1" )

DF <- expand.grid( x = x, y = y )
# randomize
set.seed( 42 )
DF <- DF[ sample( nrow( DF ) ), ]

# missing from gtools
mixedrank <- function( x ) {
   seq.int( length( x ) )[ gtools::mixedorder(x) ]
}

o <- do.call( order, lapply( DF, mixedrank ) )
DF[ o, ]

# or, as Bert suggests:

myrank <- function( v ) {
   vu <- unique(v)
   vl <- regmatches( vu,regexec("^([A-Za-z]+)(\\d+)$",vu))
   alph <- sapply( vl, function(s) s[2] )
   digt <- as.integer( sapply( vl, function(s) s[3] ) )
   o <- order( alph, digt )
   vo <- ordered( v, levels=vu[ o ] )
}

o2 <- do.call( order, lapply( DF, myrank ) )
DF[ o2, ]

?order
?ordered
?rank

On Sun, 11 Mar 2018, Bert Gunter wrote:

> ???
>
>> y <- sort( c("a1","a2","a10","a12","a100"))
>> y
> [1] "a1"   "a10"  "a100" "a12"  "a2"
>> mixedsort(y)
> [1] "a1"   "a2"   "a10"  "a12"  "a100"
>
> **Please read the docs!** They say that mixedsort() and mixedorder()  both
> take a **single vector**  as the argument to be sorted or ordered and, as
> the above indicates, they perform exactly as advertised. **Unlike
> order()**. So of course your do.call() construction fails.
>
> So presumably you have a data frame with multiple columns of mixed alpha
> and numerics?  (A reproducible example would be most helpful here.)
>
> If this is the case, one **possibly dumb** approach (you have been warned!)
> would be to turn each column into an ordered factor and then call order()
> on the data frame of ordered factors via do.call() as above. i.e.
>
>> y1 <- ordered(y,lev = mixedsort(y))
>> y1
> [1] a1   a10  a100 a12  a2
> Levels: a1 < a2 < a10 < a12 < a100
>> order(y1)
> [1] 1 5 2 4 3
>
> (this is just for 1 vector to show how the idea would work).
>
> Of course, if this is **not** what you want, you'll need to clarify,
> hopefully with a reprex. Or hope that someone else has better insight than
> I.
>
> Cheers,
> Bert
>
>
>
>
> 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 Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel <
> [hidden email]> wrote:
>
>> Hi,
>>
>> Searching for functions that would order strings that mix characters and
>> numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I
>> found the mixedsort and mixedorder from the gtools package.
>>
>> Problems:
>> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call
>> like the order function does
>> 2- gtools has not been updated in 2.5 years
>>
>> Are you aware of an equivalent of this function in base R or a another
>> contributed package (with correction of problem #1)?
>>
>> Thanks
>>
>> ______________________________________________
>> [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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[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: Equivalent of gtools::mixedsort in R base

R help mailing list-2
In reply to this post by Sebastien Bihorel
       1- mixedorder does not work in a "do.call(mixedorder, mydataframe)"
call like the order function does

This is tangential, but do.call(order, mydataframe) is not safe to use in a
general purpose function either - you need to remove the names from
the second argument:
  > d <- data.frame(method=c("New","New","Old","Old","Old"), result=5:1)
  > do.call(order, d)
  Error in match.arg(method) : 'arg' must be NULL or a character vector
  > do.call(order, unname(as.list(d)))
  [1] 2 1 5 4 3


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel <
[hidden email]> wrote:

> Hi,
>
> Searching for functions that would order strings that mix characters and
> numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I
> found the mixedsort and mixedorder from the gtools package.
>
> Problems:
> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call
> like the order function does
> 2- gtools has not been updated in 2.5 years
>
> Are you aware of an equivalent of this function in base R or a another
> contributed package (with correction of problem #1)?
>
> Thanks
>
> ______________________________________________
> [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: Equivalent of gtools::mixedsort in R base

Sebastien Bihorel
In reply to this post by Bert Gunter-2

So I take this is a no to my initial question.

Cheers too.

PS: some users just ask questions to get straight answers not to get a solution to their problem :D


From: "Bert Gunter" <[hidden email]>
To: "Sebastien Bihorel" <[hidden email]>
Cc: "R-help" <[hidden email]>
Sent: Monday, March 12, 2018 12:57:00 AM
Subject: Re: [R] Equivalent of gtools::mixedsort in R base

???

> y <- sort( c("a1","a2","a10","a12","a100"))
> y
[1] "a1" "a10" "a100" "a12" "a2"
> mixedsort(y)
[1] "a1" "a2" "a10" "a12" "a100"

**Please read the docs!** They say that mixedsort() and mixedorder() both take a **single vector** as the argument to be sorted or ordered and, as the above indicates, they perform exactly as advertised. **Unlike order()**. So of course your do.call() construction fails.

So presumably you have a data frame with multiple columns of mixed alpha and numerics? (A reproducible example would be most helpful here.)

If this is the case, one **possibly dumb** approach (you have been warned!) would be to turn each column into an ordered factor and then call order() on the data frame of ordered factors via do.call() as above. i.e.

> y1 <- ordered(y,lev = mixedsort(y))
> y1
[1] a1 a10 a100 a12 a2
Levels: a1 < a2 < a10 < a12 < a100
> order(y1)
[1] 1 5 2 4 3

(this is just for 1 vector to show how the idea would work).

Of course, if this is **not** what you want, you'll need to clarify, hopefully with a reprex. Or hope that someone else has better insight than I.

Cheers,
Bert




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 Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel < [ mailto:[hidden email] | [hidden email] ] > wrote:


Hi,

Searching for functions that would order strings that mix characters and numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I found the mixedsort and mixedorder from the gtools package.

Problems:
1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does
2- gtools has not been updated in 2.5 years

Are you aware of an equivalent of this function in base R or a another contributed package (with correction of problem #1)?

Thanks

______________________________________________
[ mailto:[hidden email] | [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: Equivalent of gtools::mixedsort in R base

Sebastien Bihorel
In reply to this post by R help mailing list-2
Hi,

Point taken... although this error is not returned in older version of R (3.1.2 does not have any issue with your test case... not sure when the added layer of check was introduced).


From: "William Dunlap" <[hidden email]>
To: "Sebastien Bihorel" <[hidden email]>
Cc: [hidden email]
Sent: Monday, March 12, 2018 10:56:43 AM
Subject: Re: [R] Equivalent of gtools::mixedsort in R base

1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does

This is tangential, but do.call(order, mydataframe) is not safe to use in a
general purpose function either - you need to remove the names from
the second argument:
> d <- data.frame(method=c("New","New","Old","Old","Old"), result=5:1)
> do.call(order, d)
Error in match.arg(method) : 'arg' must be NULL or a character vector
> do.call(order, unname(as.list(d)))
[1] 2 1 5 4 3


Bill Dunlap
TIBCO Software
wdunlap [ http://tibco.com/ | tibco.com ]

On Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel < [ mailto:[hidden email] | [hidden email] ] > wrote:


Hi,

Searching for functions that would order strings that mix characters and numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I found the mixedsort and mixedorder from the gtools package.

Problems:
1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does
2- gtools has not been updated in 2.5 years

Are you aware of an equivalent of this function in base R or a another contributed package (with correction of problem #1)?

Thanks

______________________________________________
[ mailto:[hidden email] | [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: Equivalent of gtools::mixedsort in R base

Sebastien Bihorel
In reply to this post by Jeff Newmiller

Thanks for your reply. I take this is also a no to my question and appreciated the suggested mixedrank function and its usage with do.call.

Thanks


----- Original Message -----
From: "Jeff Newmiller" <[hidden email]>
To: "Bert Gunter" <[hidden email]>
Cc: "Sebastien Bihorel" <[hidden email]>, "R-help" <[hidden email]>
Sent: Monday, March 12, 2018 2:11:03 AM
Subject: Re: [R] Equivalent of gtools::mixedsort in R base

x <- c( "a1", "a10", "a2" )
y <- c( "b10", "b2", "a12", "ca1" )

DF <- expand.grid( x = x, y = y )
# randomize
set.seed( 42 )
DF <- DF[ sample( nrow( DF ) ), ]

# missing from gtools
mixedrank <- function( x ) {
   seq.int( length( x ) )[ gtools::mixedorder(x) ]
}

o <- do.call( order, lapply( DF, mixedrank ) )
DF[ o, ]

# or, as Bert suggests:

myrank <- function( v ) {
   vu <- unique(v)
   vl <- regmatches( vu,regexec("^([A-Za-z]+)(\\d+)$",vu))
   alph <- sapply( vl, function(s) s[2] )
   digt <- as.integer( sapply( vl, function(s) s[3] ) )
   o <- order( alph, digt )
   vo <- ordered( v, levels=vu[ o ] )
}

o2 <- do.call( order, lapply( DF, myrank ) )
DF[ o2, ]

?order
?ordered
?rank

On Sun, 11 Mar 2018, Bert Gunter wrote:

> ???
>
>> y <- sort( c("a1","a2","a10","a12","a100"))
>> y
> [1] "a1"   "a10"  "a100" "a12"  "a2"
>> mixedsort(y)
> [1] "a1"   "a2"   "a10"  "a12"  "a100"
>
> **Please read the docs!** They say that mixedsort() and mixedorder()  both
> take a **single vector**  as the argument to be sorted or ordered and, as
> the above indicates, they perform exactly as advertised. **Unlike
> order()**. So of course your do.call() construction fails.
>
> So presumably you have a data frame with multiple columns of mixed alpha
> and numerics?  (A reproducible example would be most helpful here.)
>
> If this is the case, one **possibly dumb** approach (you have been warned!)
> would be to turn each column into an ordered factor and then call order()
> on the data frame of ordered factors via do.call() as above. i.e.
>
>> y1 <- ordered(y,lev = mixedsort(y))
>> y1
> [1] a1   a10  a100 a12  a2
> Levels: a1 < a2 < a10 < a12 < a100
>> order(y1)
> [1] 1 5 2 4 3
>
> (this is just for 1 vector to show how the idea would work).
>
> Of course, if this is **not** what you want, you'll need to clarify,
> hopefully with a reprex. Or hope that someone else has better insight than
> I.
>
> Cheers,
> Bert
>
>
>
>
> 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 Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel <
> [hidden email]> wrote:
>
>> Hi,
>>
>> Searching for functions that would order strings that mix characters and
>> numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I
>> found the mixedsort and mixedorder from the gtools package.
>>
>> Problems:
>> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call
>> like the order function does
>> 2- gtools has not been updated in 2.5 years
>>
>> Are you aware of an equivalent of this function in base R or a another
>> contributed package (with correction of problem #1)?
>>
>> Thanks
>>
>> ______________________________________________
>> [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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[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: Equivalent of gtools::mixedsort in R base

Gabor Grothendieck
In reply to this post by Sebastien Bihorel
split any mixed columns into letter and number columns
and then order can be used on that:

  DF <- data.frame(x = c("a10", "a2", "a1"))
  o <- do.call("order", transform(DF, let = gsub("\\d", "", x),
                                                         no =
as.numeric(gsub("\\D", "", x)),
                                                         x = NULL))
  DF[o,, drop = FALSE ]


On Mon, Mar 12, 2018 at 12:15 AM, Sebastien Bihorel
<[hidden email]> wrote:

> Hi,
>
> Searching for functions that would order strings that mix characters and numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I found the mixedsort and mixedorder from the gtools package.
>
> Problems:
> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does
> 2- gtools has not been updated in 2.5 years
>
> Are you aware of an equivalent of this function in base R or a another contributed package (with correction of problem #1)?
>
> Thanks
>
> ______________________________________________
> [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.



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

______________________________________________
[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: Equivalent of gtools::mixedsort in R base

Sebastien Bihorel
Thanks.

----- Original Message -----
From: "Gabor Grothendieck" <[hidden email]>
To: "Sebastien Bihorel" <[hidden email]>
Cc: [hidden email]
Sent: Monday, March 12, 2018 3:49:10 PM
Subject: Re: [R] Equivalent of gtools::mixedsort in R base

split any mixed columns into letter and number columns
and then order can be used on that:

  DF <- data.frame(x = c("a10", "a2", "a1"))
  o <- do.call("order", transform(DF, let = gsub("\\d", "", x),
                                                         no =
as.numeric(gsub("\\D", "", x)),
                                                         x = NULL))
  DF[o,, drop = FALSE ]


On Mon, Mar 12, 2018 at 12:15 AM, Sebastien Bihorel
<[hidden email]> wrote:

> Hi,
>
> Searching for functions that would order strings that mix characters and numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I found the mixedsort and mixedorder from the gtools package.
>
> Problems:
> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does
> 2- gtools has not been updated in 2.5 years
>
> Are you aware of an equivalent of this function in base R or a another contributed package (with correction of problem #1)?
>
> Thanks
>
> ______________________________________________
> [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.



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

______________________________________________
[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.