I am trying to make a new column that categorises one column in my data in to BMI categories, which I can then rbind in to a new, complete data frame. However, my current method only produces a numeric vector. As a result, this won't seem to bind back to my original data set.
Here is my code:
BMI_cut <- cut(alldata$BMI,
breaks = c(-Inf, 18.5, 25.0, 30.0, Inf),
labels = c("<18.50", "18.50-24.99", "25.00-29.99", ">=30.00"),
right = FALSE)
BMIbind <- rbind(sorteddata, BMI_cut)
When trying this, I get the error:
Warning messages:
1: In [<-.factor
(*tmp*
, ri, value = 2L) :
invalid factor level, NA generated
2: In [<-.factor
(*tmp*
, ri, value = 2L) :
invalid factor level, NA generated
And the result is a bind with the original data and no BMI category column. The only difference is a new row with values of <NA>, 2 and 3. I can't make sense of this.
I am a complete beginner to R. Additionally, whilst there are some packages that look like they can do this much easier, I cannot use them as this is for an assignment. Any help would be greatly appreciated.