[R] Rearranging data
Henrique Dallazuanna
wwwhsd at gmail.com
Thu Feb 18 13:41:54 CET 2010
Try this also;
xtabs(rate ~ date + corp_id + investment_id, data = DF)
On Thu, Feb 18, 2010 at 7:21 AM, Anna Carter <anna_carter09 at yahoo.com> wrote:
> Hi!
> I have just started learning R and today only I have joined this group. This is my first mail and I wish to thank all of you for allowing me to be part of this group.
>
> I have following problem. I have an input.csv file such that
>
> corp_id date investment_id rate
> corp1 17-Feb 1 65
> corp1 16-Feb 1 70
> corp1 15-Feb 1 69
> corp1 14-Feb 1 89
> corp1 13-Feb 1 88
> corp2 17-Feb 1 95
> corp2 16-Feb 1 135
> corp2 15-Feb 1 140
> corp3 17-Feb 2 86
> corp3 16-Feb 2 73
> corp5 17-Feb 3 24
> corp6 17-Feb 4 44
> corp11 17-Feb 1 30
> corp13 17-Feb 1 16
> corp21 15-Feb 3 21
>
>
> I have filtered them based on investment id as
>
> invest_data = read.csv('input.csv')
> investment_id = invest_data$investment_id
>
> filtered1 <- subset(invest_data,investment_id==1)
> filtered2 <- subset(invest_data,investment_id==2)
> filtered3 <- subset(invest_data,investment_id==3)
> filtered4 <- subset(invest_data,investment_id==4)
>
> so filtered1 will give me
>
>> filtered1
> corp_id date investment_id stock_rate
> 1 corp1 17-Feb 1 65
> 2 corp1 16-Feb 1 70
> 3 corp1 15-Feb 1 69
> 4 corp1 14-Feb 1 89
> 5 corp1 13-Feb 1 88
> 6 corp2 17-Feb 1 95
> 7 corp2 16-Feb 1 135
> 8 corp2 15-Feb 1 140
> 13 corp11 17-Feb 1 30
> 14 corp17 17-Feb 1 16
>
> My objective is to rearrange filtered1 as
>
> date corp1 corp2 corp11 corp17
> 17-Feb 65 95 30 16
> 16-Feb 70 135
> 15-Feb 69 140
> 14-Feb 89
> 13-Feb 88
>
> #(The above figures represent the corp-wise and date-wise rates for investment_id = 1.)
>
> Please guide me how the filtered1 can be rearranged?
>
> Thanking you
>
> Anna Carter
>
>
>
>
>
>
>
> The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
> [[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.
>
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list