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

PIKAL Petr petr.pikal at precheza.cz
Mon Oct 31 14:42:07 CET 2016


Hi

Another approach is to use function like following

fff<-function(x, ...) {
temp <- x[which(x[,"day"]==1),]
temp$day <- 361
x <- rbind(x, temp)
x[order(x$site, x$day),]
}

fff(dat)

Cheers
Petr

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of jim
> holtman
> Sent: Monday, October 31, 2016 2:10 PM
> To: Kristi Glover <kristi.glover at hotmail.com>
> Cc: r-help at r-project.org
> Subject: Re: [R] How to copy and paste a row at the end of each group of a
> table?
>
> 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.
>
> ______________________________________________
> 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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list