2017-08-22 48 views
1

我有以下代碼行從數據框「Diff」中提取某些列。當應用data.table函數時,列名將被重命名爲V1,V2,V3等。我如何保持列名與以前相同(因爲它們出現在Diff數據框中)?應用Data.table函數後保持列名相同

data.table(Diff$FactSet.Fund.Code, Diff$FactsetDate.x, , Diff$DeskName.x) 
+6

'data.table(DIFF [C( 「FactSet.Fund.Code」, 「FactsetDate.x」, 「DeskName.x」))' – Roland

+1

你也可以做到這一點(在如果您想選擇不同的名稱): data.table(fund = Diff $ FactSet.Fund.Code,date = Diff $ FactsetDate.x,,desk = Diff $ DeskName.x) – user108363

回答

1

假設當你從一個data.frame去data.table出現問題,你可以先用diff <- data.table(diff)把你data.frame在其整體的data.table。 然後您可以使用subset()輕鬆地將您的data.table子集以及您想要保留的列名稱列表。這樣你會保留你的原名。這裏所說:

# Libraries 
library(data.frame) 

# Here's dataframe with some random data 
# that fits your description: 
diff = data.frame(FactSet.Fund.Code=rep(c("a","b"),c(2,3)),FactsetDate.x=1:5,FactsetSomethingelse=5:1) 
#class(diff) 

diff = data.table(diff) 
#class(diff) 

# The names fo the columns you'd like to keep: 
selection = c('FactSet.Fund.Code','FactsetDate.x') 

# Your desired subset: 
diff <- subset(diff,,selection) 

# And now it should look like this: 

    FactSet.Fund.Code FactsetDate.x 
1:     a    1 
2:     a    2 
3:     b    3 
4:     b    4 
5:     b    5