[R] Apply gsub to dataframe to modify row values

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Mon Aug 9 12:40:17 CEST 2021


Thank you, that is much appreciated. But on the real data, the
substitution works only on few instances. Is there a way to introduce
regex into this?
Cheers
Luigi

On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon <drjimlemon using gmail.com> wrote:
>
> Hi Luigi,
> Ah, now I see:
>
>  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> df
>  VAR   VAL
> 1   1  blue
> 2   2   red
> 3   3 empty
>
> Jim
>
> On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
> >
> > Hello,
> > I have a dataframe where I would like to change the string of certain
> > rows, essentially I am looking to remove some useless text from the
> > variables.
> > I tried with:
> > ```
> > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", "empty"))
> > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl = FALSE)
> > > df
> >   VAR           VAL value is blue Value is red empty
> > 1   1 value is blue          blue         blue  blue
> > 2   2  Value is red           red          red   red
> > 3   3         empty         empty        empty empty
> > ```
> > which is of course wrong because I was expecting
> > ```
> >   VAR           VAL
> > 1   1             blue
> > 2   2             red
> > 3   3            empty
> > ```
> > What is the correct syntax in these cases?
> > Thank you
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.



-- 
Best regards,
Luigi



More information about the R-help mailing list