我不想用零或其他东西填充矩阵。我想知道如何处理这些问题
data<- structure(c(79L, 106L, 156L, 194L, 248L, 248L, 248L, 266L, 272L,
79L, 106L, 125L, 156L, 156L, 156L, 156L, 156L, 194L, 79L, 156L,
156L, 156L, 156L, 156L, 156L, 156L, 156L, 79L, 248L, 393L, 674L,
2447L, NA, NA, NA, NA, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(9L, 6L), .Dimnames = list(
NULL, c("a", "b", "c", "d",
"e", "f")))
预期输出
a b c d e f
[1,]248(3) 156(3) 156(8) 21(9)
[2,] 248(2)
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
预期输出
[1,] 79(4)
[2,] 106(2)
[3,] 156(2)
[4,] 156(2)
[5,] 156(2)
[6,] 156(2)
[7,] 156(2)
[8,] 156(2)
[9,]
预期产出
a b c d e f
[1,] 79 79 79 79 21
[2,] 106 106 156 248
[3,] 156 125 393
[4,] 194 156 674
[5,] 248 194 2447
[6,] 266
[7,] 272
[8,]
[9,]
预期产出
21(9) 156(8) 248(3) 156(3) 248(2)
答案 0 :(得分:2)
关于突出显示矩阵中的数字出现次数,不会:
table(data)
够了吗? 对于多次出现,您可以这样做:
table(data)[table(data) > 1]
然后,如果您希望评估行和/或列的语句,您可以这样做:
lstRes <- list()
for (i in 1:dim(data)[1]) {
lstRes[[i]] <-table(data[i,])[table(data[i,]) > 1]
}
到达data.frame
:
lstRes <- list()
for (i in 1:dim(data)[1]) {
lstRes[[i]] <- as.matrix(table(data[i,])[table(data[i,]) > 1])
}
Reduce(rbind, lstRes)
答案 1 :(得分:2)
# this gives you min, median, mean, max of each column
summary(data)
# this gives you which number are repeated
data[duplicated(data),]
# gives you how many times each elemnt appears in the data
as.data.frame(sort(table(data)))
# you can count how many unique values are in each columns and rows, respectively
apply(data, 2 function(x)length(unique(x)))
apply(data, 1, function(x)length(unique(x)))
# this also give you a logical idea of duplicated elements
apply(data,2,duplicated)
# if you want to see whether you have any duplicated row (it takes into acount all elements)
duplicated(data)