0

My R knowledge is pretty limited, but I have to an analysis for a project which is due in a few days and was hoping I could get some quick help around here!

I created this dataset https://1drv.ms/x/s!ArVyXA5cSMj2h7Mf07SZaVUSK3421Q?e=GQBfeU but only want to use the data for the year 2016.

I would either like to create a new data frame containing only those rows which include year = 2016 or do my linear regression with the original data frame, but only using the 2016 data - either way will work fine!

Tried googling this, but I wasn't sure what to search for...

  • 1
    What are you looking for? An effective way to load only `2016` related data or trying to figure out how to filter `2016` data from your dataframe? – Jason Mathews Aug 04 '21 at 18:52

2 Answers2

2

To create a new dataframe with year of 2016 we could use filter to filter your dataframe df:

new_dataframe <- filter(df, year==2016)
TarJae
  • 72,363
  • 6
  • 19
  • 66
0
library(readxl)
co2_open_cvs <- read_excel("path_to_file/co2_open_cvs.xlsx")

library(dplyr)
co2_open_cvs_only_2016 <- co2_open_cvs %>% filter(year == 2016)
dy_by
  • 1,061
  • 1
  • 4
  • 13
  • 1
    Thanks, this worked perfectly! Just wondering: what does the "%>%" part do? – Jakob Meyer Aug 04 '21 at 20:20
  • 1
    *Pipe operator* from `magrittr` (part of `tidyverse`). Pipe an object forward into a function or call expression. So `x %>% f` is equivalent to `f(x)`, for example: instead of `head(df)` you could use `df %>% head`. really really useful. for more read https://magrittr.tidyverse.org/ – dy_by Aug 04 '21 at 20:34
  • @dy_by Please add your elaboration to the answer. – user845279 Aug 04 '21 at 22:01