I have the following frequency table of intervals:
| Height (inches) | Class Mark | Frequency |
|---|---|---|
| 0–10 | 5 | 5 |
| 10–20 | 15 | 18 |
| 20–30 | 25 | 12 |
| 30–40 | 35 | 6 |
| 40–50 | 45 | 9 |
For which I tried to create a histogram using the following R code:
# Example data frame for a continuous frequency table
df <- data.frame(
ClassInterval = c("[0, 10)", "[10, 20)", "[20, 30)", "[30, 40)", "[40, 50)"),
Frequency = c(5, 8, 12, 6, 9)
)
# Extract numeric values from class intervals
class_intervals <- gsub("\\[|\\)|\\(|\\]", "", df$ClassInterval)
class_midpoints <- sapply(strsplit(class_intervals, ","), function(x) mean(as.numeric(x)))
# Manually specify breaks based on unique values in ClassInterval
breaks <- as.numeric(gsub("\\[|\\)|\\(|\\]", "", unique(df$ClassInterval)))
# Create a histogram
hist(class_midpoints, breaks = breaks, weights = df$Frequency, col = "skyblue", xlab = "Value", main = "Continuous Frequency Histogram")
However, the command that creates breaks seems to generate only NAs, and consequently the hist gives me this error:
Error in hist.default(class_midpoints, breaks = breaks, weights = df$Frequency, :
negative length vectors are not allowed
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(h[h > 0]) : no non-missing arguments to min; returning Inf
Can someone please tell me what I'm doing wrong? And how to fix the problem? Thank you very much in advance.
Given your toy data, we can create a bar plot where we display the weighted frequencies against the point ranges ("Class Intervals"):
Created on 2023-11-22 with reprex v2.0.2