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)
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)]
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>
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(.))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With