Skip to content
Back to formatted view

Raw Message

Message-ID: <200803031427.22175.elff@sowi.uni-mannheim.de>
Date: 2008-03-03T13:27:22Z
From: Martin Elff
Subject: Replace a list of values in data frame
In-Reply-To: <33312.9201.qm@web54607.mail.re2.yahoo.com>

On Friday 29 February 2008 (14:50:53), Silvia Lipski wrote:
> Dear R-users,
>
> I am sorry if I ask for something that has been asked
> before, however, I still could not solve my little
> problem by consulting the previous thread on this
> topic:
>
> I would like to replace several values in a data
> frame, such as in:
>
> colorful
> ? subject response
> 1 ? ? ?me ? ?black ?
> 2 ? ? ?me ? ?brown ?
> 3 ? ? you ? ? ?red ?
> 4 ? ? ?me ? ?black ?
> 5 ? ? you ? ?brown ?
>
> read in with read.table()
>
> I would like to replace both "black" and "brown" by
> "dark".

What about:

colorful <- within(colorful,
		test1 <- replace(response,
			c("black","brown"),
			"dark"
			)
		)

or

colorful <- within(colorful,
		test1 <- response
		test1[test1 %in% c("black","brown")] <- "dark"
		)