Change row names of a dataframe in R -
i have dataframe df
df<-structure(list(site_id = structure(c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l), .label = c("day", "id.1.00000", "id.10.0000", "id.11.0000", "id.12.0000", "id.13.0000", "id.14.0000", "id.15.0000", "id.16.0000", "id.17.0000", "id.18.0000", "id.19.0000", "id.2.00000", "id.20.0000", "id.21.0000", "id.22.0000", "id.23.0000", "id.24.0000", "id.25.0000", "id.26.0000", "id.27.0000", "id.28.0000", "id.29.0000", "id.3.00000", "id.30.0000", "id.31.0000", "id.32.0000", "id.33.0000", "id.34.0000", "id.35.0000", "id.36.0000", "id.37.0000", "id.38.0000", "id.39.0000", "id.4.00000", "id.40.0000", "id.41.0000", "id.42.0000", "id.43.0000", "id.44.0000", "id.45.0000", "id.46.0000", "id.47.0000", "id.48.0000", "id.49.0000", "id.5.00000", "id.50.0000", "id.51.0000", "id.52.0000", "id.53.0000", "id.54.0000", "id.55.0000", "id.56.0000", "id.57.0000", "id.58.0000", "id.59.0000", "id.6.00000", "id.60.0000", "id.61.0000", "id.62.0000", "id.63.0000", "id.64.0000", "id.65.0000", "id.66.0000", "id.67.0000", "id.68.0000", "id.7.00000", "id.8.00000", "id.9.00000", "month"), class = "factor"), year = structure(c(1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l, 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l, 1l, 2l, 3l, 4l, 5l, 6l), .label = c("2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012"), class = "factor"), gpp = c(6.81017, 6.57111, 7.15914, 7.26641, 6.74574, 7.26397, 6.85568, 7.91268, 8.03652, 7.399, 7.70301, 6.79211, -0.0446012, -0.0541655, 0.00244928, 0.013512, 0.0134132, -0.0389873, -0.0376675, -0.0484991, 0.0111558, -0.0170514, -0.0243235, -0.0442675, -0.0453214, -0.060819, -0.0090375, 0.0101045, 0.00918896, -0.0363025)), .names = c("site_id", "year", "gpp"), row.names = c(1l, 69l, 137l, 205l, 273l, 341l, 409l, 477l, 545l, 613l, 681l, 749l, 2l, 70l, 138l, 206l, 274l, 342l, 410l, 478l, 546l, 614l, 682l, 750l, 3l, 71l, 139l, 207l, 275l, 343l), class = "data.frame")
first, want rename each row name site_id
column follows: id.1.00000
1 id.10.00000
10 id.11.00000
11
this sample dataframe, in real dataframe have hundreds of ids.
then, want reorder (increasing) dataframe according ids.
anyone knows how it?
not sure understood problem, try :
df$site_id <- as.character(df$site_id) df$site_id <- sapply(df$site_id, function(id) as.numeric(strsplit(id,"[.]")[[1]][2])) df <- df[with(df, order(site_id)),]
(disclaimer : solution not involve row names)
Comments
Post a Comment