如何动态选择列

时间:2016-10-27 08:58:55

标签: r dplyr

让我们假设我运行了一个随机的Forest模型,我得到了变量重要性信息,如下所示:

set.seed(121)
ImpMeasure<-data.frame(mod.varImp$importance)
ImpMeasure$Vars<-row.names(ImpMeasure)
ImpMeasure.df<-ImpMeasure[order(-ImpMeasure$Overall),]
row.names(ImpMeasure.df)<-NULL
class(ImpMeasure.df)
ImpMeasure.df<-ImpMeasure.df[,c(2,1)] # so now we have the importance variable info in a data frame
ImpMeasure.df

                   Vars        Overall
1            num_voted_users 100.000000
2     num_critic_for_reviews  58.961441
3       num_user_for_reviews  56.500707
4       movie_facebook_likes  50.680318
5  cast_total_facebook_likes  30.012205
6                      gross  27.652559
7     actor_3_facebook_likes  24.094213
8     actor_2_facebook_likes  19.633290
9                 imdb_score  16.063007
10    actor_1_facebook_likes  15.848972
11                  duration  11.886036
12                    budget  11.853066
13                title_year   7.804387
14   director_facebook_likes   7.318787
15      facenumber_in_poster   1.868376
16              aspect_ratio   0.000000

现在如果我决定我只想要前5个变量进行进一步分析,那么就这样做:

library(dplyr)
top.var<-ImpMeasure.df[1:5,] %>% select(Vars)
top.var

                  Vars
1           num_voted_users
2    num_critic_for_reviews
3      num_user_for_reviews
4      movie_facebook_likes
5 cast_total_facebook_likes

如何使用此信息仅从原始数据集(如下所示)中选择这些var,而不用拼写出实际的变量名称,而是使用top.var的输出....如何使用dplyr {{1这个函数..

我原来的数据集是这样的:

select

2 个答案:

答案 0 :(得分:0)

哈德利为here提供了答案:

select_(df, .dots = top.var)

答案 1 :(得分:0)

movies.imp<-moviesdf.cluster%>% select(one_of(top.vars),cluster)
head(movies.imp)
 ##   num_voted_users num_user_for_reviews num_critic_for_reviews
 ## 1          886204            3054                    723
 ## 2          471220            1238                    302
 ## 3          275868            994                    602
 ## 4         1144337            2701                    813
 ## 5               8            127                     37
 ## 6          212204            738                    462
 ##   movie_facebook_likes cast_total_facebook_likes cluster
 ## 1                33000                     4834       1
 ## 2                    0                     48350      1
 ## 3                85000                     11700      1
 ## 4               164000                    106759      1
 ## 5                    0                       143      2
 ## 6                24000                      1873      1

完成了!