@Joris Meys great answer to this famous question suggests to drop columns by name using a list of names. It requires previous assignment of a name to the data frame/ matrix and using names(df)
, or for matrices colnames(matrix)
.
Out of curiosity, I wondered if a similar strategy is possible without assigning a name to the data frame/ matrix in a first place. I was pondering on this fact on answering this question (from where I nicked my sample data).
My suggested solution drops the column with select
as follows:
bind_cols(split(df$b, df$year)) %>% select(-'1997')
I was first trying to use do.call(cbind, split(df$b, df$year))
instead, but this gave a matrix, and dplyr::select
did not like that. Now I could of course positive select:
do.call(cbind, split(df$b, df$year))[,c('1996','1998')]
I could also use subset
:
subset(do.call(cbind, split(df$b, df$year)), select = - `1997`)
My question is how to use [
for 'negative selection' by name (here: dropping of 1997
), without previous assignment of the matrix/ data frame, i.e. in a one liner.
data
set.seed(77)
df <- data.frame(year = rep(1996:1998,3), a = runif(9), b = runif(9), e = runif(9))
# required result something like: (result from code above)
1996 1998
[1,] 0.4569087 0.9881951
[2,] 0.1658851 0.4475605
[3,] 0.3647157 0.7033574