2012-01-09 27 views
3

我正在从数据框中取出列并使用它们来创建另一个数据框,但名称不断变得混乱而不是保留原来的名称。我如何避免这种情况?创建数据框时的R名称列

> newDigit1 <- data.frame((security_id = rsPred1$security_id)) 
> head(newDigit1) 
    X.security_id...rsPred1.security_id. 
1         1 
2         6 
3         5 
4         3 
5         3 
6         2 

它应该是这样的:

> newDigit1 <- data.frame((security_id = rsPred1$security_id)) 
> head(newDigit1) 
           security_id 
1         1 
2         6 
3         5 
4         3 
5         3 
6         2 

回答

5

这是因为你已经涨了一倍括号((。 比较

dfr <- data.frame(x = 1:5) 
#Case 1 
data.frame(x = dfr$x) 
#Case 2 
data.frame((x = dfr$x)) 

在情况1中,x = dfr$x是在通入data.frame功能的名称 - 值对。

在情况2中,(x = dfr$x)返回一个没有名字的向量,所以R发明了一个临时的向量,然后将该向量传递给data.frame函数。

+0

谢谢,很好的接收,做到了。 – screechOwl 2012-01-09 17:05:31

4

当你创建自己的数据帧,不具备双括号:

newDigit1 <- data.frame(security_id = rsPred1$security_id) 

newDigit1 <- data.frame((security_id = rsPred1$security_id)) 
2

只需删除一个支架:

newDigit1 <- data.frame(security_id = rsPred1$security_id) 

现在应该工作!

相关问题