[R] Columns and rows

arun smartpink111 at yahoo.com
Fri Oct 12 06:26:17 CEST 2012


HI,
Try this:
dat1<-read.table(text="
Names Colx Coly Colz
rowName1 A E H
rowName2 B F I
rowName3 C G J
rowName4 D K 
",sep="",header=TRUE,stringsAsFactors=FALSE,fill=TRUE)
 dat2<-t(dat1)
dat3<-dat2[2:4,]
dat4<-do.call(rbind,sapply(dat3,list)))

row.names(dat4)<-1:nrow(dat4)
 dat4
 dat4
#   [,1]
#1  "A" 
#2  "E" 
#3  "H" 
#4  "B" 
#5  "F" 
#6  "I" 
#7  "C" 
#8  "G" 
#9  "J" 
#10 "D" 
#11 "K" 
#12 ""  

data.frame(col1=stack(dat1[,2:4])[,1])
#   col1
#1     A
#2     B
#3     C
#4     D
#5     E
#6     F
#7     G
#8     K
#9     H
#10    I
#11    J
#12     


dat5<-do.call(data.frame,sapply(dat3,list))
 dat5
#  A E H B F I C G J D K X..
#1 A E H B F I C G J D K    
A.K.




----- Original Message -----
From: Santana Sarma <aimanusarma at gmail.com>
To: David Winsemius <dwinsemius at comcast.net>
Cc: r-help at r-project.org
Sent: Thursday, October 11, 2012 11:07 PM
Subject: Re: [R] Columns and rows

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

    [[alternative HTML version deleted]]

______________________________________________
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