[R] Strange names when creating a data.frame: Difference between <- and =

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Oct 2 21:31:35 CEST 2007


On Tue, 2 Oct 2007, John Kane wrote:

> This is just a curiosity question.  Why do the two
> different syntaxes for df1 and df2 give such different
> results in the names(dfx)?

Because only in the second case did you specify the names in the call.
When you do nas <- c("A" ...) the argument has no name but you create an 
object called 'nas' in the calling environment (the workspace, it looks 
like).

Perhaps you are confused by thinking '=' is an assignment operator in R, 
but it is so only when it is not used in any other sense.

>
> Thanks
>
>
> df1 <- data.frame(nas = c("A",  "B" , "B" ,"C" ,"B",
> "A", "D"),nums = c(3,    2,    1,   1,   2,    3,  7))
>
> df2 <- data.frame(nas  <- c("A",  "B" , "B" ,"C" ,"B",
> "A", "D"),nums  <- c(3,    2,    1,   1,   2,    3,
> 7))
>
> df1; df2
>
>> df1
>  nas nums
> 1   A    3
> 2   B    2
>    .   .
>
>>  df2
>
>   nas....c..A....B....B....C....B....A....D..
> nums....c.3..2..1..1..2..3..7.
> 1                                           A
>                    3
> 2                                           B
>
> 3
> .                                           .
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list