0

So I have come up with this code so far but have now run into errors getting spit out back at me. I am trying to separate this dataset into weight groups listed <60, 60-70, >70 and then add it back to my data frame. I am a beginner too, so I think I did something wrong here.

runif(50, 55, 80)
rnorm(50, 176, 6)
runif(50, 65, 100)
DF <- data.frame(Height = rnorm(50, 170, 5),
                 Weight = rnorm(50, 55, 5))

weight.group = factor(NA, levels = c("0-60", "61-70", "71+"))
idx = which(rnorm <= 60)
weight.group[idx] = rep("0-60", length(idx))
idx = which(weight = 61 - 70)
weight.group[idx] = rep("61-70", length(idx))
idx = which(weight >= 71)
weight.group[idx] = rep("71+", length(idx))
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Looks like you accidentally deleted the code you are referrng to in your question. I undid that for you. Please leave the questions complete and answerable, even if you got an answer. – Yunnosch Sep 26 '21 at 20:55

1 Answers1

1

Tidyverse

library(dplyr)

DF %>% 
  mutate(weight_group  = cut(
           x = Weight,
           breaks = c(0,60,70,Inf),
           include.lowest = TRUE,
           labels = c("0-60", "61-70", "71+")
   )
  )

Base

DF$weight_group <- 
  cut(
    x = DF$Weight,
    breaks = c(0,60,70,Inf),
    include.lowest = TRUE,
    labels = c("0-60", "61-70", "71+")
  )

Result

     Height   Weight weight_group
1  166.0988 53.31766         0-60
2  171.2802 68.57363        61-70
3  164.1006 54.34834         0-60
4  173.4387 61.23054        61-70
5  167.6080 63.13028        61-70
...
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32