如何使用write.xlsx将列写入特定列

时间:2016-09-21 01:28:37

标签: r

    get_Close <- function(i,p,f){
        #read the symbol from the excel 
        library("readxl");
        symbols <- read_excel('test1.xlsx',col_names = FALSE);
        inputI <-i;
        inputP <-p;
        inputF <-f;
        #do a for loop here 
        amount <-nrow(symbols);
        for (i in 1:amount){
        inputQ <-symbols[i,];
        print(inputQ);
        dataSave<-getClose(inputQ,inputI,inputP,inputF);
        library(xlsx);
        write.xlsx(dataSave, "output.xlsx",sheetName ="Close Price",row.names = FALSE,col.names = FALSE); 
         }

    }

getClose是一个可以从url获取数据列的函数。无论如何使用write.xlsx将每列放入excel中的不同列?就像第一个数据保存到Excel中的A列,第二个数据保存到B ......

1 个答案:

答案 0 :(得分:0)

没有您的所有初始信息,这是我要开始的地方。我假设getClose()正在返回一个向量。

get_Close <- function(i,p,f){
    #read the symbol from the excel 
    library("readxl");
    symbols <- read_excel('test1.xlsx',col_names = FALSE);
    inputI <-i;
    inputP <-p;
    inputF <-f;
    #do a for loop here 
    amount <-nrow(symbols);
    #initialize an empty list to build out all your columns
    col_list <- list()
    for (i in 1:amount){
      inputQ <-symbols[i,];
      print(inputQ);
      dataSave<-getClose(inputQ,inputI,inputP,inputF);
      col_list <- c(col_list, list(dataSave))
     }
#use do.call to turn your list into a matrix
output <- do.call(cbind, col_list)
#coherce that matrix to a dataframe
output <- as.data.frame(output)
return(output)
}
library(xlsx);
#should be ready to write output to your xlsx destination
write.xlsx(output, "output.xlsx",sheetName ="Close Price",row.names = FALSE,col.names = FALSE);