[R] Expand duplicated observations
Dear all, I am trying to expand duplicated observations. I need to replace each observation in the dataset with n copies of the observation, where n is equal to the required expression rounded to the nearest integer. If the expression is less than 1 or equal to missing, it is interpreted as if it were 1, and the observation is retained but not duplicated. Example From c(1,2,3) To c(1,2,2,3,3,3) Thank you in advance. Best wishes, Martin __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] Expand duplicated observations
I think this will do what you want x=c(1,2,3) rep(x,x) [1] 1 2 2 3 3 3 Regards Francisco M. P. Papadatos wrote: Dear all, I am trying to expand duplicated observations. I need to replace each observation in the dataset with n copies of the observation, where n is equal to the required expression rounded to the nearest integer. If the expression is less than 1 or equal to missing, it is interpreted as if it were 1, and the observation is retained but not duplicated. Example From c(1,2,3) To c(1,2,2,3,3,3) Thank you in advance. Best wishes, Martin __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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.
Re: [R] Expand duplicated observations
Does this do what you want? dat - c(NA, 0, 3.2, 4) fn - function (x) { z - round(x) if (is.na(x) | x = 1) z else rep(z, each=z) } unlist(sapply(dat, fn)) [1] NA 0 3 3 3 4 4 4 4 HTH, Simon. On Wed, 2007-06-06 at 01:54 +0100, M. P. Papadatos wrote: Dear all, I am trying to expand duplicated observations. I need to replace each observation in the dataset with n copies of the observation, where n is equal to the required expression rounded to the nearest integer. If the expression is less than 1 or equal to missing, it is interpreted as if it were 1, and the observation is retained but not duplicated. Example From c(1,2,3) To c(1,2,2,3,3,3) Thank you in advance. Best wishes, Martin --Apple-Mail-4-920612661-- __ R-help@stat.math.ethz.ch mailing list 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. -- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320, Goddard Building (8) T: +61 7 3365 2506 email: S.Blomberg1_at_uq.edu.au The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey. __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] Expand duplicated observations
Also, to handle NAs and non-integers: x = c(1:3,9.4,NA) tmp = round(x) tmp[is.na(tmp)]=1 rep(x,tmp) [1] 1.0 2.0 2.0 3.0 3.0 3.0 9.4 9.4 9.4 9.4 9.4 9.4 9.4 9.4 9.4 NA On 6/6/07, Francisco J. Zagmutt [EMAIL PROTECTED] wrote: I think this will do what you want x=c(1,2,3) rep(x,x) [1] 1 2 2 3 3 3 Regards Francisco M. P. Papadatos wrote: Dear all, I am trying to expand duplicated observations. I need to replace each observation in the dataset with n copies of the observation, where n is equal to the required expression rounded to the nearest integer. If the expression is less than 1 or equal to missing, it is interpreted as if it were 1, and the observation is retained but not duplicated. Example From c(1,2,3) To c(1,2,2,3,3,3) Thank you in advance. Best wishes, Martin __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] Expand duplicated observations
D'Oh! yet again my first inclination is to write something complicated when a little thought shows a short, neat solution. Ah, well, I live and learn. Cheers, Simon. On Wed, 2007-06-06 at 09:59 +0800, Jared O'Connell wrote: Also, to handle NAs and non-integers: x = c(1:3,9.4,NA) tmp = round(x) tmp[is.na(tmp)]=1 rep(x,tmp) [1] 1.0 2.0 2.0 3.0 3.0 3.0 9.4 9.4 9.4 9.4 9.4 9.4 9.4 9.4 9.4 NA On 6/6/07, Francisco J. Zagmutt [EMAIL PROTECTED] wrote: I think this will do what you want x=c(1,2,3) rep(x,x) [1] 1 2 2 3 3 3 Regards Francisco M. P. Papadatos wrote: Dear all, I am trying to expand duplicated observations. I need to replace each observation in the dataset with n copies of the observation, where n is equal to the required expression rounded to the nearest integer. If the expression is less than 1 or equal to missing, it is interpreted as if it were 1, and the observation is retained but not duplicated. Example From c(1,2,3) To c(1,2,2,3,3,3) Thank you in advance. Best wishes, Martin __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list 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. -- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320, Goddard Building (8) T: +61 7 3365 2506 email: S.Blomberg1_at_uq.edu.au The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey. __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] Expand duplicated observations
oh dear...I forgot about 0! :( On 6/6/07, Jared O'Connell [EMAIL PROTECTED] wrote: Also, to handle NAs and non-integers: x = c(1:3,9.4,NA) tmp = round(x) tmp[is.na(tmp)]=1 rep(x,tmp) [1] 1.0 2.0 2.0 3.0 3.0 3.0 9.4 9.4 9.4 9.4 9.4 9.4 9.4 9.4 9.4 NA On 6/6/07, Francisco J. Zagmutt [EMAIL PROTECTED] wrote: I think this will do what you want x=c(1,2,3) rep(x,x) [1] 1 2 2 3 3 3 Regards Francisco M. P. Papadatos wrote: Dear all, I am trying to expand duplicated observations. I need to replace each observation in the dataset with n copies of the observation, where n is equal to the required expression rounded to the nearest integer. If the expression is less than 1 or equal to missing, it is interpreted as if it were 1, and the observation is retained but not duplicated. Example From c(1,2,3) To c(1,2,2,3,3,3) Thank you in advance. Best wishes, Martin __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list 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.