Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - select last 2 columns

Tags:

select

r

I have an dataframe below, in reality it actually has much more columns, and I´d like to select only last two columns.

   df <- read.table(text="
                 date1       date2              date3
    1            NA          2016-12-01    2016-12-01
    2            2017-01-01  2018-10-01    2016-12-01 
    3            2016-12-01  NA            2016-12-01
    4            NA          NA            2016-12-01
", header=TRUE)

How can I do it without specifying column names?

df %>%
  select(date2, date3)
like image 579
Sklenicka Avatar asked Dec 28 '18 00:12

Sklenicka


3 Answers

You could use select with tail to get last 2 column names

library(dplyr)

df %>% select(tail(names(.), 2))

#       date2      date3
#1 2016-12-01 2016-12-01
#2 2018-10-01 2016-12-01
#3       <NA> 2016-12-01
#4       <NA> 2016-12-01

which in base R is

df[tail(names(df), 2)]
like image 85
Ronak Shah Avatar answered Oct 08 '22 07:10

Ronak Shah


Late to the party. Just for the records, there's a convenient way in tidyverse to select the last column(s):


library(tidyverse)
df %>% 
  select(last_col(offset = 1), last_col())

       date2      date3
1 2016-12-01 2016-12-01
2 2018-10-01 2016-12-01
3       <NA> 2016-12-01
4       <NA> 2016-12-01

Created on 2021-01-20 by the reprex package (v0.3.0)

Source

Selecting the first column(s) is straight forward:

> df %>% 
+   select(1,2)
       date1      date2
1       <NA> 2016-12-01
2 2017-01-01 2018-10-01
3 2016-12-01       <NA>
4       <NA>       <NA>
like image 34
Sebastian Sauer Avatar answered Oct 08 '22 07:10

Sebastian Sauer


We can just make use of ncol

df[(ncol(df)-1):ncol(df)]
#       date2      date3
#1 2016-12-01 2016-12-01
#2 2018-10-01 2016-12-01
#3       <NA> 2016-12-01
#4       <NA> 2016-12-01

Or using select_at

library(tidyverse)
df %>%
    select_at((ncol(.)-1) : ncol(.))
like image 3
akrun Avatar answered Oct 08 '22 06:10

akrun