[R] Columns and rows

David L Carlson dcarlson at tamu.edu
Fri Oct 12 23:20:32 CEST 2012


This should also work:

Using the dat1 data frame that arun created:

#1
> dat2 <- t(dat1[,2:4])
> dim(dat2) <- prod(dim(dat2))
> dat2 <- data.frame(Col1=dat2, stringsAsFactors=FALSE)
> dat2
   Col1
1     A
2     E
3     H
4     B
5     F
6     I
7     C
8     G
9     J
10    D
11    K
12     

#2
> dat3 <- as.matrix(dat1[, 2:4])
> dim(dat3) <- prod(dim(dat3))
> dat3 <- data.frame(Col1=dat3, stringsAsFactors=FALSE)
> dat3
   Col1
1     A
2     B
3     C
4     D
5     E
6     F
7     G
8     K
9     H
10    I
11    J
12     

#3
> d4 <- data.frame(t(dat2), stringsAsFactors=FALSE)
> d4
     X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12
Col1  A  E  H  B  F  I  C  G  J   D   K  

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of arun
> Sent: Thursday, October 11, 2012 11:26 PM
> To: Santana Sarma
> Cc: R help
> Subject: Re: [R] Columns and rows
> 
> 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.
> 
> 
> ______________________________________________
> 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