Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (50.2k points)

I want to loop through a collection of files in R and just want to access the information from each one. I don't know why the loop is very slow. Is there a way I could vectorize this?


all_files=list.files(path="~/p/a/t/h", recursive = TRUE)

for(i in seq_along(all_files)) {

    temp = fromJSON(file = all_files[i])

    if (length(temp$tags) != 0){

        songTags <- c(songTags, temp$tags)

        songTrack_id <- c(songTrack_id, temp$track_id)



1 Answer

0 votes
by (108k points)

See in R programming, if you want to grow the objects in a loop, then usually the process is very slow. You can use lapply/sapply instead.

all_data <-, lapply(all_files, function(x) {

                temp = jsonlite::fromJSON(file = x)


                   list(tags = temp$tags, track_id = temp$track_id)


If you want a more convenient one then you can sue purrr's map_df:

all_data <- map_df(all_files, ~{

               temp = jsonlite::fromJSON(file = .x)


                  list(tags = temp$tags, track_id = temp$track_id)


Browse Categories