How to tell R/Shiny to select columns starting from specific column until end ?

Although it seems similar question but it isn't

Expected:
Select the data from particular column until end i.e delete everything to the left of the data.
But this particular column appears at different column indices

Therefore, simple approaches as below doesn't work

Select based on numbers

iris [,4:ncol(iris)]

iris[,"Petal.Width"]

Tried

iris[,"Petal.Width":colnames(iris)[ncol(iris)]]
iris[,`Petal.Width`:colnames(iris)ncol(iris)]

Since the Petal.Width can appear at any column position, it is must to use column names.

Is there any efficient way to tell to R,
How to select from Petal.Width until end of the columns irrespective of which column indices it appears ?

Both these work:

library(dplyr)
select(iris, Petal.Width:colnames(iris)[ncol(iris)])
library(data.table)
irisDT <- as.data.table(iris)
irisDT[, Petal.Width:ncol(irisDT)]

There may be more elegant versions.

1 Like

Thanks for elegant solutions :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.