[R] find and replace
arun
smartpink111 at yahoo.com
Tue Aug 28 19:34:47 CEST 2012
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.
More information about the R-help
mailing list