In the previous post, an introduction to function composition is made. As function is one of the most important objects in R and functional programming techniques can be beneficial in a way to achieve code reusability and to result in succinct code, it would be important to appreciate their values (and, of course, limitations).
In this short post, a way to read an unstructured file is illustrated. Although there are many good functions to read files in various formats, there may an occasion that a file fails to be read using one of them. In this case, rather than reformatting it to be fit into one of the existing functions externally, it could be easier to process them internally. This post just shows one of the possible ways with an example which is based on a StackOverflow question.
The question is shown below.
As shown below, an Excel pivot table seems to be saved as a csv file so that it has blank (or NULL) values at the top lefthand side. Also, although it is saved as a csv file, its delimiter is Tab rather than comma.
Due to the way how the file is structured, conventional functions such as
read.table() don’t work. Therefore the file is open as file connection using
file() and then read line by line using
readLines(). Once it is read, some extra processing is performed recursively using
I hope this tip is helpful to handle unstructure files within R.