Re: [R] replace values in data frame

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Sat 08 Jul 2006 - 01:40:28 EST

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

Without seeing the code you are using, we can only guess a syntax error of some sort. It works fine using the iris dataset:

> head(iris)

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa


> iris$Species <- replace(iris$Species, iris$Species == "setosa",
"NewValue")

> head(iris)

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1           5.1         3.5          1.4         0.2 NewValue
2           4.9         3.0          1.4         0.2 NewValue
3           4.7         3.2          1.3         0.2 NewValue
4           4.6         3.1          1.5         0.2 NewValue
5           5.0         3.6          1.4         0.2 NewValue
6           5.4         3.9          1.7         0.4 NewValue
7           4.6         3.4          1.4         0.3 NewValue
8           5.0         3.4          1.5         0.2 NewValue
9           4.4         2.9          1.4         0.2 NewValue
10          4.9         3.1          1.5         0.1 NewValue


HTH, Marc Schwartz



R-help@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 Received on Sat Jul 08 01:55:15 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 08 Jul 2006 - 06:15:32 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.