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]]
Hi, My data frame (Students) is ID Name Fav_Place 101 Andrew Phoenix AZ 201 John San Francisco 303 JulieCalifornia / New York 304 Monica New York How can I replace Phoenix with Tucson & New York with New York City in the df? Thanks [[alternative HTML version deleted]]
I am making the assumption that all the columns are character and not factors:
for (i in c("A", "B", "C", "D",
"E")){
yourdf[[i]] <- ifelse(yourdf[[i]] == 'x'
, 'y'
, ifelse(yourdf[[i]] == 'a'
, 'b'
, yourdf[[i]]
)
)
}
On Mon, Aug 27, 2012 at 4:19 PM, Sapana Lohani <lohani.sapana at
ymail.com> wrote:> 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.
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Aug-28 03:05 UTC
[R] find and replace
Take a look at gsub() Michael On Aug 27, 2012, at 6:47 PM, Sapana Lohani <lohani.sapana at ymail.com> wrote:> Hi, > > My data frame (Students) is > > ID Name Fav_Place > 101 Andrew? Phoenix AZ > 201 John San Francisco > 303 JulieCalifornia / New York > 304 Monica? New York > > How can I replace Phoenix with Tucson & New York with New York City in the df? > > 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.
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.
Hi,
Try this:
dat1<-readLines(textConnection(
?"ID Name Fav_Place
?101 Andrew? Phoenix,AZ
?201 John SanFrancisco
?303 Julie California/New York
?304 Monica? New York"))
?gsub("Phoenix","Tucson",gsub("New York","New
York City",dat1))
#[1] "ID Name Fav_Place"????????????????? "101 Andrew?
Tucson,AZ"????????????
#[3] "201 John SanFrancisco"????????????? "303 Julie
California/New York City"
#[5] "304 Monica? New York City"
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 7:47 PM
Subject: [R] find and replace
Hi,
My data frame (Students) is
ID Name Fav_Place
101 Andrew? Phoenix AZ
201 John San Francisco
303 JulieCalifornia / New York
304 Monica? New York
How can I replace Phoenix with Tucson & New York with New York City in the
df?
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.
HI,
You can also try this:
Students1<-data.frame(ID=c(101,201,303,304),Name=c("Andrew","John","Julie","Monica"),Fav_Place=c("Phoenix
AZ","San Francisco","California/New York","New
York"))
?
gsubfun<-function(pattern,replacement,x, ...){
?for(i in seq_along(pattern))
?x<-gsub(pattern[i],replacement[i],x,...)
?x
?}
toreplace<-c("Phoenix","New York")
replacedwith<-c("Tucson","New York City")
Students1$Fav_Place<-gsubfun(toreplace,replacedwith,Students1$Fav_Place)
Students1
#?? ID?? Name??????????????? Fav_Place
#1 101 Andrew??????????????? Tucson AZ
#2 201?? John??????????? San Francisco
#3 303? Julie California/New York City
#4 304 Monica??????????? New York City
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 7:47 PM
Subject: [R] find and replace
Hi,
My data frame (Students) is
ID Name Fav_Place
101 Andrew? Phoenix AZ
201 John San Francisco
303 JulieCalifornia / New York
304 Monica? New York
How can I replace Phoenix with Tucson & New York with New York City in the
df?
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.