0

I'm using package RISmed in R in order to obtain information from PubMed. The 'Mesh' package function allows me to obtain the MeSH terms of each citation. Nevertheless it is a List, containing a Data Frame. I want to list each MeSH term besides its corresponding citation id (PMID). For example, I can construct a table containing both values:

table = cbind(ArticleId(MedlineObject),Mesh(MedlineObject))

The first column is a char object but the second one is a list, containing a dataframe. If the value inside 1st column were "29145282" and the content of 2dnd column were "Cardiomyopathy, Hypertrophic", "Combined Modality Therapy" and "Diagnosis, Differential", I would want to obtain:

"29145282","Cardiomyopathy, Hypertrophic"
"29145282","Combined Modality Therapy"
"29145282","Diagnosis, Differential"

How could I accomplish this?

neilfws
  • 32,751
  • 5
  • 50
  • 63
panchtox
  • 634
  • 7
  • 16
  • 2
    Please try to provide [a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). At a minimum include the code which generated `MedlineObject`. Also, `table` is not a great variable name (there's a function with the same name). – neilfws Dec 04 '17 at 20:58
  • Hi neilfws, the example used by @TooYoung below it's a good one. "table" is not a good variable name, indeed. It wasn't the real one, only an example. Thanks – panchtox Dec 05 '17 at 09:26
  • Glad to hear it works. Would you mind accepting that answer? – TooYoung Dec 05 '17 at 15:04

2 Answers2

1

I would like to use myeloma as an example of medline object since I don't have your data. myeloma is a medline data within RISmed package.

First add the id to all dataframes in your list by mapply and cbind:

MedList = mapply(cbind, "ID"=ArticleId(myeloma),Mesh(myeloma),SIMPLIFY = FALSE)

And then merge all list into one dataframe by do.call and rbind:

MedFrame = do.call("rbind",MedList)

You just need to change 'myeloma' in the code to your own MedlineObject

TooYoung
  • 387
  • 8
  • 18
0

Check out the tidyr and tibble packages. Make sure to take a look at the nest() and unnest() functions. I can't give you much more advice without a reproducible example.

x = 1:5
data <- lapply(x, function(i){
  data.frame(y = 1:5 * i)
})

temp = tibble::tibble(x, data)
temp
tidyr::unnest(temp)
struggles
  • 825
  • 5
  • 10