Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
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 (46k 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, 2019 in R Programming by Nigam (4k points)
+1 vote
1 answer
asked May 29, 2019 in R Programming by Krishna (2.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...