[R] dataframe transposition

Michael Lapsley mlapsley at ndirect.co.uk
Wed Oct 13 11:07:13 CEST 1999


Thanks, but: 

>
>wk<-as.data.frame(rbind(c("JMC",4),c("LCA",259),
c("MCM",136),c("MDH",266),c("ML",27),c("SA",1)))
>names(wk)<-c("authorisor","count")

> levels(wk$authorisor)
NULL

str is a really good tip though:

> str(wk)
`data.frame':  6 obs. of  2 variables:
 $ authorisor: chr  "JMC" "LCA" "MCM" "MDH" "ML" "SA"
 $ count     : chr  "4" "259" "136" "266" "27" "1"
>

whereas on my transposed example:
> wk2
      JMC LCA MCM MDH  ML  SA
count 4   259 136 266 27  1 

> str(wk2)
`data.frame':  1 obs. of  6 variables:
 $ JMC: Named chr "4  "
  ..- attr(*, "names")= chr "count"
 $ LCA: Named chr "259"
  ..- attr(*, "names")= chr "count"
 $ MCM: Named chr "136"
  ..- attr(*, "names")= chr "count"
 $ MDH: Named chr "266"
  ..- attr(*, "names")= chr "count"
 $ ML : Named chr "27 "
  ..- attr(*, "names")= chr "count"
 $ SA : Named chr "1  "
  ..- attr(*, "names")= chr "count"
> 

I still don't understand what is going on and the theory of how to do
this sort of thing reliably.

Michael

On 13-Oct-99 Jan.Schelling at hydro.com wrote:
> 
> 
> 
> you could try:
> 
> cnames <- levels(wk$authorisor)
> rnames <- names(wk)[-1]
> wk <- data.frame(t(wk[,-1]))
> names(wk) <- cnames
> row.names(wk) <- rnames
> 
> to check the structure of the object I would recommend str() instead of
> page():
> 
> str(wk)
> 
> 
>> 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

----------------------------------
E-Mail: Michael Lapsley <mlapsley at ndirect.co.uk>
Date: 13-Oct-99
Time: 09:51:59

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list