35

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
  • 46,390
  • 10
  • 104
  • 180
thecheech
  • 1,811
  • 2
  • 16
  • 25

8 Answers8

81

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

data[,ncol(data)]
Troy
  • 8,381
  • 27
  • 31
  • 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 – 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
  • 63,608
  • 14
  • 148
  • 190
6

To refer to last column:

colnames(data)[ncol(data)]
cianius
  • 2,182
  • 6
  • 25
  • 40
6

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

train[, tail(colnames(train), 1)]
zx8754
  • 46,390
  • 10
  • 104
  • 180
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
  • 3,338
  • 1
  • 27
  • 45
BMLopes
  • 476
  • 5
  • 10
4

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

https://stackoverflow.com/a/44353413

tlask
  • 198
  • 8
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
  • 50,078
  • 8
  • 87
  • 138
  • 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
0

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
  • 14,910
  • 4
  • 18
  • 37
Dre Day
  • 73
  • 7