[R] making dataframes

Henrique Dallazuanna wwwhsd at gmail.com
Wed Mar 16 15:36:25 CET 2011


Try this:

reshape(dum, direction = 'long', idvar = 'date', varying =
list(c('col1', 'col2')))

On Wed, Mar 16, 2011 at 11:28 AM, pelt <pelt at knmi.nl> wrote:
> Dear all,
>
> I have a dataframe which looks like this (dummy):
>
> date<-c("jan", "feb", "mar", "apr", "may", "june", "july",
> "aug","sep","oct","nov","dec")
> col1<-c(8.2,5.4,4.3,4.1,3.1,2.5,1.1,4.5,3.2,1.9,7.8,6.5)
> col2<-c(3.1,2.3,4.7,6.9,7.5,1.1,3.6,8.5,7.5,2.5,4.1,2.3)
> dum<-data.frame(cbind(date,col1,col2))
> dum
>  date col1 col2
> 1   jan  8.2  3.1
> 2   feb  5.4  2.3
> 3   mar  4.3  4.7
> 4   apr  4.1  6.9
> 5   may  3.1  7.5
> 6  june  2.5  1.1
> 7  july  1.1  3.6
> 8   aug  4.5  8.5
> 9   sep  3.2  7.5
> 10  oct  1.9  2.5
> 11  nov  7.8  4.1
> 12  dec  6.5  2.3
>
> I would like to convert this data.frame into something that looks like this:
>  date rainfall category
> 1   jan  8.2  col1
> 2   feb  5.4  col1
> 3   mar  4.3  col1
> 4   apr  4.1  col1
> 5   may  3.1  col1
> 6  june  2.5  col1
> 7  july  1.1  col1
> 8   aug  4.5  col1
> 9   sep  3.2  col1
> 10  oct  1.9  col1
> 11  nov  7.8  col1
> 12  dec  6.5  col1
> 1   jan   3.1 col2
> 2   feb  2.3 col2
> 3   mar  4.7 col2
> 4   apr   6.9 col2
> 5   may   7.5 col2
> 6  june   1.1 col2
> 7  july    3.6 col2
> 8   aug   8.5 col2
> 9   sep   7.5 col2
> 10  oct  2.5 col2
> 11  nov  4.1 col2
> 12  dec  2.3 col2
>
> So the column-names become categories.  The dataset is rather large with
> many columns and a lengthy date-string. Is there an easy way to do this?
>
> Thank you for your help,
>
> Kind regards,
>
> Saskia van Pelt
>
> ______________________________________________
> 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