Hi All, I have data set like x = data.frame(id = 1:8, f1 = LETTERS[1:8]) I want to replace id 5, 6, 7, 8 by 1, 2, 3, 4 respectively. e.g the map is ID-1 ID-2 5 1 6 2 7 3 8 4 I have lots of data and rules. How to do it in a few lines rather than do them one by one like: x[x$id == 5,]$id = 1 x[x$id == 6,]$id = 2 etc. Thanks. HXD [[alternative HTML version deleted]]
Hi, library(car) set.seed(25) ?x1<- data.frame(id=sample(1:8,8,replace=FALSE),f1=LETTERS[1:8]) ?x1$id<-recode(x1$id,'5=1;6=2;7=3;8=4') x1 #? id f1 #1? 4? A #2? 1? B #3? 1? C #4? 3? D #5? 2? E #6? 3? F #7? 2? G #8? 4? H A.K. ----- Original Message ----- From: Hui Du <Hui.Du at dataventures.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Monday, May 6, 2013 5:29 PM Subject: [R] replace data by a rule Hi All, I have data set like x = data.frame(id = 1:8, f1 = LETTERS[1:8]) I want to replace id 5, 6, 7, 8 by 1, 2, 3, 4 respectively. e.g the map is ID-1 ID-2 5 1 6 2 7 3 8 4 I have lots of data and rules. How to do it in a few lines rather than do them one by one like: x[x$id == 5,]$id = 1 x[x$id == 6,]$id = 2 etc. Thanks. HXD ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Hello, Like this? x = data.frame(id = 1:8, f1 = LETTERS[1:8]) idx <- 5 <= x$id & x$id <= 8 x$id[idx] <- x$id[idx] - 4 x Hope this helps, Rui Barradas Em 06-05-2013 22:29, Hui Du escreveu:> Hi All, > > I have data set like > x = data.frame(id = 1:8, f1 = LETTERS[1:8]) > > I want to replace id 5, 6, 7, 8 by 1, 2, 3, 4 respectively. e.g the map is > > ID-1 > > ID-2 > > 5 > > 1 > > 6 > > 2 > > 7 > > 3 > > 8 > > 4 > > > > I have lots of data and rules. How to do it in a few lines rather than do them one by one like: > > x[x$id == 5,]$id = 1 > x[x$id == 6,]$id = 2 > > etc. > > Thanks. > > HXD > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >