[R] find and replace

arun smartpink111 at yahoo.com
Tue Aug 28 15:16:35 CEST 2012


Hi,
Try this:
set.seed(1)
 dat1<-data.frame(A=sample(letters[20:25],replace=TRUE),B=sample(letters[1:6],replace=TRUE),C=c(letters[1:3],letters[3:1]),D=sample(letters[2:7],replace=TRUE),E=sample(letters[21:26],replace=TRUE))
 newdat<-list()
 for(i in 1:ncol(dat1)){
 newdat[[i]]<-list()
 newdat[[i]]<-gsub("x","y",gsub("a","b",dat1[,i]))}
newdat1<-data.frame(do.call(cbind,newdat))
 newdat1
#  X1 X2 X3 X4 X5
#1  u  f  b  f  w
#2  v  d  b  d  y
#3  w  d  c  f  z
#4  y  b  c  d  v
#5  u  b  b  f  y
#6  y  b  b  g  u
A.K.




----- Original Message -----
From: Sapana Lohani <lohani.sapana at ymail.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Monday, August 27, 2012 4:19 PM
Subject: [R] find and replace

I have 5 (A,B,C,D,E) columns in my dataframe. I want to replace all "x" with "y" and all "a" with "b" within these 5 columns. Can I do it in one step?

Thanks
    [[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.





More information about the R-help mailing list