how to use “for loop” and “write.csv” at the same time?

后端 未结 3 1982
感情败类
感情败类 2021-01-14 04:20

I have an array. It has lat,lon,time,and value. Time starts from 1 to 300. Here is part of array for time=1.

myarray[,,1]
     lon                        
         


        
相关标签:
3条回答
  • 2021-01-14 04:40

    One way is

    for (i in 1:300)  {
    write.table(myarray[,,i],file=""myarray.i.csv"",append=TRUE,sep=",",col.names=TRUE,row.names=TRUE) 
      }
    
    0 讨论(0)
  • 2021-01-14 04:42

    There are multiple ways of going about this:

    paste('myarray', i, 'csv', sep = '.')
    

    or:

    sprintf('myarray.%d.csv', i)
    

    I prefer the last one.

    0 讨论(0)
  • 2021-01-14 04:48

    This should work fine:

    n = dim(mydata)[3]
    
    for(i in 1:n) {
      #unpack a 3D array
      mat = mydata[,,i]
      form = sprintf('subject_%s.csv', i)
      write.csv(mat, file = form)
    
    }
    
    0 讨论(0)
提交回复
热议问题