0

I have a dataframe in R and I am trying to add a new column which would use info. from one of the other columns.
Specifically, I have ages of an organism (in a column) and I am trying to group them like this in the new column.

age1-3: infant
age 4-6: toddler etc.
This is in R-studio.

I have tried using some if else statements but they just do not work.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
  • 5
    Welcome to SO! When you place a question try to add a minimum content: input sample, expected output sample, what did you try, research and where are you stuck. So leave a question, show us your code, what did you try and your research. – David García Bodego Oct 27 '19 at 03:10
  • 1
    https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ben Oct 27 '19 at 03:12
  • I assume you know that all the columns in a data frame are the same length? – Gray Oct 27 '19 at 04:07

2 Answers2

0

Using ifelse a base-r solution

df$Category <- ifelse(df$Age<=3, "Infant", ifelse(df$Age<=6, "Toddler", ifelse(df$Age<=13, "Adolescence", ifelse(df$Age<=19, "Teenage", ifelse(df$Age<=59, "Adult", "Senior")))))
# -------------------------------------------------------------------------
# df
#     Id Age    Category
# 1   1   1      Infant
# 2   2   5     Toddler
# 3   3  10 Adolescence
# 4   4  12 Adolescence
# 5   5   3      Infant
# 6   6  23       Adult
# 7   7  55       Adult
# 8   8  42       Adult
# 9   9  92      Senior
# 10 10  78      Senior
# 11 11  33       Adult
# 12 12  44       Adult
# 13 13  25       Adult
# 14 14  13 Adolescence
# 15 15  10 Adolescence
# 16 16  19     Teenage
# 17 17  45       Adult
# 18 18  39       Adult

Using dplyr mutate and case_when

library(dplyr)
df <- df %>%
  mutate(Category =
  case_when(
    Age <=3 ~ "Infant", 
    Age <=6 ~ "Toddler",
    Age <=13 ~ "Adolesence",
    Age <=19 ~ "Teenage",
    Age <=59 ~ "Adult",
    TRUE ~ "Senior"
    
  )
)
# -------------------------------------------------------------------------
# df
#     Id Age   Category
# 1   1   1     Infant
# 2   2   5    Toddler
# 3   3  10 Adolesence
# 4   4  12 Adolesence
# 5   5   3     Infant
# 6   6  23      Adult
# 7   7  55      Adult
# 8   8  42      Adult
# 9   9  92     Senior
# 10 10  78     Senior
# 11 11  33      Adult
# 12 12  44      Adult
# 13 13  25      Adult
# 14 14  13 Adolesence
# 15 15  10 Adolesence
# 16 16  19    Teenage
# 17 17  45      Adult
# 18 18  39      Adult
# 

See the comments above as well as you need to provide a reproducible example to get a solution target towards your specific problem. Providing a reproducible example guarantees a quick response.

Sample Data

df <- data.frame(Id = seq(1,18,1), 
                 Age = c(1,5,10, 12, 3, 23, 55, 42, 92, 78, 33, 44, 25, 13, 10, 19, 45, 39))

Hope this helps.

Community
  • 1
  • 1
deepseefan
  • 3,701
  • 3
  • 18
  • 31
0

below a working sample with a dedicated function to determine the categories:

#define sample
organism <- c('cat','dog','horse','elephant','snake','ant','bear')
ages <- c(1,4,7,2,5,2,1)
df <- data.frame(organism,ages)

#define classification function
classify_age <- function(age) {

  if (age >= 1 & age <= 3)
    {return ("infant")}
  else if (age >= 4 & age <= 6)
    {return ("toddler")}
  else
    {return("adult")}

}

#create the new data frame column
df$category <- lapply(df$ages, classify_age)

#print data frame
df
Alessio
  • 910
  • 7
  • 16