Skip to content
Back to formatted view

Raw Message

Message-ID: <1346159795.19163.YahooMailNeo@web142604.mail.bf1.yahoo.com>
Date: 2012-08-28T13:16:35Z
From: arun
Subject: find and replace
In-Reply-To: <1346098792.55894.YahooMailNeo@web125203.mail.ne1.yahoo.com>

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.