Appending the column names

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Appending the column names

namit
Hi Freinds,

I have two data frames X,Y. I want  to append both the data frames into one, along with the columns names from both the data frames (it should look like Z).

X:
    Summary    G      Y     R
      Acc               12    12   13
      Bcc               11    14   15
      Ccc               13    15   16

Y:
   Summary    G      Y     R
      Acc               10    11   12
      Bcc               13    12   11
      Ccc               11    16   20



Result
----------
Z:

Summary         G      Y     R
      Acc               12    12   13
      Bcc               11    14   15
      Ccc               13    15   16
Summary          G      Y     R
      Acc               10    11   12
      Bcc               13    12   11
      Ccc               11    16   20


Can anyone help me on this.

Thanks in Advance.

Thanks,
Namit.

Arun  your logic is not working,getting error message(could not find function"Colnames")
Reply | Threaded
Open this post in threaded view
|

Re: Appending the column names

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hello,

It's not "Colnames".  It is "colnames().

Also, when you read the file, you have to use stringasAsFactors=FALSE.

X<-read.table(text="
    Summary    G      Y     R
      Acc               12    12   13
      Bcc               11    14   15
      Ccc               13    15   16
",sep="",header=TRUE,stringsAsFactors=FALSE)
Y<-read.table(text="
   Summary    G      Y     R
      Acc               10    11   12
      Bcc               13    12   11
      Ccc               11    16   20
",sep="",header=TRUE,stringsAsFactors=FALSE)
Z<-rbind(X,colnames(Y),Y)
 Z
  Summary  G  Y  R
1     Acc 12 12 13
2     Bcc 11 14 15
3     Ccc 13 15 16
4 Summary  G  Y  R
5     Acc 10 11 12
6     Bcc 13 12 11
7     Ccc 11 16 20
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: Appending the column names

Michael Weylandt
In reply to this post by namit
On Mon, Jul 30, 2012 at 3:17 PM, namit <[hidden email]> wrote:

> Hi Freinds,
>
> I have two data frames X,Y. I want  to append both the data frames into one,
> along with the columns names from both the data frames (it should look like
> Z).
>
> X:
>     Summary    G      Y     R
>       Acc               12    12   13
>       Bcc               11    14   15
>       Ccc               13    15   16
>
> Y:
>    Summary    G      Y     R
>       Acc               10    11   12
>       Bcc               13    12   11
>       Ccc               11    16   20
>
>
>
> Result
> ----------
> Z:
>
> Summary         G      Y     R
>       Acc               12    12   13
>       Bcc               11    14   15
>       Ccc               13    15   16
> Summary          G      Y     R
>       Acc               10    11   12
>       Bcc               13    12   11
>       Ccc               11    16   20
>
>
> Can anyone help me on this.

No, as noted to you by me before
(https://stat.ethz.ch/pipermail/r-help/2012-July/319868.html) -- a
data frame, by definition, has a single column name per column. It
also must have unique rownames so your "desired output" is simply not
a data frame and thus no one can help you to construct one.

Now, to repeat myself:

What are you trying to do (big picture wise)?

R's data structures are quite flexible and powerful and it's very easy
to build one to fit your needs, but what are those needs? We cannot
know if you don't tell us.

Michael

>
> Thanks in Advance.
>
> Thanks,
> Namit.
>
> Arun  your logic is not working,getting error message(could not find
> function"Colnames")
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Appending-the-column-names-tp4638421.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Appending the column names

arun kirshna
In reply to this post by namit


Hello,

It's not "Colnames".  It is colnames().  Also, you should use stringsAsFactors=FALSE.

 I tried to reply it to nabble.  But, seems like nabble webpage is redirecting to some porn site. 

Anyway,

X<-read.table(text="
    Summary    G      Y     R
      Acc               12    12   13
      Bcc               11    14   15
      Ccc               13    15   16
",sep="",header=TRUE,stringsAsFactors=FALSE)
Y<-read.table(text="
   Summary    G      Y     R
      Acc               10    11   12
      Bcc               13    12   11
      Ccc               11    16   20
",sep="",header=TRUE,stringsAsFactors=FALSE)
Z<-rbind(X,colnames(Y),Y)
 Z
  Summary  G  Y  R
1     Acc 12 12 13
2     Bcc 11 14 15
3     Ccc 13 15 16
4 Summary  G  Y  R
5     Acc 10 11 12
6     Bcc 13 12 11
7     Ccc 11 16 20

A.K.




----- Original Message -----
From: namit <[hidden email]>
To: [hidden email]
Cc:
Sent: Monday, July 30, 2012 4:17 PM
Subject: [R] Appending the column names

Hi Freinds,

I have two data frames X,Y. I want  to append both the data frames into one,
along with the columns names from both the data frames (it should look like
Z).

X:
    Summary    G      Y     R
      Acc               12    12   13
      Bcc               11    14   15
      Ccc               13    15   16

Y:
   Summary    G      Y     R
      Acc               10    11   12
      Bcc               13    12   11
      Ccc               11    16   20



Result
----------
Z:

Summary         G      Y     R
      Acc               12    12   13
      Bcc               11    14   15
      Ccc               13    15   16
Summary          G      Y     R
      Acc               10    11   12
      Bcc               13    12   11
      Ccc               11    16   20


Can anyone help me on this.

Thanks in Advance.

Thanks,
Namit.

Arun  your logic is not working,getting error message(could not find
function"Colnames")



--
View this message in context: http://r.789695.n4.nabble.com/Appending-the-column-names-tp4638421.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Appending the column names

David Carlson
In reply to this post by Michael Weylandt
You can't always get what you want (a data.frame with two sets of column
headings), but you do have several options. Maybe they will help you think
about what you are trying to do.

Given X and Y as Arun provided earlier, you can create Z

> Z <- rbind(X,colnames(Y),Y)
> Z
  Summary  G  Y  R
1     Acc 12 12 13
2     Bcc 11 14 15
3     Ccc 13 15 16
4 Summary  G  Y  R
5     Acc 10 11 12
6     Bcc 13 12 11
7     Ccc 11 16 20

Which is a data.frame, but all the variables are character and the second
set of column headings is just row #4.

Or a list consisting of two data.frames

> ListXY <- list(X=X, Y=Y)
> ListXY
$X
  Summary  G  Y  R
1     Acc 12 12 13
2     Bcc 11 14 15
3     Ccc 13 15 16

$Y
  Summary  G  Y  R
1     Acc 10 11 12
2     Bcc 13 12 11
3     Ccc 11 16 20

Now each data.frame is preserved as a member of the list ListXY.

Or a single data frame that keeps track of where each row comes from by
adding a column called Group:

> DataXY <- rbind(data.frame(Group="X", X), data.frame(Group="Y", Y))
> DataXY
  Group Summary  G  Y  R
1     X     Acc 12 12 13
2     X     Bcc 11 14 15
3     X     Ccc 13 15 16
4     Y     Acc 10 11 12
5     Y     Bcc 13 12 11
6     Y     Ccc 11 16 20

You just might find that one of these is what you need.

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


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of R. Michael Weylandt
> Sent: Monday, July 30, 2012 3:42 PM
> To: namit
> Cc: [hidden email]
> Subject: Re: [R] Appending the column names
>
> On Mon, Jul 30, 2012 at 3:17 PM, namit <[hidden email]>
> wrote:
> > Hi Freinds,
> >
> > I have two data frames X,Y. I want  to append both the data frames
> into one,
> > along with the columns names from both the data frames (it should
> look like
> > Z).
> >
> > X:
> >     Summary    G      Y     R
> >       Acc               12    12   13
> >       Bcc               11    14   15
> >       Ccc               13    15   16
> >
> > Y:
> >    Summary    G      Y     R
> >       Acc               10    11   12
> >       Bcc               13    12   11
> >       Ccc               11    16   20
> >
> >
> >
> > Result
> > ----------
> > Z:
> >
> > Summary         G      Y     R
> >       Acc               12    12   13
> >       Bcc               11    14   15
> >       Ccc               13    15   16
> > Summary          G      Y     R
> >       Acc               10    11   12
> >       Bcc               13    12   11
> >       Ccc               11    16   20
> >
> >
> > Can anyone help me on this.
>
> No, as noted to you by me before
> (https://stat.ethz.ch/pipermail/r-help/2012-July/319868.html) -- a
> data frame, by definition, has a single column name per column. It
> also must have unique rownames so your "desired output" is simply not
> a data frame and thus no one can help you to construct one.
>
> Now, to repeat myself:
>
> What are you trying to do (big picture wise)?
>
> R's data structures are quite flexible and powerful and it's very easy
> to build one to fit your needs, but what are those needs? We cannot
> know if you don't tell us.
>
> Michael
>
> >
> > Thanks in Advance.
> >
> > Thanks,
> > Namit.
> >
> > Arun  your logic is not working,getting error message(could not find
> > function"Colnames")
> >
> >
> >
> > --
> > View this message in context:
> http://r.789695.n4.nabble.com/Appending-the-column-names-tp4638421.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.