[R] Ordering Table Columns
Pete Brecknock
Peter.Brecknock at bp.com
Sun Mar 3 02:43:34 CET 2013
cdouglass wrote
> Hello all,
>
> Totally new to this and I'm just doing a frequency distribution analysis
> on T-shirt sales by size. I have a .csv with 60 orders. I read in the
> data using read.csv. If I look at the summary() or table() of the data it
> looks fine, except that the shirt sizes are alphabetical rather than from
> S-XXL--so the bar graph loses the shape of the data based on size.
>
> All I want to do is get the table to arrange the data:
> S M L XL XXL
>
> Here's the code that I've run that got me closer to what I want. It seems
> like it should be simple, but going through the "R in a Nutshell" and
> asking Google as many different ways as I can think to phrase it are
> turning up nothing.
>
>> shirt <- read.csv("http://localhost/examples/tshirt_purchases.csv",
>> header=TRUE, sep = ",", nrows=60)
>> shirt.table<-summary(shirt)
>> shirt.table
> Shirt.Size
> L :20
> M :20
> S :11
> XL : 7
> XXL: 2
>
> What I want is:
>
> Shirt.Size
> S :11
> M :20
> L :20
> XL : 7
> XXL: 2
>
> Does anyone know how to do this, or am I coming at it from the wrong
> direction?
> If this has been answered previously and I've just failed to find it in my
> searches, please accept my apologies.
>
> Many Thanks,
> Chris
Think you want to have a look at factors....
Typing ?factor will throw up the relevant help pages
# Your Data
tbl <- read.table(header = TRUE, text = "
ShirtSize Number
L 20
M 20
S 11
XL 7
XXL 2
")
# ShirtSize Alphabetical
tbl[tbl$ShirtSize,]
# Reorder Factor
tbl$ShirtSize = factor(tbl$ShirtSize, levels=c("S","M","L","XL","XXL"))
# ShirtSize Order by Size
tbl[tbl$ShirtSize,]
Pete
--
View this message in context: http://r.789695.n4.nabble.com/Ordering-Table-Columns-tp4660110p4660129.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list