Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Setting the first column as an index

Is it possible to set the first column in my df to be the index? So my df becomes data of the 4 quarters by year, effectively year just becoming the index for plotting purposes

df snipet

df   
   year     Qtr1     Qtr2     Qtr3     Qtr4
 2 2005 13.950342 18.66797 21.73983 22.49755
 3 2006 17.116492 17.71430 20.50190 20.84159
 4 2007 18.918347 15.46002 17.87220 20.01701
 5 2008 18.508666 15.53064 16.06696 20.21658
 6 2009 16.255357 14.85671 15.28269 12.16084
 7 2010  8.889602 16.18042 19.74318 15.05649
 8 2011 15.130970 15.96652 17.79070 18.35192

Output required

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 df   
 year     Qtr1     Qtr2     Qtr3     Qtr4
 2005 13.950342 18.66797 21.73983 22.49755
 2006 17.116492 17.71430 20.50190 20.84159
 2007 18.918347 15.46002 17.87220 20.01701
 2008 18.508666 15.53064 16.06696 20.21658
 2009 16.255357 14.85671 15.28269 12.16084
 2010  8.889602 16.18042 19.74318 15.05649
 2011 15.130970 15.96652 17.79070 18.35192

>Solution :

we can use column_to_rownames from tibble package

library(tidyverse)

as_tibble(df) |> column_to_rownames(var = "year")
  • output
         Qtr1     Qtr2     Qtr3     Qtr4
2005 13.950342 18.66797 21.73983 22.49755
2006 17.116492 17.71430 20.50190 20.84159
2007 18.918347 15.46002 17.87220 20.01701
2008 18.508666 15.53064 16.06696 20.21658
2009 16.255357 14.85671 15.28269 12.16084
2010  8.889602 16.18042 19.74318 15.05649
2011 15.130970 15.96652 17.79070 18.35192
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading