[R] Reshape

Jim Lemon jim at bitwrit.com.au
Tue Jul 30 23:02:06 CEST 2013


On 07/30/2013 11:41 PM, Dominic Roye wrote:
> Hello,
>
> I try out to create a new form from the below data.frame.
>
> I would like to have this form:
>
> V3                 Santiago                  Ourense     Vigo    Ferrol
> 2013-07-04     2013-07-04 07:01:04 ......................................
>
> How can i get this?
>
>> str(sunrise)
> 'data.frame': 24 obs. of  3 variables:
>   $ day_frac: Factor w/ 4 levels "Ferrol","Ourense",..: 3 2 4 1 3 2 4 1 3 2
> ...
>   $ time    : POSIXct, format: "2013-07-04 07:01:04" ...
>   $ V3      : Factor w/ 6 levels "2013-07-04","2013-07-05",..: 1 1 1 1 2 2 2
> 2 3 3 ...
>
>> sunrise
>     day_frac                time         V3
> 1  Santiago 2013-07-04 07:01:04 2013-07-04
> 2   Ourense 2013-07-04 07:00:05 2013-07-04
> 3      Vigo 2013-07-04 07:04:06 2013-07-04
> 4    Ferrol 2013-07-04 06:57:38 2013-07-04
> 5  Santiago 2013-07-05 07:01:41 2013-07-05
> 6   Ourense 2013-07-05 07:00:41 2013-07-05
> 7      Vigo 2013-07-05 07:04:42 2013-07-05
> 8    Ferrol 2013-07-05 06:58:16 2013-07-05
> 9  Santiago 2013-07-06 07:02:20 2013-07-06
> 10  Ourense 2013-07-06 07:01:19 2013-07-06
> 11     Vigo 2013-07-06 07:05:20 2013-07-06
> 12   Ferrol 2013-07-06 06:58:56 2013-07-06

Hi Dominic,
Try this:

library(prettyR)
sunrise_wide<-stretch_df(sunrise,idvar="V3",to.stretch="time",
  ordervar="day_frac",include.ordervar=FALSE)
names(sunrise_wide)<-c("V3","Ferrol","Ourense","Santiago","Vigo")
sunrise_wide

Jim



More information about the R-help mailing list