This is an example of how to import multiple CSV files all at once. First, we download the latest version of the Flow of funds. The ZIP file contains many CSV files.
Example:
read_csv("csv/all_sectors_levels_a.csv") %>%
gather(series, value, -date) %>%
filter(value != "ND") %>%
mutate(date = date %>% as.character,
value = value %>% as.numeric,
series = series %>% as.character) %>%
head(10)
# # A tibble: 10 x 3
# date series value
# <chr> <chr> <dbl>
# 1 1946 FL154090005.A 697872
# 2 1947 FL154090005.A 745113
# 3 1948 FL154090005.A 773052
# 4 1949 FL154090005.A 796359
# 5 1950 FL154090005.A 854185
# 6 1951 FL154090005.A 927818
# 7 1952 FL154090005.A 963964
# 8 1953 FL154090005.A 994533
# 9 1954 FL154090005.A 1076464
# 10 1955 FL154090005.A 1168580
frb_z1 <- data_frame(filename = dir("csv", pattern = "*.csv")) %>%
mutate(data = map(filename, ~ read_csv(file.path("csv", .)) %>%
gather(series, value, -date) %>%
filter(value != "ND") %>%
mutate(date = date %>% as.character,
value = value %>% as.numeric,
series = series %>% as.character))) %>%
# Dataframe of tibbles to Dataframe
unnest %>%
# Remove duplicates
mutate(series = series %>% substr(1, 13)) %>%
unique %>%
# Factor: all but value
arrange(filename, series, date)
unlink("csv", recursive = TRUE)
unlink("data_dictionary", recursive = TRUE)
# # A tibble: 10 x 4
# filename date series value
# <chr> <chr> <chr> <dbl>
# 1 all_sectors_levels_a.csv 1946 FL102051003.A 0
# 2 all_sectors_levels_a.csv 1947 FL102051003.A 0
# 3 all_sectors_levels_a.csv 1948 FL102051003.A 0
# 4 all_sectors_levels_a.csv 1949 FL102051003.A 0
# 5 all_sectors_levels_a.csv 1950 FL102051003.A 0
# 6 all_sectors_levels_a.csv 1951 FL102051003.A 0
# 7 all_sectors_levels_a.csv 1952 FL102051003.A 0
# 8 all_sectors_levels_a.csv 1953 FL102051003.A 0
# 9 all_sectors_levels_a.csv 1954 FL102051003.A 0
# 10 all_sectors_levels_a.csv 1955 FL102051003.A 0