rdataframefor-loopcbind

Loop for combining specific columns from dataframe


I'm looking to make a loop that adds to an already existing data frame.

So far I've tried to use the cbind function, as each data frame has 2 columns, dates and observations for that variable on said date. I'm looking to add just the observation to the masterlist. and all columns are the same length.

library(alfred)
library(dplyr)
library(tidyverse)

#loop for retrieving data and formatting it correctly (does not need changing)
vari_list=list("GDP","INDPRO","PI","SAVINGSL", "UNEMPLOY","HOUST")
for(vari in vari_list){
  temp1=get_fred_series(vari,"value",)
  final=data.frame(date=seq(as.Date("1776-07-04"),as.Date("2022-09-16"),1)) %>% left_join(temp1) %>% fill("value")
  names(final)[names(final) == 'value'] <- tolower(vari)
  assign(tolower(vari), final)
}

#defining the starting dataframe 
masterlist <- gdp

#new vari list without gdp
vari_list2=list("INDPRO","PI","SAVINGSL", "UNEMPLOY","HOUST")

#loop to combine the dataframe observations by date
for(vari in vari_list2){
  fin=tolower(unlist(vari))
  masterlist<-cbind(masterlist, fin )
}

masterlist starts by duplicating the data frame of the first variable which is the first element in the list, but all in lowercase hence the assign(tolower())(which is also to "unstring" it). I'm trying to add each subsequent variable after it. The list will get much bigger so that's why I'm trying to make it into a loop. When running this loop it outputs not the observations columns, but a repetition of the observation name. I feel that I'm pretty close, Does anyone know to move forward?


Solution

  • We need to get the value of the object name from the global env

    for(vari in vari_list2){
      fin=tolower(unlist(vari))
      tmp <- get(fin)
      tmp <- tmp[setdiff(names(tmp), "date")]
      masterlist<-cbind(masterlist, tmp ) 
    }
    

    -output

    > tail(masterlist)
                date      gdp       date   indpro       date      pi       date savingsl       date unemploy       date houst
    89919 2022-09-11 25248.48 2022-09-11 104.5464 2022-09-11 21895.5 2022-09-11  10660.8 2022-09-11     5753 2022-09-11  1575
    89920 2022-09-12 25248.48 2022-09-12 104.5464 2022-09-12 21895.5 2022-09-12  10660.8 2022-09-12     5753 2022-09-12  1575
    89921 2022-09-13 25248.48 2022-09-13 104.5464 2022-09-13 21895.5 2022-09-13  10660.8 2022-09-13     5753 2022-09-13  1575
    89922 2022-09-14 25248.48 2022-09-14 104.5464 2022-09-14 21895.5 2022-09-14  10660.8 2022-09-14     5753 2022-09-14  1575
    89923 2022-09-15 25248.48 2022-09-15 104.5464 2022-09-15 21895.5 2022-09-15  10660.8 2022-09-15     5753 2022-09-15  1575
    89924 2022-09-16 25248.48 2022-09-16 104.5464 2022-09-16 21895.5 2022-09-16  10660.8 2022-09-16     5753 2022-09-16  1575