.. especially if you need to get multiple column indices, the approach below applies:
> df <- data.frame(a=rnorm(100),b=rnorm(100),c=rnorm(100)) > which(names(df)%in%c("b", "c")) [1] 2 3
if you use this for a subset of df you don't need to ()
> df_sub <- df[, names(df)%in%c("b", "c")] > head(df_sub) bc 1 0.1712754 0.3119079 2 -1.3656995 0.7111664 3 -0.2176488 0.7714348 4 -0.6599826 -0.3528118 5 0.4510227 -1.6438053 6 0.2451216 2.5305453
Kay Feb 14 2018-12-12T00: 00Z
source share