I have been using python for a while now to work with pandas dataframes. I want to switch the same code I am using over to R. However, I don't have much experience with R and I'm not sure what options I have to do the same thing. I have a folder of many csv files, and I have a list of the file names which I would like to iterate over and do a full outer join of the files.
In pandas, I would run the following command,
import pandas as pd
filelist = pd.read_excel("/Users/XXX/Documents/test/data/list.xlsx") #contains a list of filenames in the File column around 8000 rows long
workingdf = pd.DataFrame() #create a blank data frame
for subdir in filelist.File:
df = pd.read_csv(f"/Users/XXX/Documents/test/data/{subdir}",index_col=0) #open file 2 columns named DATE and VALUE
df = df.rename(columns={"VALUE":subdir}) #rename the value column to the file name
workingdf = pd.merge(workingdf,df,how='outer',left_index=True,right_index=True)
The last part of the code merges all the dataframes in the dictionary into one big data frame using a full outer join.
I want to take a bunch of csv files I have in a folder, iterate over a list of file names, create data tables out of the files, and append them one by one to make one big data table in R.
I am trying to switch to R because I am running into memory and efficiency problems and someone suggested I give it a try.
How can I run the same instruction set in R? I don't have much experience with it but I have the following code so far.
library(openxlsx)
library(data.table)
filelist <- openxlsx::read.xlsx("/Users/XXX/Documents/test/data/list.xlsx")
for (row in filelist[1]){
}
I know I need to use tempdf = read.csv(paste("/Users/XXX/Documents/test/data/",row,sep""))
to create the path to the file and make the frame.
The renaming function should be along the lines of names(tempdf)[2]<-row
I also know that I have to use workingdf <- merge(workingdf,tempdf,by="DATE",all=TRUE)
But I don't really know where to start on this because I can't create a blank dataframe like in pandas and just start merging them. Any suggestions?