[R] Changing a value in a particular row and column within a text file

Petr Savicky savicky at cs.cas.cz
Fri Dec 17 11:18:37 CET 2010


On Fri, Dec 17, 2010 at 01:49:23AM -0800, T.V. Nguyen wrote:
> On 17 Dec 2010, at 01:13, Mauricio Zambrano wrote:
[...]
> > but some text, and I now exactly the row that has to be modified and
> > the columns within that row that have to be changed with a new
> > numerical value.
[...]
> You can either edit the line you want manually, or use sub to make changes:
> 
> > sub("Lords-a-Leaping","Jaffa Cakes",list)
> > head(list)
> [1] "12 Drummers Drumming" "11 Pipers Piping"    
> [3] "10 Jaffa Cakes"       "9 Ladies Dancing"    
> [5] "8 Maids-a-Milking"    "7 Swans-a-Swimming"  

Additionally, if you know the exact character positions, which have
to be changed, then substr() can be used.

  x <- "123456789"
  substr(x, 5, 7) <- "abc"
  x # [1] "1234abc89"

For an exact replacement, the length of the new text should be
the same as of the original field.

Petr Savicky.



More information about the R-help mailing list