I need to import several *.csv sheets and afterwards I have to combine them. I have a Panel dataset with quarterly data over 100 periods, eleven countries and 20 variables. I have each variable as extra csv. My aim is to have a dataframe in the way that the first column are the countries, the secound the date and from the third onwards my variables.
Here are the first rows of a *.csv:
Date,Austria,Belgium,Finland,France,Germany,Greece,Ireland,Italy,Netherlands,Portugal,Spain
Q1 1990,91.739,8.978,-12.598,28.071,37.638,94.159,34.13,13.214,24.101,40.43,2.556
Q2 1990,134.143,-2.89,-26.014,16.421,48.189,166.933,19.602,8.507,7.219,17.512,-19.39
Q3 1990,84.336,-3.891,-32.364,14.527,31.013,376.683,21.09,6.773,2.065,24.313,-7.234
What did I do so far?:
path = "~/R file/"
filenames <- dir(path, pattern = '\\.csv', full.names = F)
data <- read.csv(filenames[1])
data <- melt(data, id.vars="Date", value.name=filenames[1], variable.name="Country",na.rm=F)
data <- data[,c(1,2)]
for (i in length(filenames)){
print(paste("opening file ", filenames[i],sep=""))
dta <- read.csv(filenames[i])
dta_long <- melt(dta, id.vars="Date", value.name=filenames[i], variable.name="Country",na.rm=F)
data <- merge(data,dta_long,by = c("Country","Date"))
}
Print shows me a/my problem, that the loop only dealt with the last csv file. Therefore I have only three columns in data, instead of 22. Unfortunately I do not see how to solve the problem. Does someone has an idea?
Thanks in advance!