如何通过与其他向量不同的值计算一个向量中的唯一数字的数量

时间:2014-07-19 02:09:50

标签: r aggregate data.table apply

我有两个载体

x <- c(1,5,3,2,NA, 4,1,2,3,4, 10,5,2,10,12)
y <- c(1,1,2,NA,2, 3,3,1,4,NA, 4,5,5,4,4)

我需要为向量Y中的每个值计算向量X中的1和3的数量。

例如,输出应采用以下格式:

y x=1 x=3 x=others(not x=1 and not x=3)
1  1   0    2
2  0   1    0
3  1   0    1
4  0   1    3
5  0   0    2

2 个答案:

答案 0 :(得分:3)

以下是一些选项:

factor + table

您可以使用factor,然后使用table,如下所示:

x <- c(1,5,3,2,NA, 4,1,2,3,4, 10,5,2,10,12)
y <- c(1,1,2,NA,2, 3,3,1,4,NA, 4,5,5,4,4)
X <- factor(x)
levels(X) <- list("x = 1" = 1, "x = 3" = 3, "others" = setdiff(x, c(1, 3)))
table(y, X)
#    X
# y   x = 1 x = 3 others
#   1     1     0      2
#   2     0     1      0
#   3     1     0      1
#   4     0     1      3
#   5     0     0      2

ifelse

这也可以通过ifelse

完成
X <- `ifelse(x == 1, "x = 1", ifelse(x == 3, "x = 3", "others")`.
table(y, X)
data.table(+ ifelse + dcast.data.table
library(data.table)
library(reshape2)

DT <- data.table(y, x)
DT[, X := ifelse(x == 1, "x = 1", ifelse(x == 3, "x = 3", "others"))]
dcast.data.table(DT, y ~ X, value.var = "X", fun.aggregate = length)
#     y NA others x = 1 x = 3
# 1: NA  0      2     0     0
# 2:  1  0      2     1     0
# 3:  2  1      0     0     1
# 4:  3  0      1     1     0
# 5:  4  0      3     0     1
# 6:  5  0      2     0     0

答案 1 :(得分:0)

使用table

的另一种方法
x[!is.na(x) &  !x %in% c(1,3)] <- "Others"
res <- table(y,x)
dimnames(res)$x <- c("x = 1", "x = 3", "Others")
res
#       x
#y   x = 1 x = 3 Others
 # 1     1     0      2
 # 2     0     1      0
 # 3     1     0      1
 # 4     0     1      3
 # 5     0     0      2