簡體   English   中英

創建數據框時的R name列

[英]R name column when creating data frame

我正在從數據框中獲取列並使用它們來創建另一個數據框,但名稱不斷變得混亂,而不是保留原始名稱。 我該如何避免這種情況?

> 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

這是因為你把括號加倍(( 。比較

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函數。

創建數據框時,不要使用雙括號:

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

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

只需刪除一個括號:

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

應該現在正在工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM