Reputation: 39
I have two variables, X and Y:
x <- c(1.18,1.42,0.69,0.88,1.69,1.09,1.53,1.02,1.19,1.32)
y <- c(1.72,1.42,1.69,0.79,1.79,0.77,1.44,1.29,1.96,0.99)
I would like to create a table of the absolute, relative and cumulative frequencies of both X and Y in R
plot(table(x)/length(x), type ="h", ylab = "Relative Frequency", xlim = c(0.6,1.8))
plot(table(y)/length(y), type ="h", ylab = "Relative Frequency", xlim = c(0.6,1.8))
I did a sample of the relative frequency but it came out like this: plot of the relative frequency. I think it is wrong. What do you think? Also, how can I use hist(x)$counts
to obtain the absolute and cumulative frequencies?
Upvotes: 2
Views: 4544
Reputation: 48241
I'm not sure why you wish to use hist(x)
. Everything can be obtained using table
:
# Absolute frequencies
table(x)
# x
# 0.69 0.88 1.02 1.09 1.18 1.19 1.32 1.42 1.53 1.69
# 1 1 1 1 1 1 1 1 1 1
# Relative frequencies
table(x) / length(x)
# x
# 0.69 0.88 1.02 1.09 1.18 1.19 1.32 1.42 1.53 1.69
# 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
# Cumulative frequencies
cumsum(table(x))
# 0.69 0.88 1.02 1.09 1.18 1.19 1.32 1.42 1.53 1.69
# 1 2 3 4 5 6 7 8 9 10
and the same for y
. As to put them together,
rbind(Absolute = table(x),
Relative = table(x) / length(x),
Cumulative = cumsum(table(x)))
# 0.69 0.88 1.02 1.09 1.18 1.19 1.32 1.42 1.53 1.69
# Absolute 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
# Relative 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
# Cumulative 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
The results are correct, although indeed somewhat boring. If you have more data, with repetitions, it will look better.
Upvotes: 3