Wonderful!
Thank you Arun!
Irene
Irene Ruberto
________________________________
Da: arun kirshna [via R] <ml-node+s789695n4677558h52@n4.nabble.com>
Inviato: Giovedì 3 Ottobre 2013 22:51
Oggetto: Re: String substitution
Hi,
Try:
dat$y<- as.character(dat$y)
dat1<- dat
dat2<- dat
library(stringr)
dat$y[NET]<- substr(word(dat$y[NET],2),1,1)
dat$y
#[1] "n" "n" "house" "n"
"tree"
#or
for(i in 1:length(NET)){dat1$y[NET[i]]<- "n"}
dat1$y
#[1] "n" "n" "house" "n"
"tree"
#or
dat2$y[NET]<- gsub(".*(n).*","\\1",dat2$y[NET])
dat2$y
#[1] "n" "n" "house" "n"
"tree"
A.K.
Hello,
I am trying to replace strings containing a certain word, I
first identified the word (in this example "net") with grep, and then
I
need to replace those string with "n". It should be very simple but I
don't seem to find the solution.
Example:
x<-c(5:9)
y<- c("with net", "with nets", "house",
"no nets", "tree")
dat<-as.data.frame(cbind(x, y) )
NET<-grep("net", dat$y)
# I want y to become ("n", "n", "house",
"n", "tree") #
# I have tried several ways including the following but without success #
for (i in 1: length(NET)) {
dat$y[NET[i]]<- "n" }
Thank you for your help!
______________________________________________
[hidden email] 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.
________________________________
If you reply to this email, your message will be added to the discussion
below:http://r.789695.n4.nabble.com/String-substitution-tp4677541p4677558.html
To unsubscribe from String substitution, click here.
NAML
--
View this message in context:
http://r.789695.n4.nabble.com/String-substitution-tp4677541p4677612.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]