import - R - Reading unknown number of files -
this question has answer here:
- importing multiple .csv files r 12 answers
i'm new r. try process experimental data , i'm stuck bug when reading files. want read data files in folder don't know how many there. know files named "subject1manualx.log", x being 1 or higher. since didn't find possibility count how many files in folder, try open files in while loop until exception raised (i.e. try open "subject1manual1.log", "subject1manual2.log" etc).
here code (prints debug):
# script work on et data temp <- 0 while (temp == 0){ trycatch({ subjectdata <- read.delim("d:/doctorat/xps/xp1-2_lipsmwa/smarteye/subject1/subject1manual3.log") }, warning = function(w){ print(paste("warning", i)) temp <- 1 }, error = function(e){ print(paste("error", i)) temp <- 1 }, = { print("finished") }) }
unfortunately doesn't work (that's why i'm here...). know have warnings , errors. if handle warnings, r crashes because errors aren't handled ("all connection in use" crash). if handle errors, doesn't go through , while loop continues @ each iteration.
any idea on matter highly appreciated (including better ways read unknown number of files). thank time!
pyxel
edit: ok nice persons answered how import multiple data files, curiosity know how deal try catch within while loop. idea?
# here read path '*.log' files in folder path_to_files = dir("d:/doctorat/xps/xp1-2_lipsmwa/smarteye/subject1/", full.names = true, recursive = true, pattern = "\\.log$") # further read files list files = lapply(path_to_files, read.delim) # , finaly combine files single data.frame # headers of files should identical res = do.call(rbind, files)
update code trycatch , while. not safe , grows data.frame in loop - bad practice.
subjectdata = true = 1 res = null while (!is.null(subjectdata)){ subjectdata <- trycatch({ read.delim(sprintf("d:/doctorat/xps/xp1-2_lipsmwa/smarteye/subject1/subject1manual%s.log", i)) }, warning = function(w){ print(paste("warning", i)) null }, error = function(e){ print(paste("error", i)) null }) if(is.null(res)){ res = subjectdata } else { res = rbind(res, subjectdata) } = + 1 }
Comments
Post a Comment