dplyr:在NSE中按位置选择列

时间:2021-08-13 22:25:59

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.

我正在尝试创建一个函数,根据它们的位置选择DF中的列。我将始终需要第一列,然后是DF的子集。我需要为每个子集选择一个对象。

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'

解析时出错(text = x):: 1:2:意外'x'

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

似乎问题是列位置的逗号分离。

Is there a workaround?

有解决方法吗?

4 个解决方案

#1


17  

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

您可以使用带有索引的数字向量的select:

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

#2


1  

Using mtcars and:

使用mtcars和:

  1. add auto as my first column
  2. 添加auto作为我的第一列

  3. select `c(auto, the first column, and any column containing an 'a')
  4. 选择`c(auto,第一列,以及任何包含'a'的列)

  5. subset the data where only the sums of numeric rows are greater than the mean of all sums of those rows
  6. 数据的子集,其中只有数字行的总和大于这些行的所有总和的平均值

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

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


0  

 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

pos参数不应该是字符串而是数字矢量:) dt是一个数据帧

#4


0  

Another solution with dplyr :

dplyr的另一个解决方案:

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

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

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

#1


17  

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

您可以使用带有索引的数字向量的select:

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

#2


1  

Using mtcars and:

使用mtcars和:

  1. add auto as my first column
  2. 添加auto作为我的第一列

  3. select `c(auto, the first column, and any column containing an 'a')
  4. 选择`c(auto,第一列,以及任何包含'a'的列)

  5. subset the data where only the sums of numeric rows are greater than the mean of all sums of those rows
  6. 数据的子集,其中只有数字行的总和大于这些行的所有总和的平均值

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

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


0  

 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

pos参数不应该是字符串而是数字矢量:) dt是一个数据帧

#4


0  

Another solution with dplyr :

dplyr的另一个解决方案:

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

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

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