[R] gsub in data frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Apr 5 15:46:15 CEST 2006


On Wed, 5 Apr 2006, Lapointe, Pierre wrote:

> Hello,
>
> I have this data frame:
>
> ### begin
>
> d <-data.frame(matrix(c("1","--","bla",2),2,2))
> d

So d is a two-column data frame with factor columns.

> # I want to replace the "--" by "\N" and still get a data frame.

levels(d$X1) <- gsub("--","\\\\N", levels(d$X1))


> # I tried:
>
> out <-gsub("--","\\\\N",as.matrix(d)) #using as.matrix to get rid of factors
> out
> cat(out)
>
> # But I lost my data frame
>
> ### end
>
> Any idea?
>
> Regards,
>
> Pierre Lapointe
>
> **************************************************
> AVIS DE NON-RESPONSABILITE: Ce document transmis par courrie...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list