0

what should i do when i want to make new column with mutate but with if condition status on it.

example :

dt <- read.table(text="
name,gender,fat_%
adam,male,32
anya,female,27
gilang,male,24
andine,female,34
",sep=',',header=TRUE) 


## + > dt
##       name gender fat_.
##   1   adam   male    32
##   2   anya female    27
##   3 gilang   male    24
##   4 andine female    34

my question : what code i have to write if i want to make new column where gonna take 2 answer "yes" or "no". and my new column will be like this :

name      gender      fat_%      obesity
adam       male       32           yes
anya      female      27           no
gilang     male       24           yes
andine    female      34           no

note : formula to find obesity is (if male & fat > 26 = yes ,if girl & fat >32 = yes) if (if male & fat < 26 = no ,if girl & fat <32 = no)

CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
  • Welcome to stackoverflow. I would suggest [these](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) guidelines for posting new questions. They will increase your chances of obtaining help – DJJ May 10 '20 at 12:15
  • This question has been answered already. For instance [here](https://stackoverflow.com/questions/18012222/nested-ifelse-statement) and [here](https://stackoverflow.com/questions/31150764/how-to-create-a-new-column-of-data-in-r-with-if-statements). – DJJ May 10 '20 at 12:17
  • 1
    Does this answer your question? [Nested ifelse statement](https://stackoverflow.com/questions/18012222/nested-ifelse-statement) – DJJ May 10 '20 at 12:18
  • It is a good practice to accept a solution if that works for you. – Shan R May 21 '20 at 04:27

3 Answers3

0

Couple of suggestions first. Gender can be a single char M/F. You cannot use % in column name. Your column name 'fat', you probably meant BMI??

Does this work for you?

dt %>% 
  mutate (newcol = ifelse ((gender == "male"), (ifelse ((fat_ > 26), TRUE, FALSE)),
                           (ifelse ((fat_ > 32), TRUE, FALSE))))
Shan R
  • 521
  • 4
  • 8
0

Two solutions. First, a base Rsolution:

df$obesity <- ifelse (df$gender == "m" & df$fat_ > 26 , "yes",
                       ifelse(df$gender == "f" & df$fat_ > 32, "yes", "no"))

Using mutatefrom dplyr, a more compact code based on dplyrs if_else rather than base R's ifelse is this:

df %>% 
  mutate(obesity = if_else(gender=="m" & fat_ > 26|gender=="f" & fat_ > 32, "yes", "no"))

RESULT:

df
    name gender fat_ obesity
1   adam      m   32     yes
2   anya      f   27      no
3 gilang      m   24      no
4 andine      f   34     yes

DATA:

df <- data.frame(
  name = c("adam", "anya", "gilang", "andine"),
  gender = c("m", "f", "m", "f"),
  fat_ = c(32,27,24,34)
)
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
0

One approach is to use case_when from dplyr:

library(dplyr)
df %>% 
  mutate(obesity = case_when(gender == "male" & fat > 26 ~ "yes",
                             gender == "female" & fat > 32 ~ "yes",
                             TRUE ~ "no"))
#    name gender fat obesity
#1   adam   male  32     yes
#2   anya female  27      no
#3 gilang   male  24      no
#4 andine female  34     yes

Once you understand the syntax, it comes in handy quite often.

Data

structure(list(name = structure(c(1L, 3L, 4L, 2L), .Label = c("adam", 
"andine", "anya", "gilang"), class = "factor"), gender = structure(c(2L, 
1L, 2L, 1L), .Label = c("female", "male"), class = "factor"), 
    fat = c(32, 27, 24, 34)), class = "data.frame", row.names = c(NA, 
-4L))
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57