[R] dataframe transposition
John Logsdon
j.logsdon at lancaster.ac.uk
Wed Oct 13 12:26:14 CEST 1999
Try, for example (R 0.63.2)
# Generate some silly data to replace yours and set column names:
> tt<-data.frame(cbind(c('one','two','three'),c(1,2,3)))
> colnames(tt)<-c('Col1','Col2')
> tt
Col1 Col2
1 one 1
2 two 2
3 three 3
# Pick out the second column and transpose into a new structure:
> ttt<-data.frame(t(tt[,2]))
# Take the column names of ttt from the first column of tt:
> colnames(ttt)<-tt[,1]
# Take rowname for ttt from second colname of tt
> rownames(ttt)<-colnames(tt)[2]
# then:
> ttt
one two three
Col2 1 2 3
# Then to plot, since plot assumes a column vector:
> plot(t(ttt))
Isn't this what you want? Seems pretty intuitive here. Mind you, I always
have to check but less intuitive is to get rid of column titles etc when
you want to turn a numerical data frame into a matrix. The titles keep
following me around like a bad smell!!!
John
On Wed, 13 Oct 1999, Michael Lapsley wrote:
> Dear R-helpers,
>
> I wonder if I could impose upon you for forther assistance, this
> time with dataframes: hopefully this will be of general interest,
> as I personally have found them hard to get to grips with.
>
> I was trying to transpose rows and cols and move col1 to the names.
> Then all sorts of things go wrong. Although the end result looks
> the same, page() shows the structure to be quite different compared
> to building it by hand.
>
> What is going on? And how am I supposed to do this. (And no,
> I didn't expect that the output of plot() would be sensible!)
>
>
> Thanks again,
>
> Michael
>
> > wk
> authorisor count(*)
> 1 JMC 4
> 2 LCA 259
> 3 MCM 136
> 4 MDH 266
> 5 ML 27
> 6 SA 1
> >
> >page(wk)
> structure(list(authorisor = c("JMC", "LCA", "MCM", "MDH", "ML",
> "SA"), "count(*)" = c("4", "259", "136", "266", "27", "1")),
> .Names =c("authorisor",
> "count(*)"), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")
>
> >wk[,1]->dnames
> >as.data.frame(t(wk))->wk
> >wk[-1,]->wk
> >names(wk)<-dnames
> >wk
> JMC LCA MCM MDH ML SA
> count(*) 4 259 136 266 27 1
>
> > plot(wk)
> Error: non-numeric data type in frame
> >
> >page(wk3)
> structure(list(JMC = structure("4 ", .Names = "count(*)"), LCA =
> structure("259", .Names = "count(*)"),
> MCM = structure("136", .Names = "count(*)"), MDH = structure("266",
> .Names= "count(*)"),
> ML = structure("27 ", .Names = "count(*)"), SA = structure("1 ", .Names =
> "count(*)")), .Names = c("JMC",
> "LCA", "MCM", "MDH", "ML", "SA"), row.names = "count(*)", class = "data.frame")
>
>
> rbindnd(c(4,259,136,266,27,1))->wk2
> > as.data.frame(rbind(c(4,259,136,266,27,1)))->wk2
> > names(wk2)<-dnames
> > wk2
> JMC LCA MCM MDH ML SA
> 1 4 259 136 266 27 1
> > page (wk2)
> structure(list(JMC = 4, LCA = 259, MCM = 136, MDH = 266, ML = 27,
> SA = 1), .Names = c("JMC", "LCA", "MCM", "MDH", "ML", "SA"
> ), row.names = "1", class = "data.frame")
>
> >
>
>
> ----------------------------------
> E-Mail: Michael Lapsley <mlapsley at ndirect.co.uk>
> Date: 13-Oct-99
> Time: 01:12:23
>
> This message was sent by XFMail
> ----------------------------------
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list