我有以下代码行,它从数据框“Diff”中提取某些列。应用data.table函数时,列名称将重命名为V1,V2,V3等。如何保持列名与以前相同(因为它们出现在Diff数据帧中)?
data.table(Diff$FactSet.Fund.Code, Diff$FactsetDate.x, , Diff$DeskName.x)
答案 0 :(得分: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