0

I have been given a MATLAB data set with 20 different variables. I've been able to read it into R using readMat(). When I look at the structure (str()) I get the following output, and I'm wanting to create a data frame so I can run further analysis. structure output

I've tried using data.frame() but this only gives me 1 variable with 2 observations.

I've also tried lapply() but it gives many observations with only 1 variable. r code

How can I create a data frame that shows the correct variables and observations?

Joundill
  • 6,828
  • 12
  • 36
  • 50
BKeys
  • 13
  • 3
  • 1
    Please add data using `dput` or something that we can copy and use. Also show expected output for the data shared. Read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Nov 19 '20 at 02:55
  • Hi Ronak Shah - I don't believe I'm allowed to share my data (agreements etc). Is there some other way? Apologies, this is my first question. – BKeys Nov 19 '20 at 02:57
  • Have you tried df <- as.data.frame(df) – hachiko Nov 19 '20 at 02:58
  • Yes, as.data.frame() gives me the same object – BKeys Nov 19 '20 at 02:59
  • According to [this solution](https://stackoverflow.com/a/28097519/6288065), I think you're supposed to use lapply() not on dat, but on the structure inside dat, which is dat$SG_surveys_stn_p_scalar_data.mat, like `lapply(dat$SG_surveys_stn_p_scalar_data.mat, unlist, use.names=FALSE)`. – LC-datascientist Nov 19 '20 at 03:11
  • Thanks @LC-datascientist, I've tried the new lapply like you've written and get an object of a large list with 2 elements. Very similar to my first problem unfortunately. – BKeys Nov 19 '20 at 03:21
  • from the image ot would look like `as.data.frame(dat[[1]][[1]])` would work, and then grab the names from the second entry perhaps with `c(unlist(dat[[1]][[2]]))` or similar – user20650 Nov 19 '20 at 03:21
  • 1
    @user20650 This worked! Thank you very much :D – BKeys Nov 19 '20 at 03:56

1 Answers1

0

From the image you attached it looks like you could try data.frame(dat[[1]][[1]]) for the data.

The column names appear to be nested under dat[[1]][[2]] so you might need to use lapply or flatten (dat[[1]][[2]]).

Itamar
  • 2,111
  • 13
  • 16