when I use tidyr's gather() function and manipulate my dataframe, I lose row names of prev. data frame.
this is output of my rstudio console
> DF <- as.data.frame((freethrows/Games), row.names = rownames(Games), col.names = colnames(Games))
> head(DF)
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
KobeBryant 8.700000 8.662338 7.597561 5.890244 6.013699 5.890244 6.568966 6.730769 3.000000 5.600000
JoeJohnson 3.182927 4.122807 3.853659 3.784810 2.894737 2.708333 2.633333 1.833333 2.012658 1.762500
LeBronJames 7.607595 6.269231 7.320000 7.333333 7.802632 6.367089 6.241935 5.302632 5.701299 5.434783
CarmeloAnthony 7.162500 7.061538 6.025974 5.621212 7.362319 6.584416 5.363636 6.343284 5.961039 4.725000
DwightHoward 4.341463 4.756098 6.451220 6.379747 5.890244 7.000000 5.203704 4.671053 4.915493 3.487805
ChrisBosh 6.771429 6.710145 7.044776 6.545455 6.714286 4.987013 4.017544 3.256757 2.822785 4.068182
> DF_gathered <- DF %>%
+ gather('2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', key = 'year', value = 'freeThrowsPerGame')
> head(DF_gathered)
year freeThrowsPerGame
1 2005 8.700000
2 2005 3.182927
3 2005 7.607595
4 2005 7.162500
5 2005 4.341463
6 2005 6.771429
>
after I pipe my DF into gather() I expected the rownames to remain.
as rownames must have unique values, I can't have rownames as rownames after gather()
, so I use rownames_to_column()
to have rownames as the 1st column and also after gather()
, I can't use column_to_rownames("player")
, due to unique values problem.
thanks @m-aurélio.