[R] replace certain elements in data frame

Joshua Wiley jwiley.psych at gmail.com
Sat Apr 23 23:16:35 CEST 2011


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/



More information about the R-help mailing list