0

There are 219 columns in each of the 8 csv files. And I want to export 219 csv file that combines each columns from each 8 files.

How to export multiple csv files as different name?? This is my code of importing csv files.

df1<-read.csv("RSI9D.csv", header=TRUE)
df2<-read.csv("RSI30D.csv", header=TRUE)
df3<-read.csv("MKT_CAP.csv", header=TRUE)
df4<-read.csv("VOLUME.csv", header=TRUE)
df5<-read.csv("INST_HOLD.csv", header=TRUE)
df6<-read.csv("MOVAVG30.csv", header=TRUE)
df7<-read.csv("MOVAVG10.csv", header=TRUE)
df8<-read.csv("MOVAVG5.csv", header=TRUE)

As I said above, I want to export a file that combines each column in each file. Since there are 219 columns, a total of 219 files will be generated.

송재민
  • 11
  • 1
  • Sequentially named variables are awful. You should have a look at [how to make a list of data frames](https://stackoverflow.com/a/24376207/903061). – Gregor Thomas May 24 '17 at 05:35
  • If you want to combine all files from df1 to df8: all – Adamm May 24 '17 at 05:36
  • I don t understand what you mean by 219 csv files. Do you mean: Take col1 from file1, file2, file3, ... file8 -> make csv file. Take col2 from file1, file2, file3,...file8 -> make csv file? – Buggy May 24 '17 at 05:38
  • @AdammIn my 8 csv files, there are 219 columns. And I want to combine first column of df1+first column of df2+...+first column of df8 – 송재민 May 24 '17 at 05:42
  • @user3293236 YES!!! right! how to do that?? I think i have to use for( )function and cbind etc... – 송재민 May 24 '17 at 05:43

3 Answers3

1

you could try using SQL Server, import each file into a table and than merge them using commands like UNION and simply take and export out.(given that all tables headers/csv files are having same value that you expect)

nightfury
  • 300
  • 6
  • 17
1
dat <- do.call(rbind, lapply(1:8, function(x)get(paste0("df", x))))

for(i in 1:ncol(dat)) write.csv(dat[,i], paste0("file_",i, ".csv"), row.names=F)

Update:

df_list <- lapply(1:8, function(x) get(paste0("df", x)))

bindFn <- function(i, df_list){
  sapply(df_list, '[[', i)
}

for(i in names(df1)) write.csv(bindFn(i, df_list), paste0("file_",i, ".csv"), row.names=F)
Adam Quek
  • 5,660
  • 1
  • 11
  • 20
  • Thank you! But I have a problem! All observations are recorded in one column. I would like to have the 8 columns from each file. For example, if 219 files are created, each file has 8 columns – 송재민 May 24 '17 at 05:57
  • Thank you! very nice code!! May I ask one more if you do not mind? Based on your code, I'll perform a principal component analysis and try to export two results per file as csv file as follows.>df1.pcabbbwrite.csv(bb,"df1_importance.csv") >awrite.csv(a,"df1_rotation.csv")How should I enter the code at once? – 송재민 May 24 '17 at 09:56
  • When I bind the column, how to delete the row that has NA? – 송재민 May 24 '17 at 10:53
  • `na.exclude` and/or `na.omit` will remove rows containing missing value. Do note that this is only practical if you have large number of observations and few missing values. – Adam Quek May 25 '17 at 01:50
  • Do not really understand your question on `pca` though. Advise to post that as a separate question. – Adam Quek May 25 '17 at 01:50
0

if every csv has the same column names ,you could use "rbind" the build the 8 csv together,then export every column as a single csv file.

inputfiles = c("RSI9D.csv","RSI30D.csv","MKT_CAP.csv","VOLUME.csv","INST_HOLD.csv","MOVAVG30.csv","MOVAVG10.csv","MOVAVG5.csv")
new_data=data.frame()


for(inputfile in inputfiles){
  data = read.csv(inputfile,header = FALSE)
  new_data<-rbind(new_data,data)
}

df8<-read.csv("MOVAVG5.csv", header=TRUE)
colnames(new_data) = colnames(df8)
attach(new_data)

for(i in 1:length(new_data)){
  outputfile = paste(colnames(new_data)[i],".csv",sep = "")
  print(paste(outputfile," is exporting ----"))
  write.csv(new_data[,i],outputfile)
}