重新排序,排除一列并将其他列保留在R中?

时间:2018-08-19 11:14:28

标签: r dplyr

这是我的玩具数据框:

structure(list(a = c(1, 2), b = c(3, 4), c = c(5, 6), d = c(7, 
8)), .Names = c("a", "b", "c", "d"), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

现在,我想重新排序并排除其中一列,并保留其他列:

df %>% select(-a, d, everything())

我希望我的df是:

d b c
7 3 5
8 4 6

我得到以下信息:

          b     c     d     a
        <dbl> <dbl> <dbl> <dbl>
    1     3     5     7     1
    2     4     6     8     2

1 个答案:

答案 0 :(得分:2)

CREATE FUNCTION LIST_AGG ( @id int ) RETURNS TABLE AS RETURN ( SELECT id ,STUFF(( SELECT ', ' + val FROM (select id , val FROM test t) A WHERE a.id = b.id FOR XML PATH(''),TYPE /* Use .value to uncomment XML entities e.g. &gt; &lt; etc*/ ).value('.','NVARCHAR(255)') ,1,2,'') as list FROM (select @id id ) B GROUP BY id ) select distinct t1.id, t2.list from Test t1 cross apply list_agg(t1.id) t2 保留在-a的最后。即使,我们在一开始就删除了select,而在结尾a仍在检查整个数据集的列名

everythig()