如何计算值在数据框的列中出现的次数?

djq*_*djq 5 statistics r

有没有一种简单的方法来识别值在数据帧的向量或列中的次数?我基本上想要直方图的数值,但我不知道如何访问它.

# sample vector
a <- c(1,2,1,1,1,3,1,2,3,3)

#hist
hist(a)
Run Code Online (Sandbox Code Playgroud)

谢谢.

更新:

关于Dirk的建议我正在使用hist.当我知道我的所有值都是整数时,有没有比将范围指定为1.9,2.9等更好的方法?

 hist(a, breaks=c(1,1.9,2.9,3.9,4.9,5.9,6.9,7.9,8.9,9.9), plot=FALSE)$counts
Run Code Online (Sandbox Code Playgroud)

mbq*_*mbq 19

使用table功能.


Dir*_*tel 9

试试这个:

R> a <- c(1,2,1,1,1,3,1,2,3,3)
R> b <- hist(a, plot=FALSE)
R> str(b)
List of 7
 $ breaks     : num [1:5] 1 1.5 2 2.5 3
 $ counts     : int [1:4] 5 2 0 3
 $ intensities: num [1:4] 1 0.4 0 0.6
 $ density    : num [1:4] 1 0.4 0 0.6
 $ mids       : num [1:4] 1.25 1.75 2.25 2.75
 $ xname      : chr "a"
 $ equidist   : logi TRUE
 - attr(*, "class")= chr "histogram"
R> 
Run Code Online (Sandbox Code Playgroud)

R是面向对象的,大多数方法都会给你带来有意义的结果.使用它们.

  • 试试`help(hist)`. (3认同)