dplyr: select columns by position in NSE

无人久伴 提交于 2019-12-01 14:36:45

问题


I am trying to create a function that will select columns in a DF based on their position. I will always need the first column and then a subset of the DF. I have 1 object for each subset I need to select.

So far I have tried the following:

position <- "1,28:31"
DF %>%
  select_(.dots = position)

but I receive the following error:

Error in parse(text = x) : :1:2: unexpected 'x'

It would seem the problem is the comma separation in the column position.

Is there a workaround?


回答1:


You can just use select with a numeric vector of indexes:

positions <- c(1,28:31)
DF %>% select(positions)



回答2:


Using mtcars and:

  1. add auto as my first column
  2. select `c(auto, the first column, and any column containing an 'a')
  3. subset the data where only the sums of numeric rows are greater than the mean of all sums of those rows

mtcars %>% mutate(auto = row.names(.)) %>% select(auto, 1, contains('a')) %>% dplyr::filter(rowSums(.[-1]) > mean(rowSums(.[-1])))

             auto  mpg drat am gear carb
1       Mazda RX4 21.0 3.90  1    4    4
2   Mazda RX4 Wag 21.0 3.90  1    4    4
3      Datsun 710 22.8 3.85  1    4    1
4       Merc 240D 24.4 3.69  0    4    2
5        Merc 230 22.8 3.92  0    4    2
6        Merc 280 19.2 3.92  0    4    4
7        Fiat 128 32.4 4.08  1    4    1
8     Honda Civic 30.4 4.93  1    4    2
9  Toyota Corolla 33.9 4.22  1    4    1
10      Fiat X1-9 27.3 4.08  1    4    1
11  Porsche 914-2 26.0 4.43  1    5    2
12   Lotus Europa 30.4 3.77  1    5    2
13   Ferrari Dino 19.7 3.62  1    5    6
14  Maserati Bora 15.0 3.54  1    5    8
15     Volvo 142E 21.4 4.11  1    4    2



回答3:


Another solution with dplyr :

DF %>%
  select(!!c(1, 28:31))

cf : https://www.rdocumentation.org/packages/dplyr/versions/0.7.6/topics/select

You can also use with the dplyr version 0.8

DF %>%
      select(1, 28:31)



回答4:


 Select.by.pos <- function(pos, dt){

   return(dt[, pos])

 }

The pos argument should not be a string but a numeeic vector :) dt is a dataframe



来源:https://stackoverflow.com/questions/42493381/dplyr-select-columns-by-position-in-nse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!