This is more of a magrittr/dplyr question, so not really the appropriate forum, however I'll make a few notes.
For follow up questions, or future questions like these, you should go over to the manipulatr google group, which seems to be the forum to go to for all questions realted to plyr, reshap2, dplyr, and tidyr (or stackoverflow would also work, too, I think)
Part of the dplyr/tidy manifesto is that "rownames are bad", and a few of the dplyr verbs (like mutate
) actually strip rownames from your data.frame, witness:
R> library(magrittr)
R> library(dplyr)
R> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 | 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag | 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 | 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive | 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout| 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant | 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
R> mutate(mtcars, score=rnorm(nrow(mtcars))) %>% head
mpg cyl disp hp drat wt qsec vs am gear carb score
1| 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 -0.6913923
2| 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 -0.6626077
3| 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 -0.6879882
4| 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 -0.9331001
5| 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 -0.5598960
6| 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 -0.1118152
So, first thing to note is that it looks like you're working with a dplyr::tbl_df
and not a normal data.frame
, and no matter how hard you try, you may not be able to add rownames to it all (or you might not see them when printing tbl_df to the console), so you might have to convert your back back to a normal data.frame
to get the behavior you're probably expecting.
Second, I think you're calling the wrong function. add_rownames()
takes the rownames from the current data.frame
and adds them as a new column to it (presumably because you will soon be stripping (or losing) the rownames in your data manipulation piping chain). The function you want to use to add rownames is magrittr::set_rownames
I think we're on different wavelengths here, as it's not quite clear what you're after. If the resulting data.frame has the results you expected, then I guess this is OK, but only you can make that call.