R - select last 2 columns
Asked Answered
P

6

8

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)
Pileus answered 28/12, 2018 at 0:48 Comment(0)
T
16

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)]
Taboo answered 28/12, 2018 at 1:12 Comment(4)
Nice. The concept works without dplyr too: df[,tail(names(df),2)]Inkhorn
@EdwardCarney yes, you are right. I have included that as well in the answer.Taboo
Thats perferct, how you select togheder first and last columns?Pileus
@Pileus you mean df %>% select(1, tail(names(.), 2)) ?Taboo
A
8

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>
Acropetal answered 20/1, 2021 at 12:58 Comment(0)
W
3

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(.))
Wilscam answered 28/12, 2018 at 2:14 Comment(1)
Or slightly modifying the first suggestion: df[-(1:(ncol(df)-n))], where using n you can just specify how many columns you want from the end. In the case of the example dataframe, it would be df[-(1:(ncol(df)-2))]Shrike
K
0

Without dplyr:

df.minus.2.columns <- df[,c(-1,-2)]

Sorry, no idea how you'd do so with dplyr.

Koreykorff answered 28/12, 2018 at 0:54 Comment(1)
The minus indexing means dropping columns. This answer drops the first and second columns leaving only the third left (returned as a list).Shrike
H
0

With dplyr and using vectorized operations, you can select the last n observations with:

n <- 2
library(tidyverse)
df %>% tail()
df %>% select(last_col()-(n-1):0) %>% tail()

       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
Harlequin answered 5/10, 2021 at 23:44 Comment(0)
M
0

Another variation:

library(tidyverse)

diamonds |> 
  select(9:last_col())
#> # A tibble: 53,940 × 2
#>        y     z
#>    <dbl> <dbl>
#>  1  3.98  2.43
#>  2  3.84  2.31
#>  3  4.07  2.31
#>  4  4.23  2.63
#>  5  4.35  2.75
#>  6  3.96  2.48
#>  7  3.98  2.47
#>  8  4.11  2.53
#>  9  3.78  2.49
#> 10  4.05  2.39
#> # ℹ 53,930 more rows

Created on 2024-05-02 with reprex v2.0.2

Miyamoto answered 2/5 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.