What is the difference between <- and = in data.frame?

in data.frame, <- a very strange conclusion can be obtained, 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 between <- and = in data.frame here?

+2
source share
3 answers

No, this is not strange. You invoke the data.frame constructor with named and unnamed objects.

I initially assumed that data.frame is a list and uses help (list) to explain the behavior of data.frame. Even the philosophy is the same (named and unnamed argument), it was a mistake, and the answer is to help data.frame

from? data.frame I take this part where we talk about argument names

If the arguments are all named and simple objects (not lists, data matrices), then the argument names will give the column names. For an unnamed simple argument, the dewaxed version of the argument is used as the name (with closed I (...) deleted).

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 note that this only explains the procedure for naming a simple argument to an object. Naming is more complicated than adding some points. For example, I find it very surprising that theses 2 statements are equivalent (using check.names = T or F):

  a <- data.frame(y <- list(x=1)) a <- data.frame(y = list(x=1)) 
+8
source

Yes, I believe so. The expression age<-c(5,6) gives the result (values ​​from <- ), but since you could not use the correct assignment operator for the formulas in the argument list, the complete expression was passed to check.names as the column name, It will convert all invalid characters to the point. You can prevent check.names from being called (and not that you'd normally want 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 
+7
source

R, like many languages, works from the inside out. So, in your example, for x, "name" is assigned the values ​​"n1" and "n2", and "age" is assigned the values ​​5 and 6, but when the data.frame file is created, the names of these columns are interpreted by R as full lines that you indicated with special characters becoming dots ... Better stick to the syntax of your y example.

0
source

Source: https://habr.com/ru/post/944913/


All Articles