I want to create a new variable excluding data above 15, how do I do that?

Hello,

Please kindly advice.

I want to create a new variable that excludes all data above 15hours, How do I do that?
When I use the formula below I get a binary outcome. Please help.

Thank you in advance,
Christine

brfss2013 <- brfss2013 %>% mutate(sleep_revised = sleptim1<15)

Thanks in advance,
Christine Kelly

Hi Christine,

What do you want to do with values above 15?

If you wish to edit values above 15 to a new label, then use if_else() to specify the two outcomes you wish.

Hi Connor,

Thank you very much! I am new to R, could you please give me an example of how I should use the ifelse with mutate statement? I just want to discard values over 15 as they are outliers.

So mutate() is used to edit existing columns or create new columns.

If you wish to exclude rows where sleeptime > 15, you need to use filter(). This takes a condition that you pass, and returns rows where this condition is true.

Hi, you need to use filter to filter the result. Here is an example:

library(tidyverse)
set.seed(23) # for reproducibilty
brfss2013 <- tibble(sleptim1 = sample.int(16, 100, replace=TRUE))
brfss2013_filtered <- brfss2013 %>% filter(sleptim1 < 15)
length(brfss2013_filtered$sleptim1)
# 84
max(brfss2013_filtered$sleptim1)
# 14
1 Like

Yes I have filtered but I want to save my filtered data as a new variable!

Thank you Florian, but how do I then save my filter as a new variable?

A dataframe must have the same length for all the columns, so you could do a new data fame (as in my above post) or have a binary column that tells you if the criteria is met, which was what you did in the first place.

I get it, I will just create a new data frame.Thank you very much.

Thanks Connor for your time, I see what you mean now.

You could overwrite the existing dataframe if you no longer require the data where sleptim1 >= 15, i.e.:
brfss2013 <- brfss2013 %>% filter(sleptim1 < 15)

1 Like