[R] replace values in data frame

Marc Schwartz (via MN) mschwartz at mn.rr.com
Fri Jul 7 19:18:27 CEST 2006


Thanks for re-posting onlist.  My offlist reply:

The 'list' argument, as per ?replace, needs to be a vector of one or
more indices into another vector, generally the source vector argument
'x', not the actual values that you want to replace. 'list' can either
be explicit integers as the indices, or the result of a logical
operation as I used in my example.

In addition, the objects 'list' and 'replace' in your code are not
vectors, but they are data frames. That is the default
type of object created when using read.table().

You can use str(list) and str(replace) to check this. The str() function
returns the structure of an object.

It is likely that you really want list$V1 and replace$V1 as the vectors
of interest here. V1 thru Vx are the default column names created when
using read.table() if no header row exists in the incoming file.

In addition, note that replace() is not vectorized. It will not handle
multiple search and replace arguments in a single call. See my example
using the iris dataset.

You would need to do each one separately. You can create a loop of one
type or another to cycle through multiple arguments if you wish, where
each cycle through the loop is a single call to replace() with a new set
of values as appropriate for the arguments.

Finally, you should generally not use R object or function names for
user created objects. Both 'list' and 'replace' are such objects. R will
generally be able to differentiate, but there is no guarantee and it
will help you avoid code debugging headaches.

HTH,

Marc Schwartz



On Fri, 2006-07-07 at 12:58 -0400, Wade Wall wrote:
> The format is like this.
> 
> Plot  species   Value
> 
> P1  ACERRUB   3
> P2  MAGNVIR    2
> P3  ARONARB   2
> etc.
> 
> imported using x<-read.table(file="filename.txt")
> 
> I want to replace a list of values in the 2nd column with another list. For
> example, I want to replace ARONARB with PHOTPYR.
> list<-read.table(file="originalnames.txt")
> replace<-read.table(file="replacementnames.txt")
> I tried to use replace in this manner:
> 
> newx<-replace(x,list,replace)
> however, I get the error message: error in replace, invalid subscript type.
> 
> I have tried transforming the above list and modifying the column names
> (column 2), but to no avail.  I hope this clarifies a little.  Sorry about
> that.

> 
> >
> > On Fri, 2006-07-07 at 11:20 -0400, Wade Wall wrote:
> > > Hi all,
> > >
> > > I have a three columned list that I have imported into R.  The first
> > column
> > > is a plot (ex. Plot1), the second is a species name (ex ACERRUB) and the
> > > third a numeric value.  I want to replace some of the second column
> > names
> > > with other names (for example replace ACERRUB with ACERDRU).  The
> > original
> > > and replacement values are in separate lists (not vectors), but I can't
> > seem
> > > to find the right function to perform this.  The replace function seems
> > to
> > > only want to work with numbers.
> > >
> > > Any clues?
> > >
> > > Wade
> >



More information about the R-help mailing list