dan davison davisonstatsoxacuk wed aug 06 2008 054221pm 0000 zack holden wrote dear r wizards folder containing 1000 files file need extract first row file paste new file write file need repeat operation additional row row 2 row 3 etc 23 rows file loop hi zack theres problems sketchedout loop see ive understood problem couple solutions use loops way intending take line file 1 line file 2 write file called lines_i 123 first one case tabular data uses readtable writetable might want mess arguments readtable writetable specifying whether header whether want rownames printed etc second one similar works line line regardless line looks like ie doesnt assume tabular data files collatelines1 functionfolder nrows23 files listfilesfolder fullnamestrue forfile files fileasdataframe readtablefile forrow 1nrows outfile pastelines_ row csv sep writetablefileasdataframerow fileoutfile appendtrue rownamesfalse colnamesfalse sep collatelines2 functionfolder nrows23 files listfilesfolder fullnamestrue forfile files fileascharactervector scanfile sepn forrow 1nrows outfile pastelines row sep_ catfileascharactervectorrow n fileoutfile appendtrue way use indexing power r get around nasty loop really mean want solution without explicit loops r possible would recommend stick straightforward solution youre completely comfortable programming style conceivable noforloop versions might faster lots files rows dont worry aout speed problem heres effort without loops bit stretch wasnt easy write first two ive probably missed cleaner solution collatelines1fancy functionfolder nrows23 outfiles pastelines_ 1nrows csv sep files listfilesfolder fullnamestrue filesasdataframes lapplyfiles readtable x lapplyfilesasdataframes functiondf splitdf ffactor1nrowdf split rows apart x docallmapply cx listfunfunction rbind simplifyfalse collate rows different data frames writefunction functiondataframe outfile writetabledataframe fileoutfile rownamesfalse colnamesfalse sep invisiblemapplywritefunction x outfiles thank advance suggestions newoutfile dataframe list listfilescdata list good name builtin function file 1 dont need forfile list row file1 thats going work list character vector havent got files dataframes yet newoutfile rbindrow newoutfile file file 1 writecsvoutfile file outputcsv alternative html version deleted ______________________________________________ rhelprprojectorg mailing list httpsstatethzchmailmanlistinforhelp please read posting guide httpwwwrprojectorgpostingguidehtml provide commented minimal selfcontained reproducible code ______________________________________________ rhelprprojectorg mailing list httpsstatethzchmailmanlistinforhelp please read posting guide httpwwwrprojectorgpostingguidehtml provide commented minimal selfcontained reproducible code thu 07 aug 2008 112125 0100 r index alternative nasty loop