add a column with an if statement

Hi i would like to add a column (agecat) based on the first column (age) of this data:
age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal num
1 63 1 1 145 233 1 2 150 0 2.3 3 0 6 0
2 67 1 4 160 286 0 2 108 1 1.5 2 3 3 2
3 67 1 4 120 229 0 2 129 1 2.6 2 2 7 1
4 37 1 3 130 250 0 0 187 0 3.5 3 0 3 0
5 41 0 2 130 204 0 2 172 0 1.4 1 0 3 0
6 56 1 2 120 236 0 0 178 0 0.8 1 0 3 0
7 62 0 4 140 268 0 2 160 0 3.6 3 2 3 3
8 57 0 4 120 354 0 0 163 1 0.6 1 0 3 0
9 63 1 4 130 254 0 2 147 0 1.4 2 1 7 2
10 53 1 4 140 203 1 2 155 1 3.1 3 0 7 1
11 57 1 4 140 192 0 0 148 0 0.4 2 0 6 0
12 56 0 2 140 294 0 2 153 0 1.3 2 0 3 0
13 56 1 3 130 256 1 2 142 1 0.6 2 1 6 2
14 44 1 2 120 263 0 0 173 0 0.0 1 0 7 0
15 52 1 3 172 199 1 0 162 0 0.5 1 0 7 0
16 57 1 3 150 168 0 0 174 0 1.6 1 0 3 0
17 48 1 2 110 229 0 0 168 0 1.0 3 0 7 1
18 54 1 4 140 239 0 0 160 0 1.2 1 0 3 0
19 48 0 3 130 275 0 0 139 0 0.2 1 0 3 0
20 49 1 2 130 266 0 0 171 0 0.6 1 0 3 0
21 64 1 1 110 211 0 2 144 1 1.8 2 0 3 0
22 58 0 1 150 283 1 2 162 0 1.0 1 0 3 0
23 58 1 2 120 284 0 2 160 0 1.8 2 0 3 1
24 58 1 3 132 224 0 2 173 0 3.2 1 2 7 3
25 60 1 4 130 206 0 2 132 1 2.4 2 2 7 4

Here is my code so long :

a$agecat <- if(a$age>=25&a$age<40) {

  • print("[25:40)years")
  • } else if(a$age>=40&a$age<65) {
  • print("[40:65)years")
  • } else if(a$age>=65&a$age<80) {
  • print("[65:80)years")
  • } else {
  • print(NULL)
  • }

It gives me the same answer by each number "[40:65)years". How can I change the error? I need to create a column 'agent' that categorise the age by [25:40) & [40:65) & [65:80). Please help!

Hi @22623639, welcome to RStudio Community.

You need to use the vectorized ifelse() instead.

a <- data.frame(age = c(63, 67, 67, 37, 41, 56, 62, 57, 63, 53))

print(a)
#>    age
#> 1   63
#> 2   67
#> 3   67
#> 4   37
#> 5   41
#> 6   56
#> 7   62
#> 8   57
#> 9   63
#> 10  53

a$agecat <- ifelse(a$age >= 25 & a$age < 40, 
                   yes = "25-40 years", 
                   no = ifelse(a$age >= 40 & a$age < 65, 
                               yes = "40-65 years", 
                               no = ifelse(a$age >= 65 & a$age < 80, 
                                           yes = "65-80 years", 
                                           no = NA)))

print(a)
#>    age      agecat
#> 1   63 40-65 years
#> 2   67 65-80 years
#> 3   67 65-80 years
#> 4   37 25-40 years
#> 5   41 40-65 years
#> 6   56 40-65 years
#> 7   62 40-65 years
#> 8   57 40-65 years
#> 9   63 40-65 years
#> 10  53 40-65 years

Created on 2020-04-15 by the reprex package (v0.3.0)

1 Like

Thank you so much! Means the world that you could help me! Have a nice day.

1 Like

Glad to help. If I solved your problem, please consider marking my post as a solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.