2012-01-09 34 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) 

現在應該工作!

相關問題