I have a table called tableOne in R like this:
idNum binaryVariable salePrice
2 1 55.56
4 0 88.33
15 0 4.45
87 1 35.77
... ... ...
I’d like to take the values produced from: summary(tableOne$salePrice) to create four quartiles by salePrice. I’d then like to create a column tableOne$quartile with which quartile each rows salePrice is in. It would look like:
idNum binaryVariable salePrice quartile
2 1 55.56 3
4 0 88.33 4
15 0 4.45 1
87 1 35.77 2
... ... ... ...
Any suggestions?
This should do it:
…Some details:
The
withinfunction is great for calculating new columns. You don’t have to refer to columns astableOne$salesPriceetc.The
quantilefunction calculates the quantiles (or in your case, quartiles).0:4/4evaluates toc(0, 0.25, 0.50, 0.75, 1).Finally the
cutfunction splits your data into those quartiles. But you get afactorwith weird names, soas.integerturns it into groups1,2,3,4.Try
?withinetc to learn more about the functions mentioned here…