Adding an ID column in a list of dataframes in R

Viewed 502

I have a list of XML files that I am merging together, but I am trying to figure out how to add an "id" column to each data frame based on the file name.


# BUILD DATAFRAME LIST
list_filenames <- list.files(pattern = ".xml$")

df_list <- lapply(list_filenames, function(f) {

  list_ids <- as.list(list_filenames)

  doc  <- xmlParse(f, useInternalNodes = TRUE)
  doc2 <- xmlToDataFrame(doc, nodes = getNodeSet(doc, "//Event"))
  mapply(cbind, doc2, "id" = list_ids, SIMPLIFY = F)  # Code that kind of works

  })

final_df <- do.call(rbind, df_list)

I'm hoping to get something that looks like this:

ex_df <- cbind(x = c(3, 2, 10, 12),
               y = c("a", "b", "c", "d"),
               id = c("file_name_1", "file_name_1", "file_name_2", "file_name_2")) %>% 
  as.data.frame()

> ex_df
   x y          id
1  3 a file_name_1
2  2 b file_name_1
3 10 c file_name_2
4 12 d file_name_2
1 Answers

We can use Map

nm1 <- sub("\\.xml$", "", list_filenames)
out <- do.call(rbind, Map(cbind, df_list, id = nm1))

In the OP's code, we are looping over the list_filenames and then in the second line, using the full set of list_filenames in

as.list(list_filenames)

Instead it would be just 'f' i.e

df_list <- lapply(list_filenames, function(f) {

  list_id <- sub("\\.xml$", "", f) #####

  doc  <- xmlParse(f, useInternalNodes = TRUE)
  doc2 <- xmlToDataFrame(doc, nodes = getNodeSet(doc, "//Event"))
  doc2$id = list_id
  doc2

  })

Then, we could rbind the `list elements

 do.call(rbind, df_list)
Related