stevensanderson,
@stevensanderson@mstdn.social avatar

🔍 How to Extract Last Row in Data Frame in R

Base R
Use nrow(my_df) to get the total rows.
Extract the last row with indexing: my_df[nrow(my_df), ].

dplyr
Use tail(my_df, 1) to get the last row.

data.table
Convert to data.table: my_dt <- as.data.table(my_df).
Get last row using .N: my_dt[.N].

Now you know three ways to extract the last row. Try it yourself! 📊

#RProgramming #DataFrames #CodingTips #R #RStats #Programming #Coding #Data #datatable #dplyr #baseR

Post: https://www.spsanderson.com/steveondata/posts/2024-04-18/

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming
  • thenastyranch
  • DreamBathrooms
  • osvaldo12
  • magazineikmin
  • mdbf
  • Youngstown
  • everett
  • slotface
  • GTA5RPClips
  • rosin
  • InstantRegret
  • kavyap
  • khanakhh
  • Durango
  • provamag3
  • ngwrru68w68
  • modclub
  • cubers
  • cisconetworking
  • tester
  • tacticalgear
  • normalnudes
  • ethstaker
  • megavids
  • Leos
  • anitta
  • JUstTest
  • lostlight
  • All magazines