[R] select() columns using their positions

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Aug 20 11:03:47 CEST 2020


Did you try it?

mydata %>%
  select( c( 1, 2, 4 ) )

On August 20, 2020 1:41:13 AM PDT, Ivan Calandra <calandra using rgzm.de> wrote:
>Dear useRs,
>
>I'm still trying to learn tidyverse syntax.
>
>I would like to select() columns based on their positions/indices, but
>I
>cannot find a way to do that (I've seen a lot about doing that for
>rows,
>but I could not find anything for columns). I thought it would be
>obvious, but I cannot find it.
>
>Basically, I am looking for something like:
>mydata %>%
>  select( vector_of_indices )
>I know that the pipe is useless here, but there are more steps in my
>real code.
>
>The helper num_range() works only when headers contains the positions
>(e.g. "x1, x2...").
>
>Of course, it's easy using "[", but I expected it would be possible
>with
>select() as well; it would make the code more readable than:
>mydata %>%
>  .[ vector_of_indices ]
>
>Thank you for your help.
>Ivan

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list