These four ways of creating a dataframe
look pretty similar to me:
myData1 <- data.frame(a <- c(1,2), b <- c(3, 4))
myData2 <- data.frame(a = c(1,2), b = c(3,4))
myData3 <- data.frame(`<-`(a,c(1,2)), `<-`(b,c(3, 4)))
myData4 <- data.frame(`=`(a,c(1,2)), `=`(b,c(3,4)))
But If I print out the column names, I only get the nice column names that I would hope for if I use the =
operator. In all the other cases, the whole expression becomes the column name, with all the non-alphanumerics replaced by periods:
> colnames(myData1)
[1] "a....c.1..2." "b....c.3..4."
> colnames(myData2)
[1] "a" "b"
> colnames(myData3)
[1] "a....c.1..2." "b....c.3..4."
> colnames(myData4)
[1] "a...c.1..2." "b...c.3..4."
I've read about differences between <-
and =
when used in function calls in terms of variable scope, but as far as I can reason (possibly not very far), that doesn't explain this particular behavior.
- What accounts for the difference between
=
and<-
? - What accounts for the difference between the prefix and infix versions of
=
?