[R] How to copy and paste a row at the end of each group of a table?
jim holtman
jholtman at gmail.com
Mon Oct 31 14:09:53 CET 2016
try this:
> 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")
>
> # split the data, copy value of first row to end
> dat2 <- do.call(rbind, lapply(split(dat, dat$site), function(.site){
+ x <- rbind(.site, .site[1L, ]) # add first row to bottom
+ x$day[nrow(x)] <- 361
+ x # return x
+ }))
> dat2
site day temp
1.1 1 1 8.3
1.2 1 31 10.3
1.3 1 61 9.4
1.4 1 91 6.1
1.5 1 121 3.0
1.6 1 151 1.3
1.7 1 181 1.0
1.8 1 211 0.8
1.9 1 241 1.0
1.10 1 271 1.4
1.11 1 301 2.7
1.12 1 331 5.1
1.13 1 361 8.3
2.13 2 1 9.0
2.14 2 31 11.2
2.15 2 61 9.6
2.16 2 91 5.7
2.17 2 121 2.0
2.18 2 151 0.8
2.19 2 181 0.6
2.20 2 211 0.4
2.21 2 241 0.4
2.22 2 271 0.6
2.23 2 301 1.5
2.24 2 331 4.5
2.131 2 361 9.0
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Mon, Oct 31, 2016 at 8:59 AM, Kristi Glover
<kristi.glover at hotmail.com> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list