-1

My question is similar to the topic, I would like to load two files at once. I know I can use the function list.files. However, I do not know how to apply it correctly so that my program will work. I would also like to ask how can I make two frames of data for each loaded file. Below is the look of my code (for one file):

txt <- stri_read_lines("script.R")
txt <- txt[txt != ""]
r1 <- strsplit(txt, "")
r2 <- lengths(r1)
r3 <- unlist(r1)
r4 <- rep(
  seq_along(r1),
  r2
)
r5<- unlist(
  lapply(r2, seq_len)
)
TD <- data.frame(
  signs = r3,
  rows= r4,
  columns= r5
)
TD
Elia
  • 53
  • 7
  • Can't you define the above as a function and call that function inside `lapply` or `sapply`? – KenHBS Feb 05 '18 at 15:35
  • `all_files <- list.files("path_to_files", full.names=TRUE)` `data_as_list <- lapply(all_files, function(i) readLines(i, ...))` – CPak Feb 05 '18 at 15:35
  • 1
    @CPak that will not work. The second argument to `lapply` must be a named function (e.g., `lapply(x, readLines, more_args)` or an anonymous function (e.g., `lapply(x, function(i) {...})`. – r2evans Feb 05 '18 at 15:38
  • @r2evans, you're right. I was careless – CPak Feb 05 '18 at 15:40
  • Elia, it is not clear what you want or what your code is trying to do. You mentioned *"multiple files"*, but I see no mention of filenames in your code. If your question is about reading lines for files, please simplify your code to *only* include parts relevant to (a) defining a vector/list of filenames, either literally or a simple one-line call to something like `list.files`; and (b) your attempt to read the text from each of those files. I further suggest you be explicit in your expected output, partly because I'm confused by *"make two frames of data for each loaded file"*. – r2evans Feb 05 '18 at 15:42
  • I would like my program to work for reading two files. As you can see, my works only for one. – Elia Feb 05 '18 at 15:46

1 Answers1

1

You can define your code as a function and then use it inside lapply or sapply:

readfiles <- function(docname){
  txt <- stri_read_lines(docname)
  txt <- txt[txt != ""]
  r1 <- strsplit(txt, "")
  r2 <- lengths(r1)
  r3 <- unlist(r1)
  r4 <- rep(seq_along(r1), r2)
  r5 <- unlist(lapply(r2, seq_len))
  TD <- data.frame(
          signs=r3,
          rows=r4,
          columns=r5)
  return(TD)
}

docnames <- list.files(pattern="*.R")
yourdocs <- lapply(docnames, readfiles)
list2env(yourdocs)
KenHBS
  • 6,756
  • 6
  • 37
  • 52
  • However, the program works badly should return a data frame for each file. And here the result is different. e.g., `signs factor,935 rows Integer,935 columns Integer,935 ` – Elia Feb 05 '18 at 20:22
  • 1
    It should be lapply instead of sapply – Elia Feb 05 '18 at 20:31
  • Yes, it should. You can also transform the two elements of the list to two separate data frames with ‘list2env()’, as long as the list is a named list – KenHBS Feb 05 '18 at 20:34