1

enter image description hereI am reading several SAS files from a server and load them all into a list into R. I removed one of the datasets because I didn't need it in the final analysis ( dateset # 31)

mylist<-list.files("path" ,  pattern = ".sas7bdat")
mylist <- mylist[- 31]

Then I used lapply to read all the datasets in the list ( mylist) at the same time

read.all <- lapply(mylist, read_sas)

the code works well. However when I run view(read.all) to see the the datasets, I can only see a number ( e.g, 1, 2, etc) instead of the names of the initial datasets. Does anyone know how I can keep the name of datasets in the final list? Also, can anyone tell me how I can work with this list in R? is it an object ? may I read one of the dateset of the list ? or how can I join some of the datasets of the list?

Benn
  • 59
  • 7
  • 1
    Hello, it is quite hard to work with non reproductible code. Could you please provide a reproductible example and examples of desired outputs? Based only on the current question, maybe [this solution](https://stackoverflow.com/a/18520422/10264278) can help you. List are container for objects. You can access objects inside a list using usual signs (`[]`, `[[]]`, object names, relative position, etc). You will find many tutorials on the internet (like [this](http://www.r-tutor.com/r-introduction/list) or [this](https://www.datamentor.io/r-programming/list/]) about lists – Paul May 27 '20 at 07:21
  • Do you want `names(read.all) <- mylist` ? – Ronak Shah May 27 '20 at 07:23
  • 2
    `sapply(newfi, read_sas, simplify=FALSE)` will do effectively the same as `lapply` but preserve names. – r2evans May 27 '20 at 07:26
  • @RonakShah , Sorry I corrected the code already . So, I am reading from a server various datasets and putting them in a list ( mylist) . Then I read them all using lapply(mylist, read_sas) - when I look at the final product ( read. all) which is a list , it just shows numbers instead of the names of imported datasets. – Benn May 27 '20 at 19:31
  • @r2evans Thanks it is working but it shows the whole path of the datasets instead of just the name of a dateset. This is what I see for one of the datasets imported in the list : //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/all_lift_batch.sas7bdat - but I just need to see the name of a dataset which is all_lift_batch in this regard with no path address and no .sas7bdat – Benn May 27 '20 at 19:51

1 Answers1

0

Use basename and tools::file_path_sans_ext:

filenames <- head(list.files("~/StackOverflow", pattern = "^[^#].*\\.R", recursive = TRUE, full.names = TRUE))
filenames
# [1] "C:\\Users\\r2/StackOverflow/1000343/61469332.R"  "C:\\Users\\r2/StackOverflow/10087004/61857346.R"
# [3] "C:\\Users\\r2/StackOverflow/10097832/60589834.R" "C:\\Users\\r2/StackOverflow/10214507/60837843.R"
# [5] "C:\\Users\\r2/StackOverflow/10215127/61720149.R" "C:\\Users\\r2/StackOverflow/10226369/60778116.R"
basename(filenames)
# [1] "61469332.R" "61857346.R" "60589834.R" "60837843.R" "61720149.R" "60778116.R"
tools::file_path_sans_ext(basename(filenames))
# [1] "61469332" "61857346" "60589834" "60837843" "61720149" "60778116"

somedat <- setNames(lapply(filenames, readLines, n=2), 
                    tools::file_path_sans_ext(basename(filenames)))
names(somedat)
# [1] "61469332" "61857346" "60589834" "60837843" "61720149" "60778116"

str(somedat)
# List of 6
#  $ 61469332: chr [1:2] "# https://stackoverflow.com/questions/61469332/determine-function-name-within-that-function/61469380" ""
#  $ 61857346: chr [1:2] "# https://stackoverflow.com/questions/61857346/how-to-use-apply-family-instead-of-nested-for-loop-for-my-problem?noredirect=1" ""
#  $ 60589834: chr [1:2] "# https://stackoverflow.com/questions/60589834/add-columns-to-data-frame-based-on-function-argument" ""
#  $ 60837843: chr [1:2] "# https://stackoverflow.com/questions/60837843/how-to-remove-all-parentheses-from-a-vector-of-string-except-whe"| __truncated__ ""
#  $ 61720149: chr [1:2] "# https://stackoverflow.com/questions/61720149/extracting-the-original-data-based-on-filtering-criteria" ""
#  $ 60778116: chr [1:2] "# https://stackoverflow.com/questions/60778116/how-to-shift-data-by-a-factor-of-two-months-in-r" ""

Each "name" is the character representation of (in this case) the stackoverflow question number, with the ".R" removed. (And since I typically include the normal URL as the first line then an empty line in the files I use to test/play and answer SO questions, all of these files look similar at the top two lines.)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thanks! I wrote somedat <- setNames(lapply(mylist, read_sas), tools::file_path_sans_ext(basename(mylist))) and it exactly produced what I expected . Now, I can see a list with objects that have the name of datasets. Thanks so much. – Benn May 28 '20 at 14:47
  • do you happen to have a document / tutorial to work with a list of datasets in R to recommend? Cheers – Benn May 28 '20 at 14:50