This must be easy to do..... I have a vector and a lookup data.frame: > v [1] "5" "234" "234" "42-43" "234" "42-43" "234" "234" "42-43" "234" "5" "234" "234" "5" "234" "234" "5" "234" "234" > df id Name 1 5 12-13 2 2 234 3 4 42-43 4 1 5 How can I simply substitute the values in vector v with the corresponding id value from lookup table df? I'd expect the following output: > v [1] "1" "2" "2" "4" "2" "4" "2" "2" "4" "2" "1" "2" "2" "1" "2" "2" "1" "2" "2" Cheers, Nathan -- -------------------------------------------------------- Dr. Nathan S. Watson-Haigh OCE Post Doctoral Fellow CSIRO Livestock Industries University Drive Townsville, QLD 4810 Australia Tel: +61 (0)7 4753 8548 Fax: +61 (0)7 4753 8600 Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html
Nathan -
One way would be
df$id[match(v,df$Name)]
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Wed, 27 Jan 2010, Nathan S. Watson-Haigh wrote:
> This must be easy to do.....
>
> I have a vector and a lookup data.frame:
>
>> v
> [1] "5" "234" "234" "42-43"
"234" "42-43" "234" "234"
> "42-43" "234" "5" "234"
"234" "5" "234" "234"
"5" "234"
> "234"
>> df
> id Name
> 1 5 12-13
> 2 2 234
> 3 4 42-43
> 4 1 5
>
> How can I simply substitute the values in vector v with the corresponding
id
> value from lookup table df? I'd expect the following output:
>> v
> [1] "1" "2" "2" "4"
"2" "4" "2" "2" "4"
"2" "1" "2"
> "2" "1" "2" "2"
"1" "2" "2"
>
> Cheers,
> Nathan
>
> --
> --------------------------------------------------------
> Dr. Nathan S. Watson-Haigh
> OCE Post Doctoral Fellow
> CSIRO Livestock Industries
> University Drive
> Townsville, QLD 4810
> Australia
>
> Tel: +61 (0)7 4753 8548
> Fax: +61 (0)7 4753 8600
> Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html
>
> ______________________________________________
> 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.
>
> (id <- with(df, id[match(v,Name)]))[1] 1 2 2 4 2 4 2 2 4 2 1 2 2 1 2 2 1 2 2 ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Nathan S. Watson-Haigh [nathan.watson-haigh at csiro.au] Sent: 27 January 2010 10:31 To: r-help at r-project.org Subject: [ExternalEmail] [R] Bulk Match/Replace This must be easy to do..... I have a vector and a lookup data.frame: > v [1] "5" "234" "234" "42-43" "234" "42-43" "234" "234" "42-43" "234" "5" "234" "234" "5" "234" "234" "5" "234" "234" > df id Name 1 5 12-13 2 2 234 3 4 42-43 4 1 5 How can I simply substitute the values in vector v with the corresponding id value from lookup table df? I'd expect the following output: > v [1] "1" "2" "2" "4" "2" "4" "2" "2" "4" "2" "1" "2" "2" "1" "2" "2" "1" "2" "2" Cheers, Nathan -- -------------------------------------------------------- Dr. Nathan S. Watson-Haigh OCE Post Doctoral Fellow CSIRO Livestock Industries University Drive Townsville, QLD 4810 Australia Tel: +61 (0)7 4753 8548 Fax: +61 (0)7 4753 8600 Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html ______________________________________________ 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.