Hi there, Wonder if someone who is R-savvy can help me with the following task (see below) that I occasionally do work and gets quite tedious if I do it manually. Thanks in advance! jenny. ----------------- I have a column of data that looks like this: NA18501 NA18502 NA18504 NA18505 NA18507 NA18508 NA18516 NA18517 NA18522 NA18523 And I want to duplicate the values and sort of "interweave" them to look like this: NA18501 NA18501 NA18502 NA18502 NA18504 NA18504 NA18505 NA18505 NA18507 NA18507 NA18508 NA18508 NA18516 NA18516 NA18517 NA18517 NA18522 NA18522 NA18523 NA18523
If x is your data, and is a vector, then rep(x, each=2) should do it for you. Cheers, Simon. jenny tan wrote:> Hi there, > > Wonder if someone who is R-savvy can help me with the following task (see > below) that I occasionally do work and gets quite tedious if I do it > manually. Thanks in advance! > > jenny. > > ----------------- > > I have a column of data that looks like this: > NA18501 > NA18502 > NA18504 > NA18505 > NA18507 > NA18508 > NA18516 > NA18517 > NA18522 > NA18523 > > And I want to duplicate the values and sort of "interweave" them to look > like this: > > NA18501 > NA18501 > NA18502 > NA18502 > NA18504 > NA18504 > NA18505 > NA18505 > NA18507 > NA18507 > NA18508 > NA18508 > NA18516 > NA18516 > NA18517 > NA18517 > NA18522 > NA18522 > NA18523 > NA18523 > > ______________________________________________ > R-help at 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, B.Sc.(Hons.), Ph.D, M.App.Stat. Centre for Resource and Environmental Studies The Australian National University Canberra ACT 0200 Australia T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au F: +61 2 6125 0757 CRICOS Provider # 00120C
Here is how to do it with vectors; you can extend to a data frame:> x <- "NA18501+ NA18502 + NA18504 + NA18505 + NA18507 + NA18508 + NA18516 + NA18517 + NA18522 + NA18523 + "> x <- scan(textConnection(x), what='')Read 10 items> x[1] "NA18501" "NA18502" "NA18504" "NA18505" "NA18507" "NA18508" "NA18516" "NA18517" "NA18522" "NA18523"> newDF <- x[rep(seq(length(x)), each=2)] > newDF[1] "NA18501" "NA18501" "NA18502" "NA18502" "NA18504" "NA18504" "NA18505" "NA18505" "NA18507" "NA18507" [11] "NA18508" "NA18508" "NA18516" "NA18516" "NA18517" "NA18517" "NA18522" "NA18522" "NA18523" "NA18523">On 8/1/06, jenny tan <jennytimp@hotmail.com> wrote:> > Hi there, > > Wonder if someone who is R-savvy can help me with the following task (see > below) that I occasionally do work and gets quite tedious if I do it > manually. Thanks in advance! > > jenny. > > ----------------- > > I have a column of data that looks like this: > NA18501 > NA18502 > NA18504 > NA18505 > NA18507 > NA18508 > NA18516 > NA18517 > NA18522 > NA18523 > > And I want to duplicate the values and sort of "interweave" them to look > like this: > > NA18501 > NA18501 > NA18502 > NA18502 > NA18504 > NA18504 > NA18505 > NA18505 > NA18507 > NA18507 > NA18508 > NA18508 > NA18516 > NA18516 > NA18517 > NA18517 > NA18522 > NA18522 > NA18523 > NA18523 > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]