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?
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.
filter() allows you to select a subset of rows in a data frame.
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.
You can just use select
with a numeric vector of indexes:
positions <- c(1,28:31) DF %>% select(positions)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With