2

in the data.frame ,<- can get very strange output,what is the reason?

x<-data.frame(name<-c("n1","n2"),age<-c(5,6))
y<-data.frame(name=c("n1","n2"),age=c(5,6))
> x
name....c..n1....n2.. age....c.5..6.
1                    n1              5
2                    n2              6
> y
  name age
1   n1   5
2   n2   6

what is the difference meaning between <- and = in data.frame here?

Bqsj Sjbq
  • 1,231
  • 1
  • 12
  • 9
  • May also look at http://stackoverflow.com/questions/11676179/different-behavior-in-using-versus-operator-while-assigning-a-dataframe and http://stackoverflow.com/questions/1741820/assignment-operators-in-r-and – Stat-R Dec 31 '12 at 18:04

3 Answers3

8

No this it is not strange. You call the constructor of a data.frame with named and unnamed objects.

Originally I supposed that a data.frame is a list and use help(list) to explain the behaviour of data.frame. Even the philosophy is the same (named and unnamed argument) it was a mistake and the answer is in the help of data.frame

from ?data.frame I take this part where we speak about the names of arguments

If the arguments are all named and simple objects (not lists, matrices of data frames) then the argument names give the column names. For an unnamed simple argument, a deparsed version of the argument is used as the name (with an enclosing I(...) removed).

So

x<-data.frame(name<-c("n1","n2"),age<-c(5,6))

this is equivalent to :

 x <- data.frame(c("n1","n2"),c(5,6))   ## unnamed objects The functions return dotted pair list 
 name<-c("n1","n2")
 age<-c(5,6)

Then for y

y<-data.frame(name=c("n1","n2"),age=c(5,6))  ## named objects functions return a list 

But notice that this explain only the naming procedure for simple object argument. The naming is more complicated than adding some dots. For example, I find very amazing that theses 2 statements are equivalent(with check.names=T or F) :

         a <- data.frame(y <- list(x=1)) 
         a <- data.frame(y = list(x=1)) 
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    I do not think that the information in `help(list)` explains this. Look at the result of `list(x<-1:4)` – IRTFM Dec 30 '12 at 08:24
  • @DWin you're right! thanks I correct my answer, hope this one explain better the question. – agstudy Dec 30 '12 at 09:34
  • In the last bit, the value of the expression `y <- list(x=1)` _does_ have a value, and the value of that object _does_ have a name. – IRTFM Dec 30 '12 at 16:03
7

Yes, I suppose you do. The expression age<-c(5,6) does give a result (the values from <-), but since you failed to use the proper assignment operator for formals in an argument list, the full expression was passed to check.names as the name of the column. It converts all the invalid characters to dots. You can prevent check.names from being called (not that you would generally wnat to do this.

> x<-data.frame(name<-c("n1","n2"),age<-c(5,6), check.names=FALSE)
> x
  name <- c("n1", "n2") age <- c(5, 6)
1                    n1              5
2                    n2              6
IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

R, like many languages, works from the inside out.. So in your example for x, 'name' is being assigned values of 'n1' and 'n2', and 'age' values of 5 and 6, but when the data.frame is created, the names of those columns are interpreted by R as the complete strings you gave with the special characters becoming dots... Best to stick with the syntax of your y example.

N8TRO
  • 3,348
  • 3
  • 22
  • 40
  • Unfortunately, R does not always work "inside out". See [non-standard evaluation](http://adv-r.had.co.nz/Computing-on-the-language.html) – Steve Pitchers Jul 21 '15 at 17:42