This should do it: > x <- read.table(textConnection(" Id Father Mother + 123 0 0 + 124 0 0 + 125 123 124 + 126 123 0 + 127 125 126 + 128 0 127 + 130 123 125"), header=TRUE) > # convert to matrix > x <- as.matrix(x) > closeAllConnections() > # map numbers to a new sequence > allNums <- sort(unique(as.vector(x))) > # new numbers > newNums <- seq(0, by=1, length=length(allNums)) > # now remap the numbers > x[] <- newNums[match(x, allNums)] > x <- cbind(x, orig=allNums[x[, "Id"] + 1]) > > x Id Father Mother orig [1,] 1 0 0 123 [2,] 2 0 0 124 [3,] 3 1 2 125 [4,] 4 1 0 126 [5,] 5 3 4 127 [6,] 6 0 5 128 [7,] 7 1 3 130 >
On Sun, Mar 28, 2010 at 6:33 PM, Val <valkr...@gmail.com> wrote: > Thank you for your quick response. You are right the ls two rows were not > correct. Typ error. > > Is it possible to add the original ID number as a cross reference as well > Like, > Id Father Mother Original ID > [1,] 1 0 0 123 > [2,] 2 0 0 124 > [3,] 3 1 2 125 > [4,] 4 1 0 126 > [5,] 5 3 4 127 > [6,] 6 0 5 128 > [7,] 7 1 3 130 > > Thanks again > Val > > > > > > On Sun, Mar 28, 2010 at 5:09 PM, jim holtman <jholt...@gmail.com> wrote: > >> This is not really a "renumbering" and there is no builtin function. This >> is a case where you want to do some special remapping of numbers and you >> have to decide what it is in your case. Also use 'dput' to put data in >> email since what you sent was not parseable. Also I think the last two >> lines of your desired output is incorrect. >> >> Try this: >> >> > x <- read.table(textConnection(" Id Father Mother >> >> + 123 0 0 >> + 124 0 0 >> + 125 123 124 >> + 126 123 0 >> + 127 125 126 >> + 128 0 127 >> + 130 123 125"), header=TRUE) >> > # convert to matrix >> > x <- as.matrix(x) >> > closeAllConnections() >> > # map numbers to a new sequence >> > allNums <- sort(unique(as.vector(x))) >> > # new numbers >> > newNums <- seq(0, by=1, length=length(allNums)) >> > # now remap the numbers >> > x[] <- newNums[match(x, allNums)] >> > >> > x >> Id Father Mother >> [1,] 1 0 0 >> [2,] 2 0 0 >> [3,] 3 1 2 >> [4,] 4 1 0 >> [5,] 5 3 4 >> [6,] 6 0 5 >> [7,] 7 1 3 >> > >> >> >> On Sun, Mar 28, 2010 at 4:50 PM, Val <valkr...@gmail.com> wrote: >> >>> HI all, >>> Is there a renumbering function in R? >>> >>> I would like to renumber the following id numbers >>> >>> *Input file >>> * >>> Id Father Mother 123 0 0 124 0 0 125 123 124 126 123 0 127 125 126 >>> 128 0 127 130 123 125 >>> *Output* >>> >>> Id Father Mother 1 0 0 2 0 0 3 1 2 4 1 0 5 3 4 6 0 7 7 1 2 >>> Any help is highly appreciated in Advance >>> >>> Val K >>> >>> [[alternative HTML version deleted]] >>> >>> >>> ______________________________________________ >>> R-help@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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 that you are trying to solve? >> > > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org 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.