Create and save a new dataframe in a summary?


#1

Hello,

I am trying to create a new dataframe and save it but I get no results from my code.
Please kindly advise?
Thank you
Christine

sleep_cleaned <- brfss2013%>%

filter(sleptim1< 24) %>%

group_by (genhlth) %>%

summarise (median_sleep= median(sleptim1),avg_sleep= mean(sleptim1), sleep_sd = sd(sleptim1), n = n())

#2

By “no results” do you mean that the code doesn’t run, or you get an empty table?

If it’s the former, does reformating so that the line breaks in the code are removed help (this shouldn’t matter, but the interpreter may be getting stuck with the empty lines between function calls)? E.g.:

sleep_cleaned <- brfss2013 %>%
  filter(sleptim1 < 24) %>%
  group_by(genhlth) %>%
  summarise(
    median_sleep = median(sleptim1),
    avg_sleep = mean(sleptim1), 
    sleep_sd = sd(sleptim1), 
    n = n()
    )

If it’s the latter, are you sure that the criteria in filter() will return results, i.e. are there any records in brfss2013 where sleptim1 is (strictly) less than 24? If not then you’ll be summarising an empty data.frame/tibble and will get an empty one back.


#3

On your first line you have brfss2013%>%. There needs to be a space between the object you’re manipulating (the data frame brfss2013, I assume) and the operator you’re using (in this case, %>%).

I’d recommend taking a look at the Workflow Basics section of @hadley and @garrett’s book (which is free online, linked to above).

The first exercise for that chapter (it’s only a few pages long), in the Practice section is a perfect example of this type of detail that’s easy to miss, but that make for a substantial programming payoff if you “[train] your brain to notice even the tiniest difference…” :yum:


#4

Hi Jim,
Thanks for your reply! I mean that it does run without errors but I do not get any table or any data. (see photo, nothing under my code and not errors)
I tried reformatting as you suggested but it does not work.
I am 100% sure there are data below 24h.


#5

Hi Mara,

Thank you that is great, I will have a look at that.
If you mean like below, It is still not working.

sleep_cleaned <- brfss2013 %>%

filter(sleptim1< 24) %>%

group_by (genhlth) %>%

summarise (median_sleep= median(sleptim1),avg_sleep= mean(sleptim1), sleep_sd = sd(sleptim1), n = n())

#6

It looks like you’re using a notebook. Nothing will show in the notebook output because you are saving the results to a variable sleep_cleaned; is that what you mean? What happens if you run the chunk without saving the answer to a variable?

E.g.

brfss2013 %>%
  filter(sleptim1 < 24) %>%
  group_by(genhlth) %>%
  summarise(
    median_sleep = median(sleptim1),
    avg_sleep = mean(sleptim1), 
    sleep_sd = sd(sleptim1), 
    n = n()
    )

#7

What are the contents of sleep_cleaned after you have run the code? Empty (zero rows) or something else?

Can you show the result of print(sleep_cleaned) to help us.


#8

XOR there’s never a wrong time to learn to make a reprex…

http://reprex.tidyverse.org/

That way we can see what libraries you’ve loaded, etc., since it would be self-contained.


#9

Hi Mara.
Thank you I tried with the reprex, I tried but could not get it to work I will give it another go!


#10

Are you sure there is a requirement for a space there? For example this works fine:

5%>%`+`(2)

The R style guide http://adv-r.had.co.nz/Style.html recommends a space around variable, operators, etc. but I don’t think the language https://cran.r-project.org/doc/manuals/r-devel/R-lang.html itself has that requirement. The language doc doesn’t seem to say anything directly about where space must be used and where it is optional.

But in the end the style guide should be followed. You can enable this in preferences:

and you will get warnings when it is not followed. It’s not perfect but really helps.


#11

Hello Martin,

I tried it in a ggplot and it works but I need to have my summary chunk before the ggplot in my markdown so I need to create the frame before my ggplot.
I just want a saved data frame with values below 24 in my sleptim1 column and then a summary of the data in the markdown.
There are no zero rows.

sleep_cleaned <- brfss2013%>%
filter(sleptim1< 24)
  ggplot( sleep_cleaned,aes(x = genhlth,y=sleptim1)) + geom_boxplot()+ theme(axis.text.x = element_text(angle = 90, hjust = 1)) + theme_bw()

42


#12

Never mind:grinning:… you post popped up just when mine did.

You should confirm that there are date below 24h by doing the snippet below if you haven’t done this already. I can’t even begin to count I though I knew for sure what was in my data only to be proved wrong:grinning:

brfss2013%>%
filter(sleptim1< 24)

and see if you get anything back.

One of the issues with many language that let you use this pipe style, fluent programming is that it is often harder to debug because the pipe chain is treated as an atomic statement.

So it would be good to try doing one step at a time in you pipe chain as far as you can.


#13

So it looks like you have saved your data successfully into sleep_time and you just need to print a table for the markdown document.

There are many different packages for showing tables. This shows some:
http://rmarkdown.rstudio.com/lesson-7.html

Others can suggest their favourites.


#14

There are data below 24. But I realised that by running the summary my data frame is grouped and I do not want a grouped data frame.
So I need to create a data frame and then run the chunk with the summary in a new chunk.


#15

I did not know that! :flushed: The more I learn, the more i learn I don’t know…


#16

You can put more than one line of code in a given chunk. For example, you can save the data frame with your current code, and then on the next line, put print(sleep_cleaned).


#17

Hello thank you so much for your time!
I realised that my ggplot contains the data frame the way I want it but when I run my summary chunk the frame gets grouped. So it is working just not showing in the markdown.
But I want the data frame as in the ggplot and not grouped so I need to run a separate chunk before the summary to get the frame I want.

I will have a look at your packages for showing tables, that is great.


#18

Sorry that it hasn’t worked out for you yet! I’m sure we’ll get you reprex-ing in no time.(though, I know, that doesn’t necessarily mean you want to tackle that at the moment :blush:)

It might not be right now, but at some point, I definitely recommend taking the 10-15 minutes at some point to watch @jennybryan’s quick, but helpful overview of the package and how to use it (Jenny starts ~10:40):


#19

Try adding sleep_cleaned %>% ungroup() for your summary.


#20

Ahhh!!! perfect thank you!