[R] replace values in data frame
Marc Schwartz (via MN)
mschwartz at mn.rr.com
Fri Jul 7 17:40:28 CEST 2006
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
More information about the R-help
mailing list