variable file names for read.csv in R

后端 未结 2 2007
梦毁少年i
梦毁少年i 2021-01-25 04:06

This may be a very common problem, so I am specifically looking for an elegant or at least less-kludgy-than-mine solution.

I have series of files from 001.csv to 200.csv

相关标签:
2条回答
  • 2021-01-25 04:26

    A combination of formatC() and paste0() should work:

    paste0(formatC(1:200, width = 3, format = "d", flag = 0), ".csv")
    
    0 讨论(0)
  • 2021-01-25 04:34

    This would be a good use of the sprintf function. It has many formatting options, but for this case I would use

    sprintf("%03d.csv", 1:200)
    
    #   [1] "001.csv" "002.csv" "003.csv" "004.csv" "005.csv"
    #   [6] "006.csv" "007.csv" "008.csv" "009.csv" "010.csv"
    #     ...
    #  [96] "096.csv" "097.csv" "098.csv" "099.csv" "100.csv"
    # [101] "101.csv" "102.csv" "103.csv" "104.csv" "105.csv"
    #     ...
    # [191] "191.csv" "192.csv" "193.csv" "194.csv" "195.csv"
    # [196] "196.csv" "197.csv" "198.csv" "199.csv" "200.csv"
    
    0 讨论(0)
提交回复
热议问题