I'm working with a rather large dataframe. I need to use pivot_wider as follows
imf_ifs_data=pivot_wider(imf_ifs_longer,names_from = indicatorcode, values_from = value)
But this returns a dataframe with lists, so I saw examples and tried this
imf_ifs_data=imf_ifs_longer %>%
select(-c(indicatorname))%>%
#group_by(countryname) %>%
mutate(row = row_number()) %>%
tidyr::pivot_wider(names_from = indicatorcode, values_from = value) %>%
select(-row)
The problem is that the result here is a dataframe where the rows are not collapsed.
Here's a sample of the input data
> dput(imf_ifs_longer[c(1:15),c(1:6)])
structure(list(name = c("Dominica", "Dominica", "Dominica", "Dominica",
"Dominica", "Dominica", "Dominica", "Dominica", "Dominica", "Dominica",
"Dominica", "Dominica", "Dominica", "Dominica", "Dominica"),
countrycode = c(321L, 321L, 321L, 321L, 321L, 321L, 321L,
321L, 321L, 321L, 321L, 321L, 321L, 321L, 321L), indicatorname = c("Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index"
), indicatorcode = c("EREER_IX", "EREER_IX", "EREER_IX",
"EREER_IX", "EREER_IX", "EREER_IX", "EREER_IX", "EREER_IX",
"EREER_IX", "EREER_IX", "EREER_IX", "EREER_IX", "EREER_IX",
"EREER_IX", "EREER_IX"), date = c("X1977M11", "X1977M12",
"X1978M1", "X1978M2", "X1978M3", "X1978M4", "X1978M5", "X1978M6",
"X1978M7", "X1978M8", "X1978M9", "X1978M10", "X1978M11",
"X1978M12", "X1979M1"), value = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 98.5739799661449)), row.names = c(NA,
-15L), class = c("tbl_df", "tbl", "data.frame"))
Here's a sample of the data I get from the second version of pivot_wider
structure(list(name = c("Dominica", "Dominica", "Dominica", "Dominica",
"Dominica", "Dominica", "Dominica", "Dominica", "Dominica", "Dominica",
"Costa Rica", "Costa Rica", "Costa Rica", "Costa Rica", "Costa Rica"
), indicatorname = c("Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index",
"Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index"
), date = c("2023M5", "2023M6", "2023M7", "2023M8", "2023M9",
"2023M10", "2023M11", "2023M12", "2024M1", "2024M2", "1977M11",
"1977M12", "1978M1", "1978M2", "1978M3"), countryname = c("Dominica",
"Dominica", "Dominica", "Dominica", "Dominica", "Dominica", "Dominica",
"Dominica", "Dominica", "Dominica", "Costa Rica", "Costa Rica",
"Costa Rica", "Costa Rica", "Costa Rica"), time = structure(c(19478,
19509, 19539, 19570, 19601, 19631, 19662, 19692, 19723, 19754,
2861, 2891, 2922, 2953, 2981), class = "Date"), EREER_IX = c(94.0485257761888,
93.9416527401942, 92.2607108467543, 91.9934431700547, 93.5960429917284,
93.7240955617686, 92.7756740426763, 92.088492678905, 91.9458324906238,
NA, NA, NA, NA, NA, NA), ENDE_XDC_USD_RATE = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), row.names = c(NA,
-15L), class = c("tbl_df", "tbl", "data.frame"))