Reputation: 443
I have the following line of code which extracts certain columns from the data frame "Diff". When applying the data.table function the columns names get renamed V1,V2,V3 etc. How can I keep the columns names the same as before (as they appear in the Diff dataframe)?
data.table(Diff$FactSet.Fund.Code, Diff$FactsetDate.x, , Diff$DeskName.x)
Upvotes: 2
Views: 1781
Reputation: 61074
Assuming that your problems arise when you go from a data.frame to a data.table, you can first turn your data.frame to a data.table in its entirety by using diff <- data.table(diff)
. Then you can easily subset your data.table using subset()
and a list with the names of columns you want to keep. This way you'll keep your original names. Here goes:
# 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
Upvotes: 1