0 votes
1 view
in R Programming by (3.5k points)

Suppose I have a vector that is nested in a dataframe one or two levels. Is there a quick and dirty way to access the last value, without using the length() function? Something ala PERL's $# special var?

So I would like something like:

dat$vec1$vec2[$#]

instead of

dat$vec1$vec2[length(dat$vec1$vec2)]

1 Answer

0 votes
by (46.1k points)

There are many ways to do it:

  • tail()

  • x[length()]

  • dplyr::last()

  • x[end()[1]]

  • mylast()

  • rev()[1]

as some of them are either C++ function implemented through Rcpp or used by installing a package, I recommend you to use tail() as it's a built-in function and works with data frames too.

tail(vector, n=1)

Hope this helps. Cheers...!!

Related questions

+1 vote
1 answer
+1 vote
1 answer
asked May 23 in R Programming by Nigam (4.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...