Need suggestion to understand other people's R code

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Need suggestion to understand other people's R code

zydjohn
Hello:
I am rather new to R language, but now I have to understand someone else’s R program, which is rather complicated.
I try to make it simple like the following:
I have one and only one CSV file in C:\Temp\Data\cffex_20170601.csv
This file has the following headers, total 3 columns:
Code, Time, Seconds
It has more than 16000 rows, the first 3 rows are like this:
IF1234,9:15:00,33300
IF1234,9:15:00,33300
IF1234,9:15:01,33301
Note: all the rows in the CSV file are the same for the first column, which is "IF1234", but could have different incremental values in the second and third columns.
……
Now I have other people’s code try to load the CSV files as dataframe. (The directory could have many similar files, but to make it simple, I used only one file)

ReadFiles <- function(path, contract) {
    files <- list.files(path = path)
    data <- list()
    for (f in files) {
        key <- substr(f, 7, 14)
        temp <- read.csv(paste(path, f, sep = ''), header = T, stringsAsFactors = F)
        data[[key]] <- temp[which(substr(temp$Code, 1, 2) == contract), c(1,2,3)]
    }
    save.image(data, file = paste(path, '/DataSet.RData', sep = ''))
    return(filepath)
}

However, the author didn’t show how to call this ReadFiles function.
I tried to guess what his code wants to do, so I tried called the function with the following:
ReadFiles("C:/Temp/Data/", "IF1234")

But the code didn’t work.
The issues are:
data[[key]] <- temp[which(substr(temp$Code, 1, 2) == contract), c(1,2,3)]
The substr(temp$Code, 1, 2) == contract can’t find any result, as in the CSV file, all the rows in column “Code” are the same “IF1234”, therefore, either
ReadFiles("C:/Temp/Data/", "IF1234") or ReadFiles("C:/Temp/Data/", "IF") will not produce any meaningful results.  I guess this statement should be like:
data[[key]] <- temp[which(substr(temp$Code, 1, 6) == contract), c(1,2,3)]

Another issue is:
save.image(data, file = paste(path, '/DataSet.RData', sep = ''))
It seems save.image function is rather picky, it always complains.
Error in save(list = names(.GlobalEnv), file = outfile, version = version,  :
  (list) object cannot be coerced to type 'double'
Even I changed the code from:
data[[key]] <- temp[which(substr(temp$Code, 1, 2) == contract), c(1,2,3)]
to:
data[[key]] <- temp[which(substr(temp$Code, 1, 6) == contract), c(1,2,3)]

Another issue is:
    return(filepath)
The filepath is not defined, I don’t know if R has some internal variable like filepath or simply because the save.image didn’t work, so return(filepath) failed.

Finlly, I wonder, even if I use the code:
data[[key]] <- temp[which(substr(temp$Code, 1, 6) == contract), c(1,2,3)]
I can see it has returned 16000 rows, but if I tried to see its length using either: length(data) or length(data[[key]] or nrow(data) or nrow(data[[key]]) the results are either 1 or NULL.
I hope I should get the result of 16000.
Please advise what you think.
Thanks,