[R] combining data from different datasets

Gabor Grothendieck ggrothendieck at gmail.com
Fri Oct 24 17:16:46 CEST 2008


Here are two solutions.  The first uses the R merge command
and the second uses the R sqldf package.  See ?merge
and http://sqldf.googlecode.com
Note that alter is an sql keyword so I have changed it
to alt for the second example:

> merge(iso, rawdata)[c("alt", "sex", "country")]
  alter sex country
1    NA   M Andorra
2    39   F Namibia
> library(sqldf)

> sqldf("select alt, sex, country from iso join rawdata using(country)")
  alt sex country
1  NA   M Andorra
2  39   F Namibia


On Fri, Oct 24, 2008 at 11:01 AM, Dr Eberhard W Lisse <el at lisse.na> wrote:
> Hi,
>
> I have two tables:
>
>
>> iso
>    continent code code3 codenum                          country
> 1          EU   AD   AND      20         Andorra, Principality of
> 2          AS   AE   ARE     784             United Arab Emirates
> 3          AS   AF   AFG       4 Afghanistan, Islamic Republic of
> 4          NA   AG   ATG      28              Antigua and Barbuda
> 5          NA   AI   AIA     660                         Anguilla
> 159        AF   NA   NAM     516             Namibia, Republic of
> ...
> 246        AF   ZW   ZWE     716            Zimbabwe, Republic of
>
>
>
> and
>
>> rawdata
>   idno alter  sex cctld capacity topics content speakers
> 1     1    NA    M    AD        A      2       1        2
> 2     2    52    M  <NA>        A      3       2        2
> 3     3    39    F    NA        T      2       2        2
> ...
>
> How do I make it print out something like
>
>        alter   sex     country
> 1       NA      M       Andorra, Principality of
> 2       39      F       Namibia, Republic of
> ...
>
> in other words do something like the following SQL:
>
> SELECT r.alter, r.sex, i.country
> FROM iso i, rawdata r
> WHERE i.code = r.cctld
>
> or even better:
>
>        alter   sex     country
> 1       NA      M       Andorra, Principality of
> 2       52      M       <NA>
> 3       39      F       Namibia
> ...
>
> And idea (where I can read up on this)?
>
>
> el
> --
> Dr. Eberhard W. Lisse  \        / Obstetrician & Gynaecologist (Saar)
> el at lisse.NA el108-ARIN /   *   |   Telephone: +264 81 124 6733 (cell)
> PO Box 8421             \     / Please send DNS/NA-NiC related e-mail
> Bachbrecht, Namibia     ;____/             to dns-admin at na-nic.com.na
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list