Skip to content

how to factor in the ID of the imported subtable to R table?

2 messages · YANJUN CHEN, David Winsemius

#
Dear R community,

I am new to R?did some online tutorials and exercises in R playground. I was wondering if I could seek guidance on the following matter.

I have a set of 403 .csv files. Each.csv file contains the same layouts and distinguished by subject ID and date in the file name. The dataset looks like this:

Sub1-20170305.csv
Sub2-20180214.csv
?
Sub403-20191109.csv

I will use rbind function to combine 403 csv files in a single file (myFile). I will create two new variables (use mutate function) in myFile (subject ID and date). Is there a way to subtract subject ID (shown as ?Sub1, 2,,,403?) and date from the name of the csv file and then place them in ?subject ID? and ?date? in myFile?

Any info on the issue itself or where to look for will be appreciated.

Thanks,

CJ
#
On 5/21/20 9:24 AM, YANJUN CHEN via R-help wrote:
Something along the lines of:

?regex ; ?sub

?read.table

?data.frame

?do.call

?rbind

myfiles <- lapply( list.files(your_path) , # each file name will be 
passed to anonymous function

 ?????????????????????????? function(nm) data.frame( subID = sub("-.+", 
"",? nm), # remove chars after "-"

date=sub("^.+-(.{8})[.]csv", "\\1", nm), #extract date as capture class

 ??????????????????????????????????????????? #assuming all files have 
same number of columns with no headers

 ????????????????????????????????????????? ? read.table( 
paste0(your_path, nm) )

big_file <- do.call(rbind, myfiles)
If you search StackOverflow or Rseek with topic terms " stacking 
multiple data files" you should find many worked examples.
You should now read the Posting Guide which will explain why you should 
NOT post in HTML.


Best;

David.