Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

statistics - Even spacing of a numeric vector in R-Markdown

I am currently in an intermediate statistics class. I ran into a problem in my homework assignment and it is as follows:

From the assignment: "Create a vector x with these 25 observations of BMI for patients with cystic fibrosis...

68, 65, 64, 67, 93, 68, 89, 69, 67, 68, 89, 90, 93, 93, 66, 70, 70, 92, 69, 72, 86, 86, 97, 71, 95

The parts I would like help with:

a. Create a histogram going from 60-100 with breaks at 60, 65, 70, 75, 80, 85, 90, 95, 100."

b. Compute the percentage of observations that fall into the following classes:

  1. 60-65
  2. 65-70
  3. 70-75
  4. 75-80
  5. 80-85
  6. 85-90
  7. 90-95
  8. 95-100 total 100%

I created an object named cyst_fibro_sample to contain the observations. The assignment instructs us to use R to compute some basic statistics such as five number summaries, interquartile range, and standard deviation. I am using the built in hist() function as I have no variables and only observations to plot and I don't know how to plot the observations using ggplot()

hist(x, breaks = 60,65,70...) I would like to avoid doing punching in all the values by hand as this is grossly inefficient. However, I can't seem the vector operation for even spacing.

I would like for that argument to be hist(x, breaks = c(60:100, increasing by = 5)

For part b of the assignment, I think we are being asked to compute the density of each category of observation but I am not sure.

Any help is appreciated!

question from:https://stackoverflow.com/questions/65929473/even-spacing-of-a-numeric-vector-in-r-markdown

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've used the cars dataset here, but by using a function to detect the bounds, we can generate a vector of breaks/bins using seq to produce the histogram, and calculate the density using cut.

data <- cars

make_bins <- function(data, m = 5) {
  rounded <- round(data/m) * m
  bins <- seq(min(rounded), max(rounded), by = m)
  return(bins)
}

bins <- make_bins(data$dist)

hist(data$dist, bins)

data$bins <- cut(data$dist, bins, include.lowest = TRUE)
dens <- table(data$bins)/nrow(data)
print(dens)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...