I have a dataframe:
X65L X65L.1 X65L.2 X67L X67L.1 X65L.3
[1,] 0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
[2,] 0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
[3,] 0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
[4,] 0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
[5,] 0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
[6,] 0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
[7,] 0.0067 0.0071 0.0067 0.0067 0.0071 0.0067
[8,] 0.0067 0.0071 0.0067 0.0067 0.0071 0.0067
[9,] 0.0067 0.0071 0.0067 0.0067 0.0071 0.0071
[10,] 0.0067 0.0084 0.0067 0.0067 0.0084 0.0071
[11,] 0.0067 0.0084 0.0067 0.0067 0.0084 0.0084
[12,] 0.0067 0.0084 0.0067 0.0067 0.0084 0.0084
I want to count the frequency of each unique element in a column and get an output like this:
6 3 6 0 0 6
6 3 6 12 6 2
0 3 0 0 3 2
0 3 0 0 3 2
The MATLAB equivalent is:
[m1 n1]=hist(s,unique(s));
I would like to know, how this could be done in R.
You can try the code below
apply(mat, 2, function(x) table(factor(x, levels = unique(c(mat)))))
which gives
X65L X65L.1 X65L.2 X67L X67L.1 X65L.3
0.0065 6 3 6 0 0 6
0.0067 6 3 6 12 6 2
0.0071 0 3 0 0 3 2
0.0084 0 3 0 0 3 2
Data
> dput(mat)
structure(c(0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0067,
0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0065, 0.0065, 0.0065,
0.0067, 0.0067, 0.0067, 0.0071, 0.0071, 0.0071, 0.0084, 0.0084,
0.0084, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0067,
0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067,
0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067,
0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0071,
0.0071, 0.0071, 0.0084, 0.0084, 0.0084, 0.0065, 0.0065, 0.0065,
0.0065, 0.0065, 0.0065, 0.0067, 0.0067, 0.0071, 0.0071, 0.0084,
0.0084), .Dim = c(12L, 6L), .Dimnames = list(NULL, c("X65L",
"X65L.1", "X65L.2", "X67L", "X67L.1", "X65L.3")))