Fetching Data from FRED

The eFRED package makes it easy to import any number of series from the FRED website into a single data.frame.

First, you will need an API key. You can freely obtain one by following the instructions at https://fred.stlouisfed.org/docs/api/api_key.html. The API key is a 32 character, lower-cased string. Once you have registered for one, assign it to a variable or use the set_fred_key function.

library(eFRED)
#> Loading required package: jsonlite
#> Loading required package: httr
api_key <- "abcdefghijklmnopqrstuvwxyz123456"
set_fred_key(api_key)

Almost all of the eFRED functions require an API key. The set_fred_key function sets the default key that is used by the functions so that you do not need to directly pass the key each time the function is called.

FRED data can be extracted by using the fred function. The general format of the function is fred(name1 = "code1", name2 = "code2", ...), where "code" is the unique series ID that can be found next to each series’ title at https://fred.stlouisfed.org/, and name is the new label for the series in the resulting data.frame. If no name is provided, then the code will be used. Any number of series can be used and each code can be a character vector containing with multiple entries.

The following searches for two series: “GDPC1” (Real GDP) and “UNRATE” (unemployment rate).

df <- fred(y = "GDPC1", "unrate", all=FALSE)
head(df)
#>         date        y UNRATE
#> 1 1948-01-01 2239.682    3.4
#> 2 1948-04-01 2276.690    3.9
#> 3 1948-07-01 2289.770    3.6
#> 4 1948-10-01 2292.364    3.7
#> 5 1949-01-01 2260.807    4.3
#> 6 1949-04-01 2253.128    5.3

The column containing the data for GDP is named "y". Since no name was provided for "UNRATE", the column name is the same as the series. Note that the codes are not case sensitive. The fred function automatically converts all codes to uppercase. The last argument, all=FALSE, is included because GDP and the unemployment rate have different frequencies (quarterly vs annual) and different starting periods. When all=FALSE is used, the data.frame forces each value to the shortest series; otherwise, the frequency would be monthly with many NA values for y.

The raw information about each series is contained within the data.frame’s info attribute, unless info=FALSE is included.

attr(df, "info")
#>       id realtime_start realtime_end                       title
#> 1  GDPC1     2025-02-27   2025-02-27 Real Gross Domestic Product
#> 2 UNRATE     2025-02-14   2025-02-14           Unemployment Rate
#>   observation_start observation_end frequency frequency_short
#> 1        1947-01-01      2024-10-01 Quarterly               Q
#> 2        1948-01-01      2025-01-01   Monthly               M
#>                              units         units_short
#> 1 Billions of Chained 2017 Dollars Bil. of Chn. 2017 $
#> 2                          Percent                   %
#>               seasonal_adjustment seasonal_adjustment_short        last_updated
#> 1 Seasonally Adjusted Annual Rate                      SAAR 2025-02-27 07:58:40
#> 2             Seasonally Adjusted                        SA 2025-02-07 07:47:42
#>   popularity
#> 1         92
#> 2         95
#>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             notes
#> 1                                                                                                                                                                                    BEA Account Code: A191RX\n\nReal gross domestic product is the inflation adjusted value of the goods and services produced by labor and property located in the United States.For more information see the Guide to the National Income and Product Accounts of the United States (NIPA). For more information, please visit the Bureau of Economic Analysis (http://www.bea.gov/national/pdf/nipaguid.pdf).
#> 2 The unemployment rate represents the number of unemployed as a percentage of the labor force. Labor force data are restricted to people 16 years of age and older, who currently reside in 1 of the 50 states or the District of Columbia, who do not reside in institutions (e.g., penal and mental facilities, homes for the aged), and who are not on active duty in the Armed Forces.\r\n\r\nThis rate is also defined as the U-3 measure of labor underutilization.\r\n\r\nThe series comes from the 'Current Population Survey (Household Survey)'\r\n\r\nThe source code is: LNS14000000
#>    group
#> 1      y
#> 2 UNRATE

