How do I subset column variables in DF1 based on the important variables I got in DF2?

泄露秘密 提交于 2019-11-28 14:31:34

I can't find a dupe so here goes- simply subset by the values of as.character(df1$ID) as in

df2[as.character(df1$ID)] ## Or just `df2[df1$ID]` if its already a character
#   x1 x2 x5
# 1  1 11 41
# 2  2 12 42
# 3  3 13 43
# 4  4 14 44
# 5  5 15 45

The reason for as.character is in order to avoid sub-setting by df1$ID underlying storage mode (integer) rather by it's levels


Though this question is tagged with data.table, so we could also do this by reference (if we have a data.table)- no need to convert to character

setDT(df2)[, setdiff(names(df2), df1$ID) := NULL]
df2
#    x1 x2 x5
# 1:  1 11 41
# 2:  2 12 42
# 3:  3 13 43
# 4:  4 14 44
# 5:  5 15 45
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!