[R] Columns and rows

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Oct 12 07:13:32 CEST 2012


Better, but you didn't do your reading.  Seriously... learn to make it 
reproducible... you will get quicker help from more skilled R users. 
Also, post in text, not HTML.

Note that data frames are not really the right data structure for doing 
the kinds of operations you are describing below, since you assume that 
rows and columns are essentially interchangeable.  Lists of vectors might 
be, but only if you were reading from a data format like XML that 
reflected that structure.  Matrices have a uniform 2d structure with a 
transpose operation defined, and can contain NA values. Also, vectors are 
not described as "column" or "row" vectors in R ... that is Matlab-speak. 
Vectors have no "direction", just sequence.  Matrices are "folded" vectors 
(vectors with row and column dimension attributes).  The as.vector() 
function "forgets" the row and column attributes of a matrix (or other 
object). The matrix() function adds those attributes, effectively 
"folding" the vector up into 2d.

Data frames are lists of vectors with the same length, where each vector 
can have a different type of data (e.g. string, numeric, logical, factor). 
They are printed, read from files and written to files with the vectors as 
columns (even though vectors themselves have no notion of being columns). 
Thus, working with one row at a time is much slower than working with one 
column at a time, and since each element of a row can be a different type 
of data it doesn't make sense to mash them up the way you want to.

I suggest you print and use the "str()" function liberally on the objects 
and expressions below to understand how they work.

#reproducible data
DF <- read.csv( text=
"Names,'Col x','Col y','Col z'
rowName1,A,E,H
rowName2,B,F,I
rowName3,C,G,J
rowName4,D,,K
", header=TRUE, row.names=1, as.is=TRUE, quote="'", na.strings="" )

#solution
mat <- as.matrix( DF )
v1 <- as.vector( na.omit( as.vector( t( mat ) ) ) )
v2 <- as.vector( na.omit( as.vector( mat ) ) )
ans1a <- matrix( v1, ncol=1 )
ans1b <- matrix( v2, nrow=1 )
ans2a <- matrix( v2, ncol=1 )
ans2b <- matrix( v1, nrow=1 )


On Fri, 12 Oct 2012, Santana Sarma wrote:

> Hi,
> 
> Trying to give an example here. 
> Say, I have read in a .csv file using read.csv (), and the file contains the
> following info. 
> 
> 
> Names Col x Col y Col z
> rowName1 A E H
> rowName2 B F I
> rowName3 C G J
> rowName4 D K
> 
> 
> 
> Now, this is what is required: 
> 
> 1. Combine/stack/join contents from - 
>               a) multiple rows into one column. 
> 
> 
> That is:  
> 
> A
> E
> H
> B
> F
> I
> C
> G
> J
> D
> K
> 
>               b) multiple columns into one row.
> 
> A B C D E F G H I J K
> 
>     
> 2. Stack contents from 
> 
> A) multiple columns into one column.
> 
> A
> B
> C
> D
> E
> F
> G
> H
> I
> J
> K
> 
> 
> B) Multiple rows into one row.
> 
> A E H B F I C G J D 
> 
> 
> Thank you.
> 
> Cheers,
> Santana
> 
> ================================
> 
> 
> On Fri, Oct 12, 2012 at 1:32 PM, David Winsemius <dwinsemius at comcast.net>
> wrote:
>
>       On Oct 11, 2012, at 5:55 PM, Santana Sarma wrote:
>
>       > Hi,
>       >
>       > Could you please advice some easy way to do the following for
>       a dataframe
>       > (header=F) having unequal column- & row- length.
>       >
>       > 1. Combine/stack/join contents from -
>       >              a) multiple rows into one column.
>       >              b) multiple columns into one row.
>       >
>       > 2. Stack contents from multiple columns (or, rows) into one
>       column (or,
>       > row).
> 
> Could _you_ please produce an example.
> 
> Dataframes do not have headers. They do have column names and column
> names are required.
> 
> --
> David Winsemius, MD
> Alameda, CA, USA
> 
> 
> 
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------


More information about the R-help mailing list