same column name in a data frame

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

same column name in a data frame

Jinsong Zhao-2
Hi there,

I happened to find the following code can generate a data frame with
same column name.

 > x <- data.frame(a=c(1,2,3))
 > y <- data.frame(a=c(2,3,4))
 > z <- cbind(x,y)

However, in this case, one can not use the $ to extract the second
column, right?

Is it possible to prevent the cbind() produce a data frame with same
column name?

Best,
Jinsong

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [FORGED] same column name in a data frame

Rolf Turner
On 14/03/17 14:56, Jinsong Zhao wrote:

> Hi there,
>
> I happened to find the following code can generate a data frame with
> same column name.
>
>> x <- data.frame(a=c(1,2,3))
>> y <- data.frame(a=c(2,3,4))
>> z <- cbind(x,y)
>
> However, in this case, one can not use the $ to extract the second
> column, right?
>
> Is it possible to prevent the cbind() produce a data frame with same
> column name?

No.

Why not either:

(a) Just make sure the names in "x" and "y" differ?

Or:

(b) Change the names of "z", e.g. names(z) <- c("clyde","irving")?

Or maybe names(z) <- make.unique(names(z)).

You could probably write a wrapper function for cbind() to automate (b)
if you really want to.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [FORGED] same column name in a data frame

Peter Dalgaard-2

> On 14 Mar 2017, at 03:43 , Rolf Turner <[hidden email]> wrote:
>
> On 14/03/17 14:56, Jinsong Zhao wrote:
>> Hi there,
>>
>> I happened to find the following code can generate a data frame with
>> same column name.
>>
>>> x <- data.frame(a=c(1,2,3))
>>> y <- data.frame(a=c(2,3,4))
>>> z <- cbind(x,y)
>>
>> However, in this case, one can not use the $ to extract the second
>> column, right?
>>
>> Is it possible to prevent the cbind() produce a data frame with same
>> column name?
>
> No.
>
> Why not either:
>
> (a) Just make sure the names in "x" and "y" differ?
>
> Or:
>
> (b) Change the names of "z", e.g. names(z) <- c("clyde","irving")?
>
> Or maybe names(z) <- make.unique(names(z)).
>
> You could probably write a wrapper function for cbind() to automate (b) if you really want to.
>

Yes, this is what it is, and I doubt anyone is likely to set out to change it.

However, it is a bit of an oddity compared to the (often undesirable) pains the data.frame code goes through to ensure distinct _row_ names; e.g.,

> cbind(data.frame(a=1), data.frame(a=2))
  a a
1 1 2

but

> rbind(data.frame(a=c(foo=1)), data.frame(a=c(foo=2)))
     a
foo  1
foo1 2

-pr


--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [FORGED] same column name in a data frame

David Carlson
How about just using data.frame(): instead of cbind():

> x <- data.frame(a=c(1,2,3))
> y <- data.frame(a=c(2,3,4))
> xy <- data.frame(x, y)
> str(xy)
'data.frame':   3 obs. of  2 variables:
 $ a  : num  1 2 3
 $ a.1: num  2 3 4
> xy$a
[1] 1 2 3
> xy$a.1
[1] 2 3 4

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of peter dalgaard
Sent: Tuesday, March 14, 2017 8:43 AM
To: Rolf Turner <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] [FORGED] same column name in a data frame


> On 14 Mar 2017, at 03:43 , Rolf Turner <[hidden email]> wrote:
>
> On 14/03/17 14:56, Jinsong Zhao wrote:
>> Hi there,
>>
>> I happened to find the following code can generate a data frame with
>> same column name.
>>
>>> x <- data.frame(a=c(1,2,3))
>>> y <- data.frame(a=c(2,3,4))
>>> z <- cbind(x,y)
>>
>> However, in this case, one can not use the $ to extract the second
>> column, right?
>>
>> Is it possible to prevent the cbind() produce a data frame with same
>> column name?
>
> No.
>
> Why not either:
>
> (a) Just make sure the names in "x" and "y" differ?
>
> Or:
>
> (b) Change the names of "z", e.g. names(z) <- c("clyde","irving")?
>
> Or maybe names(z) <- make.unique(names(z)).
>
> You could probably write a wrapper function for cbind() to automate (b) if you really want to.
>

Yes, this is what it is, and I doubt anyone is likely to set out to change it.

However, it is a bit of an oddity compared to the (often undesirable) pains the data.frame code goes through to ensure distinct _row_ names; e.g.,

> cbind(data.frame(a=1), data.frame(a=2))
  a a
1 1 2

but

> rbind(data.frame(a=c(foo=1)), data.frame(a=c(foo=2)))
     a
foo  1
foo1 2

-pr


--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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.
Loading...