-1

I searched for other topics, but didn't find anything which was really matching what I was looking for.

My dataframes:

UN_match excerpt:

country country_code   emissions sector_code     year
Austria           AT  65779.1172        1.AA     2005
Austria           AT  62430.4336        1.AA     2006
Austria           AT  59108.4180        1.AA     2007
Austria           AT  58656.6719        1.AA     2008
Austria           AT  55252.9922        1.AA     2009
Austria           AT  58317.9570        1.AA     2010
Austria           AT  55898.7344        1.AA     2011
Austria           AT  53886.8242        1.AA     2012
Austria           AT  53923.7578        1.AA     2013
Austria           AT  50087.0000        1.AA     2014
Austria           AT  51978.9609        1.AA     2015
Austria           AT  52990.2305        1.AA     2016
Belgium           BE 103917.1484        1.AA     2005
Belgium           BE 102263.9297        1.AA     2006
Belgium           BE 100104.8906        1.AA     2007
Belgium           BE  99960.6328        1.AA     2008
Belgium           BE  92900.2188        1.AA     2009
Belgium           BE  96538.8047        1.AA     2010
Belgium           BE  87202.2188        1.AA     2011
Belgium           BE  86242.7656        1.AA     2012
Belgium           BE  86289.1562        1.AA     2013
Belgium           BE  80720.1406        1.AA     2014
Belgium           BE  84283.8438        1.AA     2015
Belgium           BE  84081.2031        1.AA     2016

ETS_match

     country country_code     value year smaller1.AA
     Austria           AT  16539.659 2005           0
     Austria           AT  15275.065 2006           0
     Austria           AT  14124.646 2007           0
     Austria           AT  14572.511 2008           0
     Austria           AT  12767.555 2009           0
     Austria           AT  15506.112 2010           0
     Austria           AT  15131.551 2011           0
     Austria           AT  13121.434 2012           0
     Austria           AT   8074.514 2013           0
     Austria           AT   6426.135 2014           0
     Austria           AT   7514.263 2015           0
     Austria           AT   7142.937 2016           0
     Belgium           BE  25460.856 2005           0
     Belgium           BE  24099.282 2006           0
     Belgium           BE  23706.084 2007           0
     Belgium           BE  23166.180 2008           0
     Belgium           BE  21185.552 2009           0
     Belgium           BE  22073.616 2010           0
     Belgium           BE  18950.876 2011           0
     Belgium           BE  17463.388 2012           0
     Belgium           BE  16728.267 2013           0
     Belgium           BE  15230.243 2014           0
     Belgium           BE  16053.800 2015           0
     Belgium           BE  15027.777 2016           0

I want to add TRUE or FALSE into ETS_match$smaller1.AA based on whether ETS_match$value < UN_match$emissions is true or false. This should be done for every row.

I tried some things myself with mutate and if_else, but didn't manage to finish it.

It should look like this:

     country country_code     value year smaller1.AA
     Austria           AT  16539.659 2005       TRUE
     Austria           AT  15275.065 2006       TRUE
     Austria           AT  14124.646 2007       TRUE

I know this is probably very basic, but I would be happy about any kind of help.

Best wishes,

nordsee

Nordsee
  • 81
  • 1
  • 10

3 Answers3

4

Something like this, with dplyr, maybe:

library(dplyr)
UN_match %>% left_join(ETS_match) %>%                                 # join the data
  mutate(smaller1.AA =  value < emissions, TRUE, FALSE) %>%           # add the true false
  select(country, country_code, value, year, smaller1.AA)             # only useful columns

But all your value are < of emission, so the data are rows are all TRUE in this case.

Improved removing if_else, thanks @Rui Barradas.

s__
  • 9,270
  • 3
  • 27
  • 45
2

If the datasets have the same number of rows and all the rows align correctly, you can just do:

ETS_match$smaller1.AA <- ETS_match$value < UN_match$emissions
Cleland
  • 349
  • 1
  • 6
  • Yes, they are of the same length. And this little code did exactly what I needed. Thank you very much! – Nordsee Nov 22 '18 at 12:09
1

Because the data you're working with are from two different data frames, you have either two options:

  1. Compare them independently
  2. Combine the data into one data frame.

For 1, you can do something like

ETS_match$smaller1.AA <- ETS_match$value < UN_match$emissions

For 2, you could use merge to bring the data frames together. How to join (merge) data frames (inner, outer, left, right)?