select() columns using their positions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

select() columns using their positions

Ivan Calandra-5
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

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: select() columns using their positions

Jeff Newmiller
Did you try it?

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

On August 20, 2020 1:41:13 AM PDT, Ivan Calandra <[hidden email]> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: select() columns using their positions

Ivan Calandra-5
OK, my bad... I'm sure I had tried it and it didn't work, but I guess
the error was somewhere else...

Thank you!
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 20/08/2020 11:03, Jeff Newmiller wrote:

> Did you try it?
>
> mydata %>%
>   select( c( 1, 2, 4 ) )
>
> On August 20, 2020 1:41:13 AM PDT, Ivan Calandra <[hidden email]> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: select() columns using their positions

Rui Barradas
Hello,

It is also possible to select by vectors of indices (as opposed to a
vector):
top_n is just to not clutter the display.


library(dplyr)

data(iris)

iris %>% select(1, 3, 4) %>% top_n(5)
iris %>% select(c(1, 3), 4) %>% top_n(5)


Hope this helps,

Rui Barradas


Às 10:05 de 20/08/20, Ivan Calandra escreveu:

> OK, my bad... I'm sure I had tried it and it didn't work, but I guess
> the error was somewhere else...
>
> Thank you!
> Ivan
>
> --
> Dr. Ivan Calandra
> TraCEr, laboratory for Traceology and Controlled Experiments
> MONREPOS Archaeological Research Centre and
> Museum for Human Behavioural Evolution
> Schloss Monrepos
> 56567 Neuwied, Germany
> +49 (0) 2631 9772-243
> https://www.researchgate.net/profile/Ivan_Calandra
>
> On 20/08/2020 11:03, Jeff Newmiller wrote:
>> Did you try it?
>>
>> mydata %>%
>>    select( c( 1, 2, 4 ) )
>>
>> On August 20, 2020 1:41:13 AM PDT, Ivan Calandra <[hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.