[R] making dataframes
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Wed Mar 16 23:37:33 CET 2011
Firstly, the way you have constructed your data frame in the example will convert everything to factors. What you need to do is actually a bit simpler:
#######
dum <- data.frame(date, col1, col2)
#######
One way to turn this into the kind of data frame you want is to convert the main part of it to a table first, and then coerce into a data frame:
#######
tab <- as.table(as.matrix(dum[, -1]))
row.names(tab) <- date
names(dimnames(tab)) <- c("date", "category")
Dum <- as.data.frame(tab, responseName = "rainfall")
Dum$date <- factor(Dum$date, levels = date)
#######
Here is a checK:
> head(Dum)
date category rainfall
1 jan col1 8.2
2 feb col1 5.4
3 mar col1 4.3
4 apr col1 4.1
5 may col1 3.1
6 june col1 2.5
> with(Dum, tapply(rainfall, date, mean))
jan feb mar apr may june july aug sep oct nov dec
5.65 3.85 4.50 5.50 5.30 1.80 2.35 6.50 5.35 2.20 5.95 4.40
Bill Venables.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of pelt
Sent: Thursday, 17 March 2011 12:28 AM
To: r-help at r-project.org
Subject: [R] making dataframes
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.
More information about the R-help
mailing list