Reputation: 1531
This is my CSV file:
"Product","Total","Destrio","TotDestrio","Percent"
ANGELLE DIRECT,60904,0,60904,0
CHERRY AMARILLO,60538,9071,69609,13.0313608872
CHERRY AMARILLO PER,20486,5042,25528,19.7508617988
CHERRY EN RAMA,186055,31826,217881,14.6070561453
So I use this:
prod <- read.csv("file.csv",header=T)
prod$Percent <- as.numeric(prod$Percent)
prod$Percent <- factor(prod$Percent, levels =
unique(prod[order(prod$Percent),"Percent"]))
But prod$Percent <- as.numeric(prod$Percent)
doesn't convert to number and prod$Percent <- factor(prod$Percent, levels =unique(prod[order(prod$Percent),"Percent"]))
doesn't order by this field Percent
I need Percent
to be a decimal, not a string and order by this field in descending order.
Upvotes: 1
Views: 94
Reputation: 20045
You read the CSV and everything is fine. The "Percent" column IS numeric. Then you order by it. Actually that's all.
> prod <- read.csv("f:\\temp\\test.csv",header=T)
> str(prod)
'data.frame': 4 obs. of 5 variables:
$ Product : chr "ANGELLE DIRECT" "CHERRY AMARILLO" "CHERRY AMARILLO PER" "CHERRY EN RAMA"
$ Total : int 60904 60538 20486 186055
$ Destrio : int 0 9071 5042 31826
$ TotDestrio: int 60904 69609 25528 217881
$ Percent : num 0 13 19.8 14.6
> sum(prod$Percent)
[1] 47.38928
> prod[order(prod$Percent),]
Product Total Destrio TotDestrio Percent
1 ANGELLE DIRECT 60904 0 60904 0.00000
2 CHERRY AMARILLO 60538 9071 69609 13.03136
4 CHERRY EN RAMA 186055 31826 217881 14.60706
3 CHERRY AMARILLO PER 20486 5042 25528 19.75086
Upvotes: 1