[R] How to copy and paste a row at the end of each group of a table?

Kristi Glover kristi.glover at hotmail.com
Mon Oct 31 13:59:21 CET 2016


Hi R Users,

I have a big table with many classes, I need to copy a fist row of each class and put at the end of each class.

I split the table but I wonder of copying and putting at the end of the class. Here is an example. I was trying to get the table from "dat" to "dat1". In "dat", there is a column with "day", has only 12 rows for each class. but I want to copy first row of each class and put on the bottom of the class (see table: "dat1"), the value of the column of "day" need to be "361" but the value of temp should be same as of row 1.


Thanks for your suggestions in advance.


Thanks,

dat<-structure(list(site = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), day = c(1, 31, 61, 91, 121,
151, 181, 211, 241, 271, 301, 331, 1, 31, 61, 91, 121, 151, 181,
211, 241, 271, 301, 331), temp = c(8.3, 10.3, 9.4, 6.1, 3, 1.3,
1, 0.8, 1, 1.4, 2.7, 5.1, 9, 11.2, 9.6, 5.7, 2, 0.8, 0.6, 0.4,
0.4, 0.6, 1.5, 4.5)), .Names = c("site", "day", "temp"), row.names = c(NA,
-24L), class = "data.frame")

dat1<-structure(list(site = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), day = c(1, 31, 61, 91,
121, 151, 181, 211, 241, 271, 301, 331, 361, 1, 31, 61, 91, 121,
151, 181, 211, 241, 271, 301, 331, 361), temp = c(8.3, 10.3,
9.4, 6.1, 3, 1.3, 1, 0.8, 1, 1.4, 2.7, 5.1, 8.3, 9, 11.2, 9.6,
5.7, 2, 0.8, 0.6, 0.4, 0.4, 0.6, 1.5, 4.5, 9)), .Names = c("site",
"day", "temp"), row.names = c(NA, -26L), class = "data.frame")



	[[alternative HTML version deleted]]



More information about the R-help mailing list