-4

If I have the mean and standard error from the mean (SE) for a particular set of numbers, how would I go about combining the two values into one dataframe? For example, I have the variable mean_boeing (for the average) and stde_boeing (for the error from the mean) and I want to combine these two into one dataframe. Ultimately, I will be doing this for several other variables, combining them all into one big dataframe so that I can graph them in ggplot.

Thanks

hawken1
  • 17
  • 3
  • Can you share sample data? See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – markus Feb 01 '20 at 21:07

1 Answers1

0

We can use data.frame to create a data.frame

df1 <-  data.frame(mean_boeing, stde_boeing)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you -- I will be doing this multiple times. The next step would be to then convert all of those newly formed dataframes into one single dataframe. How would I go about doing that? – hawken1 Feb 01 '20 at 21:20
  • @hawken1 I would load all of this in a `list` instead of creating multiple data.frames objects and then a final one. i.e. `do.call(rbind, Map(function(x, y) data.frame(Mean = x, stde = y), mget(ls(pattern= '^mean_')), mget(ls(pattern = '^stde'))))` – akrun Feb 01 '20 at 21:25
  • Would I do this command individually for all of the averages/stde? Or is there a way to put all of them into this? Sorry I am new to R. – hawken1 Feb 01 '20 at 21:31
  • 1
    @hawken1 The above command assumes that you have created `mean` objects for all the datasets, `mget(ls(pattern = '^mean_'))` loads those object names that starts with 'mean_` into a `list`, similarly for 'stde_. If you want to do this from a set of files il.e `.txt` or `.csv`, load it into a `list` i.e. `files <- list.files(patterns = "\\.csv", full.names = TRUE); do.call(rbind, lapply(files, function(x) {x1 <- read.csv(x); data.frame(mean = mean(x1$yourcol), sd = sd(x$yourcol))}))` – akrun Feb 01 '20 at 21:36