Creating new table from a big .csv table

前端 未结 1 666
陌清茗
陌清茗 2021-01-29 09:52

I have big .csv file. I would like to filter that file into a new table.

For example, I have .csv file as below:

   f1 f2  f3 f4  f5  f6 f7 f9  f10  f1         


        
相关标签:
1条回答
  • 2021-01-29 10:21
     sapply(1:nrow(dat), function(x) if (dat[x, "f1"]==1) { 
               write.csv( dat[ (dat[["f1"]]==1 )& (1:nrow(dat) >= x) , ])
                } else {NULL} )
    "","f1","f2","f3","f4","f5","f6","f7","f9","f10","f11"
    "t1",1,0,1,0,1,0,0,0,0,1
    "t2",1,0,0,0,0,1,1,1,1,1
    "t4",1,0,0,0,1,0,0,0,0,0
    "","f1","f2","f3","f4","f5","f6","f7","f9","f10","f11"
    "t2",1,0,0,0,0,1,1,1,1,1
    "t4",1,0,0,0,1,0,0,0,0,0
    "","f1","f2","f3","f4","f5","f6","f7","f9","f10","f11"
    "t4",1,0,0,0,1,0,0,0,0,0
    [[1]]
    NULL
    

    Will need to construct file names:

    invisible(
      sapply(1:nrow(dat), function(x) if (dat[x, "f1"]==1) { 
               write.csv( dat[ (dat[["f1"]]==1 )& (1:nrow(dat) >= x) , ] ,
                        file = paste0("fil_", x, ".csv") )
                                    } else {NULL} )
             )   
    

    If this is destined for Excel, as I fear it might be, note that the rownames are included but no column header will be created to designate the rownames.

    0 讨论(0)
提交回复
热议问题