Re: [R] replace certain elements in data frame

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Sat, 23 Apr 2011 14:16:35 -0700

Hi Yosub,

if "dat" is your data frame, try something like (untested):

dat$code[!dat$code %in% c("8210", "8310", "9190")] <- "others"

the general idea being create a logical vector of codes that match the three you want, use "!" to negate this (so you are getting all codes NOT in the ones you want), and then use this to selectively assign "others".

For documentation, see

?match
?Logic

Hope this helps,

Josh

On Sat, Apr 23, 2011 at 11:31 AM, Yosub Jung <yosubjung_at_berkeley.edu> wrote:
> Hello,
>
> I have a data frame with 2 columns: year and code. In code column, I want to
> replace elements that are not "8210", "8310", or "9190" with "others".
>
> --
> Sincerely,
> Yosub Jung.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat 23 Apr 2011 - 21:19:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 23 Apr 2011 - 22:30:33 GMT.

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

list of date sections of archive