矩阵计数表?

时间:2017-04-15 15:21:09

标签: r

从我的矩阵看起来像:

structure(list(1.5, 1.5, 1.5, 2, 1.5, 1.5, NA, NA, NA, NA, 5, 
15, 15, 1.5, 15, 15, 15, 15, 15, 1.5, 5, 15, 15, 1.5, 15, 
15, 15, 15, 15, 1), .Dim = c(10L, 3L), .Dimnames = list(c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"), NULL))

我想得到每个级别的计数表。类似的东西:

column level counts
1      1.5    5
1      2      1
2      1.5    2
2      5      1
2      15     7
3      1      1
3      1.5    1
3      5      1
3      15     7

我该怎么做?

我不确定我追求的结构名称,这可能解释了为什么我找不到我想要的东西是不成功的。对不起。

3 个答案:

答案 0 :(得分:2)

这使用dplyr进行分组,meltresshape2转换为从宽列格式转换为所有格式。这使用数据框 - 如果您将来使用dput提供数据,则可以使用您拥有的确切结构。

m <- read.table(text="
[,1] [,2] [,3]
1  1.5  5    5   
2  1.5  15   15  
3  1.5  15   15  
4  2    1.5  1.5 
5  1.5  15   15  
6  1.5  15   15  
7  NA   15   15  
8  NA   15   15  
9  NA   15   15  
10 NA   1.5  1 
", header=TRUE) 

library(reshape2)
library(dplyr)

melt(m, na.rm = TRUE) %>%
  group_by(variable, value) %>%
  summarize(n = n())

#   variable value     n
#   <fctr> <dbl> <int>
# 1    X..1.   1.5     5
# 2    X..1.   2.0     1
# 3    X..2.   1.5     2
# 4    X..2.   5.0     1
# 5    X..2.  15.0     7
# 6    X..3.   1.0     1
# 7    X..3.   1.5     1
# 8    X..3.   5.0     1
# 9    X..3.  15.0     7

答案 1 :(得分:2)

我冒昧地回答了我自己的问题。它完全基于@ epi99的答案,@ Pradeep的评论让我明白我的数据有些奇怪。它可能远不是最有效的,但我认为像我这样的R新手更容易理解。

它使用reshape2dplyr个包。如果尚未安装,请安装它们:

install.packages ("reshape2")
install.packages ("dplyr")

然后:

# Import the libraries
library (reshape2) 
library (dplyr) 

# Import the sample data
test_map <- structure(list(1.5, 1.5, 1.5, 2, 1.5, 1.5, NA, NA, NA, NA, 5, 
           15, 15, 1.5, 15, 15, 15, 15, 15, 1.5, 5, 15, 15, 1.5, 15, 
           15, 15, 15, 15, 1), .Dim = c(10L, 3L), .Dimnames = list(c("1", 
           "2", "3", "4", "5", "6", "7", "8", "9", "10"), NULL))

# test_map is a matrix of lists, which does not work well.
# I'm first transforming it into a matrix where each column is numerics. 
# (that's probably not necessary in most cases)
testmap2 <- apply (test_map, 2, as.numeric)

# then melt (reshape2), group_by and count (dplyr)
test_melted <- melt (testmap2, na.rm = TRUE)
test_grouped <- group_by (melted, Var2, value)
test_counted <- count (grouped)

结果:

> test_counted

Source: local data frame [9 x 3]
Groups: Var2 [?]

   Var2 value     n
  <int> <dbl> <int>
1     1   1.5     5
2     1   2.0     1
3     2   1.5     2
4     2   5.0     1
5     2  15.0     7
6     3   1.0     1
7     3   1.5     1
8     3   5.0     1
9     3  15.0     7

答案 2 :(得分:1)

您还可以使用data.table

library(data.table)

编辑:在OP给出结构后,我更改了代码。

mtable <- apply(test_map,2,unlist)
mtable <- data.table(mtable,seq=1:nrow(m)) ##converting to data.table with a dummy key to melt
names(mtable) = c('a','b','c','seq') ##changing names

mfnew <- melt(mtable, id=('seq'))[,2:3,with=T] ##meting the data basis the dummy key so that the data could be gathered in one column
mfnew
setkeyv(mfnew,c("variable","value")) ##optional

final <- mfnew[,list(cnt=.N),by=list(variable,value)][,2:3,with=T]
final
final[!is.na(final$value),]**Output:**

    > final[!is.na(final$value),]
   value cnt
1:   1.5   5
2:   2.0   1
3:   1.5   2
4:   5.0   1
5:  15.0   7
6:   1.0   1
7:   1.5   1
8:   5.0   1
9:  15.0   7