我有两个变量,X和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)我想创建一个表,说明X和Y在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))我做了一个相对频率的样本,但结果是这样的:相对频率图。我认为这是错误的。你认为如何?此外,如何使用hist(x)$counts获得绝对频率和累积频率?
发布于 2018-11-11 20:24:41
我不知道你为什么要使用hist(x)。所有东西都可以使用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 y也是如此。把它们放在一起,
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结果是正确的,虽然确实有点无聊。如果你有更多的数据,与重复,它将看起来更好。
https://stackoverflow.com/questions/53252288
复制相似问题