Hi:

On Thu, Jul 29, 2010 at 3:28 AM, pdb <ph...@philbrierley.com> wrote:

>
> I've just tried to merge 2 data sets thinking they would only keep the
> common
> columns, but noticed the column count was not adding up. I've then
> replicated a simple example and got the same thing happening.
>
> q1. why doesn't 'b' have a column name?
>
> q2. when I merge, why does the new column 'y' have all values as 5.1?
>
> Thanks in advance,
>
> Mr. confused
>
>
> > a <- iris[,]
> > b <- iris[,1]
>

b is not a data frame - since it is only one column, by default it is
coerced into a numeric vector:

> class(b)
[1] "numeric"
> b
  [1] 5.1 4.9 4.7 4.6 5.0 5.4 ...

The way to keep b a data frame is to use the argument drop = FALSE, as in
> b2 <- iris[, 1, drop = FALSE]    # b2 is now a data frame
> dd <- merge(iris, b2)
> head(dd)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          4.3         3.0          1.1         0.1  setosa
2          4.4         2.9          1.4         0.2  setosa
3          4.4         2.9          1.4         0.2  setosa
4          4.4         2.9          1.4         0.2  setosa
5          4.4         3.0          1.3         0.2  setosa
6          4.4         3.0          1.3         0.2  setosa

HTH,
Dennis

>
> > head(a)
>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> 1          5.1         3.5          1.4         0.2  setosa
> 2          4.9         3.0          1.4         0.2  setosa
> 3          4.7         3.2          1.3         0.2  setosa
> 4          4.6         3.1          1.5         0.2  setosa
> 5          5.0         3.6          1.4         0.2  setosa
> 6          5.4         3.9          1.7         0.4  setosa
> > head(b)
> [1] 5.1 4.9 4.7 4.6 5.0 5.4
> >
> > c <-merge(a,b)
> > head(c)
>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   y
> 1          5.1         3.5          1.4         0.2  setosa 5.1
> 2          4.9         3.0          1.4         0.2  setosa 5.1
> 3          4.7         3.2          1.3         0.2  setosa 5.1
> 4          4.6         3.1          1.5         0.2  setosa 5.1
> 5          5.0         3.6          1.4         0.2  setosa 5.1
> 6          5.4         3.9          1.7         0.4  setosa 5.1
> >
> > NCOL(a)
> [1] 5
> > NCOL(b)
> [1] 1
> > NCOL(c)
> [1] 6
> >
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/where-did-the-column-names-go-to-tp2306267p2306267.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to