[R] importing multiple text files in R

Kenn Konstabel lebatsnok at gmail.com
Tue Apr 1 20:07:10 CEST 2014


you can extract numbers from your file names and then sort them like this:

filelist = list.files(pattern = ".s*.txt")
filelist[order(as.integer(gsub("[^0-9]", "", filelist)))]
(cf with alphabetic order: filelist[order(gsub("[^0-9]", "", filelist))]

Or if you just have s1...s120 you can construct the names programmatically

filelist<-paste0("s", 1:120, ".txt")


On Tue, Apr 1, 2014 at 7:19 PM, Frede Aakmann Tøgersen <frtog at vestas.com> wrote:
> Try
> mixedsort  in gtools package
>
> Br. Frede
>
>
> Sendt fra Samsung mobil
>
>
> -------- Oprindelig meddelelse --------
> Fra: eliza botto
> Dato:01/04/2014 18.10 (GMT+01:00)
> Til: r-help at r-project.org
> Emne: [R] importing multiple text files in R
>
> Dear useRs,
> I have a number of text file located at a certain location with the following names.
> s1.txt,s2.txt,s3.txt,s4.txt,s5.txt...............s120.txt
> when i read them, before opening them, by using
> filelist = list.files(pattern = ".s*.txt")
> The are opened in the following order
> [1] "s1.txt"   "s10.txt"  "s100.txt" "s101.txt" "s102.txt" "s103.txt" "s104.txt" "s105.txt" "s106.txt" "s107.txt" "s108.txt" "s109.txt" "s11.txt"  "s110.txt" [15] "s111.txt" "s112.txt" "s113.txt" "s114.txt" "s115.txt" "s116.txt" "s117.txt" "s118.txt" "s119.txt" "s12.txt"  "s120.txt" "s13.txt"  "s14.txt"  "s15.txt"
>  [29] "s16.txt"  "s17.txt"  "s18.txt"  "s19.txt"  "s2.txt"   "s20.txt"  "s21.txt"  "s22.txt"  "s23.txt"  "s24.txt"  "s25.txt"  "s26.txt"  "s27.txt"  "s28.txt"
>  [43] "s29.txt"  "s3.txt"   "s30.txt"  "s31.txt"  "s32.txt"  "s33.txt"  "s34.txt"  "s35.txt"  "s36.txt"  "s37.txt"  "s38.txt"  "s39.txt"  "s4.txt"   "s40.txt"
>  [57] "s41.txt"  "s42.txt"  "s43.txt"  "s44.txt"  "s45.txt"  "s46.txt"  "s47.txt"  "s48.txt"  "s49.txt"  "s5.txt"   "s50.txt"  "s51.txt"  "s52.txt"  "s53.txt"
>  [71] "s54.txt"  "s55.txt"  "s56.txt"  "s57.txt"  "s58.txt"  "s59.txt"  "s6.txt"   "s60.txt"  "s61.txt"  "s62.txt"  "s63.txt"  "s64.txt"  "s65.txt"  "s66.txt"
>  [85] "s67.txt"  "s68.txt"  "s69.txt"  "s7.txt"   "s70.txt"  "s71.txt"  "s72.txt"  "s73.txt"  "s74.txt"  "s75.txt"  "s76.txt"  "s77.txt"  "s78.txt"  "s79.txt"
>  [99] "s8.txt"   "s80.txt"  "s81.txt"  "s82.txt"  "s83.txt"  "s84.txt"  "s85.txt"  "s86.txt"  "s87.txt"  "s88.txt"  "s89.txt"  "s9.txt"   "s90.txt"  "s91.txt"
> [113] "s92.txt"  "s93.txt"  "s94.txt"  "s95.txt"  "s96.txt"  "s97.txt"  "s98.txt"  "s99.txt"
>
> How can I open them systematically starting from s1,s2,s3 and all the way upto s120?
> Thankyou very much indeed in advance.
> Eliza
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list