[R] changing display of a dataframe

Gabor Grothendieck ggrothendieck at myway.com
Fri Apr 16 20:40:03 CEST 2004


Read in the data noting that the row names are in column "name" and 
interpreting character data as character type.

The sapply loops over columns creating factors with the same levels (in 
the same order), orders them and uses that to index the row.names.  Finally 
we create row names for the result out of the levels.

d <- read.table("clipboard", header=T, row.names="name", as.is=T)
lev <- sort(d[,1])
d2 <- sapply( d, function(x) row.names(d)[ order( factor(x,levels=lev) ) ] )
row.names(d2) <- lev



Douglas Federman <dfederman <at> mco.edu> writes:

: 
: I have the following dataframe "s"
: 
:    name jul  aug  sep
: 1  Joe   m1  m2  m3
: 2  Bill    m2  m3  m2
: 3  Sue  m3  m1  m1
: 
: I am interested in transforming the data into the following:
: 
:         jul  aug  sep
: m1   Joe Sue Sue
: m2  Bill    Joe Bill
: m3  Sue  Bill   Joe
: 
: Is there a simple way to do this?  
: 
: --
: "It has often been remarked that an educated man has probably forgotten most 
of the facts he acquired in
: school and university. Education is what survives when what has been learned 
has been forgotten."  -- B.F.
: Skinner, New Scientist, 31 May 1964, p. 484
: 
: 
: 
: BEGIN:VCARD
: VERSION:2.1
: X-GWTYPE:USER
: FN:Douglas Federman
: ORG:;1331
: EMAIL;WORK;PREF;NGW:dfederman <at> mco.edu
: N:Federman;Douglas
: END:VCARD
: 
: 
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list