You are going to include the entire data frame if you use mutate
. You'll want to whole data frame if the assignment of yes or no is conditionally based on sales.
library(tidyverse)
# create carseats.df
set.seed(39582) # make it repeatable
carseats.df <- data.frame(sales = rnorm(100, 10000, 505))
# now create conditional variable
carseats.df <- carseats.df %>%
mutate(HighVol = ifelse(sales > 10000, # true or false
"yes", # result if true
"no") %>%
as.factor()) # result if false
head(carseats.df)
# sales HighVol
# 1 9992.190 yes
# 2 10077.482 no
# 3 9507.145 yes
# 4 10780.788 no
# 5 10433.133 no
# 6 10907.665 no
It looks like you're fairly new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from dput(head(dataObject)))
and any libraries you are using. Check it out: making R reproducible questions.
The reason you haven't seen any help is most likely due to the lag of meaningful tags. You only have the tag tree
which isn't meaningful. At a minimum, you would want to include a tag for the programming language: r
. You could also add things like mutate
or the library it's derived from, dplyr
.