Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr: select columns by position in NSE

Tags:

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?

like image 244
Jordi Vidal Avatar asked Feb 27 '17 18:02

Jordi Vidal


People also ask

How do I select a column by index in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.

Which of the following Dplyr functions allows you to subset observations or rows based on their values?

filter() allows you to select a subset of rows in a data frame.

What is Dplyr select?

select() is a function from dplyr R package that is used to select data frame variables by name, by index, and also is used to rename variables while selecting, and dropping variables by name.


1 Answers

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

positions <- c(1,28:31) DF %>% select(positions) 
like image 85
MrFlick Avatar answered Oct 26 '22 23:10

MrFlick