[R] how to aggregate the dataset
arun
smartpink111 at yahoo.com
Thu Dec 13 14:35:00 CET 2012
Hi,
You could try this:
dat3<-read.table(text="
product min_price max_price mean_price country price_band
11 34 50 40 VN 0-300
22 10 30 15 VN 0-300
",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
SubsetPrice<-dat3[grep("price",names(dat3))]
dat3$newPrice<-paste(SubsetPrice[,3],paste("[",SubsetPrice[,1],",",SubsetPrice[,2],"]",sep=""),sep=" ")
dcast(dat3,product+price_band~country,value.var="newPrice")
# product price_band VN
#1 11 0-300 40 [34,50]
#2 22 0-300 15 [10,30]
A.K.
----- Original Message -----
From: Tammy Ma <metal_licaling at live.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Thursday, December 13, 2012 5:42 AM
Subject: [R] how to aggregate the dataset
HI,
Sorry for messing up..
I want to transform the following dataset:
product min_price max_price mean_price country price_band
11 34 50 40 VN 0-300
22 10 30 15 VN 0-300
Into:
product VN price_band
11 40 0-300
[34,50]
22 15 0-300
[10,30]
How can I do this in r? I have large dataset like this. I want to transform all into that one. Thanks a lot.
Kind regards,
Tammy
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
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.
More information about the R-help
mailing list