How to select the last 3 columns in a data frame in R?

Asked

Viewed 207 times

2

I have a database that is updated monthly, which has 7 columns: USUÁRIO, and the last 6 months of the year with the amount of purchases for each month.

Following example:

print(DADOS)

    USUÁRIO  JAN  FEV  MAR  ABR  MAI  JUN 
     001      4    3    2    0    4    7
     002      1    9    0    1    1    9
     003      0    1    2    3    4    0
     004      2    4    0    8    0    6

The difficulty is when I need to select the last 3 months to calculate a sum. Selecting by the column name apparently solves the problem...

DADOS = DADOS %>%
select(ABR,MAI,JUN) %>%
mutate(SOMA = sum(ABR,MAI,JUN))

print(DADOS)

USUÁRIO  JAN  FEV  MAR  ABR  MAI  JUN  SOMA 
 001      4    3    2    0    4    7    20
 002      1    9    0    1    1    9    21
 003      0    1    2    3    4    0    10
 004      2    4    0    8    0    6    20

But the problem starts with the next monthly update, which instead of searching for the last 3 months (MAI,JUN,JUL), he continues to seek (ABR,MAI,JUN), it seems obvious that this would go wrong, but the question is how to select the last 3 columns of a data frame that does not search by name but by position?

1 answer

5


The function tail of R standard selects the n last observations of a vector. For example,

tail(letters, 10)
## [1] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

selects the last 10 letters of the alphabet. If you combine the function tail with names applied in the data frame, it is possible to select the last three columns of the data frame, no matter what they are. See in the example below how to do this:

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
iris %>% 
  select(tail(names(.), 3)) %>%
  head()
##   Petal.Length Petal.Width Species
## 1          1.4         0.2  setosa
## 2          1.4         0.2  setosa
## 3          1.3         0.2  setosa
## 4          1.5         0.2  setosa
## 5          1.4         0.2  setosa
## 6          1.7         0.4  setosa
  • Thanks for the reply @Marcus Nunes, I will test.

  • 1

    @Izakmandrak if Marcus Nunes' answer satisfied you please consider it as the solution to your problem.

  • @Flaviosilva as soon as I test the solution and find out if it satisfies or not, I will accept the answer smoothly, just as I did with my other questions. Thank you for reminding.

  • @Marcus Nunes it is possible to use mathematical operations after the use of tail? You can select the last 3 columns and then add them?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.