This is a follow up on this question. @Sotos had provided a correct answer to the question, but indeed my question was meant more theoretically.
I am aware that this might all not be very practical, but it is more out of curiosity. How can I access the named attributes of a named object for 'negative selection' (dropping) by name ?
'Positive selection' is neat:
data.frame(year = 1996:1998, group = letters[1:3]) ['group']
group
1 a
2 b
3 c
But following this approach it could quickly get cumbersome with 'negative selection', especially for larger data frames:
data.frame(year = 1996:1998, group = letters[1:3])[!names(data.frame(year = 1996:1998, group = letters[1:3])) %in% 'year']
group
1 a
2 b
3 c
I know that you could use subset
or dplyr::select
:
data.frame(year = 1996:1998, group = letters[1:3]) %>% select(- year)
# or
subset(data.frame(year = 1996:1998, group = letters[1:3]), select = -year)
group
1 a
2 b
3 c
But I wondered if there are other means, based on selection using [
, such as to use the foo[!names %in% x]
solution without attributing a name to foo
beforehand and without the cumbersome repetition of the data frame as in my example code.