The eFRED package contains a variety of other functions to interact with the FRED API. For example, the fred_search function can be used to search for various series. The example below searches for series based on the query "unemployment". The args parameter is a list containing other search parameters. In this case, search is limited to only the top three results.

search_results <- fred_search("unemployment", args = list(limit = 3))
search_results
#>          id realtime_start realtime_end              title observation_start
#> 1    UNRATE     2025-03-02   2025-03-02  Unemployment Rate        1948-01-01
#> 2 UNRATENSA     2025-03-02   2025-03-02  Unemployment Rate        1948-01-01
#> 3  UNEMPLOY     2025-03-02   2025-03-02 Unemployment Level        1948-01-01
#>   observation_end frequency frequency_short                units
#> 1      2025-01-01   Monthly               M              Percent
#> 2      2025-01-01   Monthly               M              Percent
#> 3      2025-01-01   Monthly               M Thousands of Persons
#>         units_short     seasonal_adjustment seasonal_adjustment_short
#> 1                 %     Seasonally Adjusted                        SA
#> 2                 % Not Seasonally Adjusted                       NSA
#> 3 Thous. of Persons     Seasonally Adjusted                        SA
#>             last_updated popularity group_popularity
#> 1 2025-02-07 07:47:42-06         95               95
#> 2 2025-02-07 07:47:42-06         58               95
#> 3 2025-02-07 07:47:42-06         71               72
#>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                notes
#> 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    The unemployment rate represents the number of unemployed as a percentage of the labor force. Labor force data are restricted to people 16 years of age and older, who currently reside in 1 of the 50 states or the District of Columbia, who do not reside in institutions (e.g., penal and mental facilities, homes for the aged), and who are not on active duty in the Armed Forces.\r\n\r\nThis rate is also defined as the U-3 measure of labor underutilization.\r\n\r\nThe series comes from the 'Current Population Survey (Household Survey)'\r\n\r\nThe source code is: LNS14000000
#> 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                The unemployment rate represents the number of unemployed as a percentage of the labor force. Labor force data are restricted to people 16 years of age and older, who currently reside in 1 of the 50 states or the District of Columbia, who do not reside in institutions (e.g., penal and mental facilities, homes for the aged), and who are not on active duty in the Armed Forces.\n\nThis rate is also defined as the U-3 measure of labor underutilization.\n\nThe series comes from the 'Current Population Survey (Household Survey)'\n\nThe source code is: LNU04000000
#> 3 The series comes from the 'Current Population Survey (Household Survey)'\r\nThe source code is: LNS13000000\r\n\r\nThe Unemployment Level is the aggregate measure of people currently unemployed in the US. Someone in the labor force is defined as unemployed if they were not employed during the survey reference week, were available for work, and made at least one active effort to find a job during the 4-week survey period. \r\n\r\nThe Unemployment Level is collected in the CPS and published by the BLS. It is provided on a monthly basis, so this data is used in part by macroeconomists as an initial economic indicator of current trends. The Unemployment Level helps government agencies, financial markets, and researchers gauge the overall health of the economy.\r\n\r\nNote that individuals that are not employed but not actively looking for a job are not counted as unemployed. For instance, declines in the Unemployment Level may either reflect movements of unemployed individuals into the labor force because they found a job, or movements of unemployed individuals out of the labor force because they stopped looking to find a job.\r\n\r\n\r\nFor more information, see:\r\nU.S. Bureau of Labor Statistics, CES Overview (https://www.bls.gov/web/empsit/cesprog.htm)\r\nU.S. Bureau of Labor Statistics, BLS Handbook of Methods: Chapter 2. Employment, Hours, and Earnings from the Establishment Survey (https://www.bls.gov/opub/hom/pdf/ces-20110307.pdf)

To grab each of these series, we can use the command fred(search_results$id, info=FALSE). The information about each is not needed since we have it from the search results.

The eFRED package also contains functions to search across tags, categories, releases, and much more.