37

I am trying to do some manipulation on the last column in a generic way.

I found here on the forums this nice piece of code that returns the name of the last columns:

tail(names(train),1) #returns [1] "last"

I still can't figure out how to reference directly to my dataset's last columns as:

data$last

zx8754
  • 52,746
  • 12
  • 114
  • 209
thecheech
  • 2,041
  • 3
  • 18
  • 25

8 Answers8

84

just use ncol() to get the index of the last col

data[,ncol(data)]
Troy
  • 8,581
  • 29
  • 32
  • 1
    This appears to not work for data tables, only data frames. Am I missing something, why would this be? I'm running R 3.6.2 and data table 1.12.8. – jeromeResearch Nov 24 '20 at 00:43
  • @jeromeResearch for data.table, use `x[, ncol(x), with = FALSE]` or use ".." `ix <- ncol(x); x[, ..ix ]`. – zx8754 Nov 05 '21 at 13:17
15

Take the first element of the reversed vector of column names:

rev(names(mtcars))[1]
[1] "carb"

Similarly, to get the last column, you can use

rev(mtcars)[1]
James
  • 65,548
  • 14
  • 155
  • 193
6

To refer to last column:

colnames(data)[ncol(data)]
cianius
  • 2,272
  • 6
  • 28
  • 41
6

I prefer @Troy's solution, here is another way:

train[, tail(colnames(train), 1)]
zx8754
  • 52,746
  • 12
  • 114
  • 209
5

Troy's answer is simpler, and can be adapted to refer to "n" elements before the last column, using the ":" operator.

If you want to refer to the last threee columns, you could write:

data[,ncol(data)] # refers to the last column
data[,(ncol(data)-2):ncol(data)] # refers to the three last columns
MS Berends
  • 4,489
  • 1
  • 40
  • 53
BMLopes
  • 546
  • 6
  • 10
4

Function last_col() from tidyselect package may help. See also answer here

https://stackoverflow.com/a/44353413

tlask
  • 198
  • 9
3

You can use tail, but you have to coerce to list:

tail(as.list(mtcars), 1)

This will return a vector with the contents of the column. If you want to preserve the structure, you can use:

utils:::tail.default(mtcars, 1)

so that tail treats the input like a list. The only reason really to use this approach over Troy's are if you want more than just the last column (i.e. last N), where it becomes a lot easier to do it this way.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • ah yea that `list(data.frame)` to get the column names.. well i'm not using `R` for general purpose programming so just memorize these "interesting" things – WestCoastProjects Sep 16 '18 at 23:26
1

Here's an example of indexing just the last column name. Reference the names(df1[,ncol(df1)]):

df1 <- df1 %>% 
  add_column(new1 = NA, new2 = NA, new3 = NA, .after = names(df1[,ncol(df1)]))
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Dre Day
  • 338
  • 2
  • 8