2

我想知道为什么使用=<-分配dataframe.

案例一:使用=

set.seed(100);a <- data.frame(a1=rnorm(10),a2=sample(c(1,0),10,replace=TRUE))

案例 b:使用<-

set.seed(100);b <- data.frame(b1 <- rnorm(10),b2 <- sample(c(1,0),10,replace=TRUE))

为什么会有以下区别?为什么第二种方法不保留变量/列名?

> a
           a1 a2
1 -0.50219235  0
2  0.13153117  0
3 -0.07891709  1
4  0.88678481  1
5  0.11697127  0

>b
  b1....rnorm.5. b2....sample.c.1..0...5..replace...TRUE.
1    -0.50219235                                        0
2     0.13153117                                        0
3    -0.07891709                                        1
4     0.88678481                                        1
5     0.11697127                                        0
4

3 回答 3

7

如果您查看?'data.frame',您将看到第一个参数的以下内容:

“...这些参数的形式为 value 或 tag = value。组件名称是根据标签(如果存在)或已解析的参数本身创建的。”

如果您使用 '<-' 而不是 '=',data.frame() 会将您的输入作为表达式读取(将 rnorm(10) 分配给 a1),而不是作为分配给 a 的(rnorm(10))标签(a1)

于 2012-07-26T19:04:09.293 回答
6

只是为了添加@Paul 和@Edward 之前的(非常好的)答案,这是您使用 of<-而不是=inside的结果data.frame()。即,您创建了两个新对象:

> b1
Error: object 'b1' not found
> b2
Error: object 'b2' not found
> set.seed(100);b <- data.frame(b1 <- rnorm(10),b2 <- sample(c(1,0),10,replace=TRUE))
> 
> b1
 [1] -0.50219235  0.13153117 -0.07891709  0.88678481  0.11697127  0.31863009 -0.58179068  0.71453271 -0.82525943 -0.35986213
> b2
 [1] 0 0 0 0 1 1 0 0 0 1
于 2012-07-26T19:34:01.823 回答
5

Within functions '=' is used as naming or referring the function to a variable of a particular name and <- refers to the assignment function. When R runs it will first resolve '<-" functions within your function parameters. It will then name the variable wither the thing to the left of the equal sign or the full expression in this case "b1 <- rnorm(10)". Finally it will resolve the function (in this case data.frame).

You almost always want to use the '=' within a function. There can be cases where you may want to nest an assignment "<-" but normally this will make your code ridiculous to read.

于 2012-07-26T19:13:16.283 回答