[R] String substitution
arun
smartpink111 at yahoo.com
Thu Oct 3 22:49:56 CEST 2013
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!
More information about the R-help
mailing list