Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutannihilation/07df7ac382b76cee5c55 to your computer and use it in GitHub Desktop.
Save yutannihilation/07df7ac382b76cee5c55 to your computer and use it in GitHub Desktop.
data.frame is tricky!

When specifying named arguments, you have to use =, instead of <-.

This code

data.frame(x2 <- x2, y2 <- y2)

is actually iterpreted like this:

data.frame( `x2 <- x2` = (x2 <- x2), `y2 <- y2` = (y2 <- y2) )

x2 <- x2 and y2 <- y2 are column name. As you know, whitespaces and symbols are converted to .. therefore you will get x2....x2 and y2....y2 as column names.

(For (x2 <- x2) and (y2 <- y2), they return x2 and y2, their assignee values.

So, the returned data.frame has no columns named x2 or y2.

> f <- function(){
+     x2 <- 1:10
+     y2 <- x2^2
+     df <- data.frame(x2 <- x2, y2 <- y2)
+     df
+ }
> f()
   x2....x2 y2....y2
1         1        1
2         2        4
3         3        9
4         4       16
5         5       25
6         6       36
7         7       49
8         8       64
9         9       81
10       10      100

Don't worry. You can use =. Perfect.

data.frame(x2 = x2, y2 = y2)

Sorry for my poor English…\(ツ)/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment