0

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.

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    why are you using `data.frame(year = 1996:1998, group = letters[1:3])` everywhere instead of assigning it to a variable say `df` and then using it ? Your `data.frame(year = 1996:1998, group = letters[1:3])[!names(data.frame(year = 1996:1998, group = letters[1:3])) %in% 'year']` would be reduced to `df[!names(df) %in% 'year']` – Ronak Shah Sep 19 '18 at 08:08
  • @RonakShah That is the whole point of my question. I know it might not be practical but my question is of curious nature – tjebo Sep 19 '18 at 08:09
  • I don't quite get it. It seems to me that @RonakShah's comment has solve your question in your desired format, `foo[!names %in% x]`. Please clarify what exactly you're looking for. – ytu Sep 19 '18 at 08:31
  • @ytu The theoretical construct would be to access the objects attributes without assigning it to a variable. If it is not possible, fine. I was just curios – tjebo Sep 19 '18 at 08:33
  • 1
    Then from where would you think to possibly access the _object_ attributes as no object is provided? – ytu Sep 19 '18 at 08:38
  • @ytu This is also what my question is about. However, the object is indeed provided, it just doesn't have a name/variable assigned to it. `dplyr::select` and `subset` seem to be able to access the object's dim names and I was (and still am) curious if one can access those with `[` for negative selection too – tjebo Sep 19 '18 at 08:48
  • For `dplyr::select` and `subset`, the object is provided in the function. with `[`, or specifically in your case, `[!names %in% x]`, the object `foo` is not provided in the operation. You may start from looking at how the source codes of `dplyr::select` and `subset` work. – ytu Sep 19 '18 at 08:55
  • @ytu I really appreciate your thoughts and this is also what I was asking for :) So bascially you would suggest that this approach would not be possible? Thanks :) – tjebo Sep 19 '18 at 08:57
  • 1
    I think that approach is impossible. Still you can wait for others' opinions. – ytu Sep 19 '18 at 08:59

0 Answers0