0

I am struggling to make a barplot with two variables in R. One variable has data ranging from 0-90, and I need to split it up into 3 groups-- the data that is <5, 5-10, and >10. So that there are only 3 bars in the plot instead of 90. Here is the code I have tried to use but I can't figure out how to get this to work. The problem is in the use of the <,>, and - signs.

First I created a new variable

SVLivedPlot <- SDreal2$SVLived

And then I am trying to group all the numbers that are under 5 to be the value of 1, 5-10 to be the value of 2, and greater than 10 to be 3.

SVLivedPlot[SDreal2$SVLived == c(<5)] <- 1
SVLivedPlot[SDreal2$SVLived == c(5-10)] <- 2
SVLivedPlot[SDreal2$SVLived == c(>90)] <-3

Once I get those values changed I will use the following code to save that new variable with the correct groupings as the variable I will use in my barplot

DataFrameName$OldVariableName <- NewVariableName

Once I can get this new variable created I know how to put it in the barplot() formula to get the plot. I just need to know how to group those data! Any help would be great! Thank you!:)

1 Answers1

1

We can use cut

SDreal2$NewVar <- with(SDreal2, as.integer(cut(SVLived, 
        breaks = c(-Inf, 5, 10, 90))))
akrun
  • 874,273
  • 37
  • 540
  • 662