I have a text file full of numbers (it's a edgelist for a graph) and I would like to recode the numbers as they are way too big to work with. So for instance the following: 676529098667 1000198767829 676529098667 100867672856227 676529098667 91098726278 676529098667 98928373 1092837363526 716172829 would become: 0 1 0 2 0 3 0 4 5 6 i.e. all 676529098667 would become 0, all 1000198767829 would become 1 etc. If I read all the values into a matrix, is there a pre-existing function that can do the recoding? Thank you! Thomas ChesneyThis message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
On Sep 1, 2011, at 10:54 AM, Thomas Chesney wrote:> I have a text file full of numbers (it's a edgelist for a graph) and > I would like to recode the numbers as they are way too big to work > with. So for instance the following: > > 676529098667 1000198767829 > 676529098667 100867672856227 > 676529098667 91098726278 > 676529098667 98928373 > 1092837363526 716172829 > > would become: > > 0 1 > 0 2 > 0 3 > 0 4 > 5 6 > > i.e. all 676529098667 would become 0, all 1000198767829 would become > 1 etc.Depending on how that set of numbers was entered see if this is helpful: 1) First entering across first then down. x <- c(676529098667 , 1000198767829, 676529098667 , 100867672856227, 676529098667 , 91098726278, 676529098667 , 98928373, 1092837363526 ,716172829) as.numeric(factor(x, levels=unique(x)) ) # [1] 1 2 1 3 1 4 1 5 6 7 2( Now entering first down then over. x2 <- matrix(x, ncol=2, byrow=TRUE) # Matrices are column first ordered. as.numeric(factor(x2, levels=unique(c(x2))) ) # need c() to avoid warning. # [1] 1 1 1 1 2 3 4 5 6 7> If I read all the values into a matrix, is there a pre-existing > function that can do the recoding?You can just subtract one from the factor results. The trick is to use explicit levels determined to match the sort order you want. Other wise the levels would be first collated. -- David Winsemius, MD West Hartford, CT
Thank you both for your replies. I've tried it with a small sample of the data and it works perfectly. I have no idea yet how it works but I will spend some time to figure it out. Thank you! Thomas -- View this message in context: http://r.789695.n4.nabble.com/Automatic-Recoding-tp3784043p3785565.html Sent from the R help mailing list archive at Nabble.com.