Good morning,
I'm working with a cvs file, where among the different columns I have "Year". For my analysis, I need to select only the year 2015. Is there a way to do it?
import the csv to R, then filter it.
You should particularly study chapter 5
Thanks for your reply. I was wondering, the code here is: filter(flights, month == 1, day == 1). In my case, I should use the name of my data set, but then I cannot find a way to select just the year 2015. I have no month or days, I just have Country, Year, GDP and so on. If I read further, I see that to filter by year I could use: ```
filter(FB, date >= as.Date("2013-01-01"), date <= as.Date("2013-12-31"))
The problem, again, is date I don't have months or days to insert
In the same way that you used month == 1
or day == 1
, you can filter for year == 2015
. If this column is just an integer then you don't need to worry about creating a date value to filter with. For example:
library(tidyverse)
library(gapminder)
head(gapminder)
#> # A tibble: 6 x 6
#> country continent year lifeExp pop gdpPercap
#> <fct> <fct> <int> <dbl> <int> <dbl>
#> 1 Afghanistan Asia 1952 28.8 8425333 779.
#> 2 Afghanistan Asia 1957 30.3 9240934 821.
#> 3 Afghanistan Asia 1962 32.0 10267083 853.
#> 4 Afghanistan Asia 1967 34.0 11537966 836.
#> 5 Afghanistan Asia 1972 36.1 13079460 740.
#> 6 Afghanistan Asia 1977 38.4 14880372 786.
filter(gapminder, year == 1952)
#> # A tibble: 142 x 6
#> country continent year lifeExp pop gdpPercap
#> <fct> <fct> <int> <dbl> <int> <dbl>
#> 1 Afghanistan Asia 1952 28.8 8425333 779.
#> 2 Albania Europe 1952 55.2 1282697 1601.
#> 3 Algeria Africa 1952 43.1 9279525 2449.
#> 4 Angola Africa 1952 30.0 4232095 3521.
#> 5 Argentina Americas 1952 62.5 17876956 5911.
#> 6 Australia Oceania 1952 69.1 8691212 10040.
#> 7 Austria Europe 1952 66.8 6927772 6137.
#> 8 Bahrain Asia 1952 50.9 120447 9867.
#> 9 Bangladesh Asia 1952 37.5 46886859 684.
#> 10 Belgium Europe 1952 68 8730405 8343.
#> # ... with 132 more rows
Created on 2021-03-22 by the reprex package (v1.0.0)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.