An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120827/e64d4c2c/attachment.pl>
find and replace
4 messages · Sapana Lohani, michael.weylandt at gmail.com (R. Michael Weylandt, arun
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:
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.