我有以下数据框
X
X1 X2 X3 X4 X5
1 1 0 1 0 1
2 1 1 0 0 0
3 1 1 0 0 1
4 0 0 1 1 0
5 1 0 0 0 0
6 1 2 3 0 0
一个载体
pattern
[1] 0 1 0
X ==模式是什么意思?我无法解释以下输出。
> X == pattern
X1 X2 X3 X4 X5
1 FALSE TRUE FALSE TRUE FALSE
2 TRUE TRUE FALSE FALSE FALSE
3 FALSE FALSE TRUE TRUE FALSE
4 TRUE TRUE FALSE FALSE TRUE
5 TRUE FALSE FALSE FALSE FALSE
6 FALSE FALSE FALSE TRUE TRUE
答案 0 :(得分:2)
看起来正在发生的事情,是将X中的值与从pattern [1]开始的逐行模式进行比较。
X[1,] is compared to pattern[1],
Then X[2,] is compared to pattern[2]
Then X[3,] is compared to pattern[3]
Then X[4,] is compared to pattern[1]
Then X[5,] is compared to pattern[2]... and so on.
如下所示创建比较
X1 X2 X3 X4 X5
1 1==0 0==0 1==0 0==0 1==0 #pattern = 0
2 1==1 1==1 0==1 0==1 0==1 #pattern = 1
3 1==0 1==0 0==0 0==0 1==0 #pattern = 0
4 0==0 0==0 1==0 1==0 0==0 #pattern = 0
5 1==1 0==1 0==1 0==1 0==1 #pattern = 1
6 1==0 2==0 3==0 0==0 0==0 #pattern = 0
答案 1 :(得分:1)
模式0,1,0小于数据框的大小,因此它将被复制足够的次数,以便可以进行所有必要的比较,使其与制作矩阵相同
(M = matrix(c(0,1,0), nrow=6, ncol=5))
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 1 1 1 1 1
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 1 1 1 1 1
[6,] 0 0 0 0 0
并说X == M
答案 2 :(得分:0)
您可以将操作理解为:它将matrix
/ data.frame
转换为vector
(按列)并使用它与模式进行比较。然后将结果(按列)返回matrix
/ data.frame
。
# X == pattern is equal to:
matrix(c(as.matrix(X)) == pattern, ncol=ncol(X))
# [,1] [,2] [,3] [,4] [,5]
# [1,] FALSE TRUE FALSE TRUE FALSE
# [2,] TRUE TRUE FALSE FALSE FALSE
# [3,] FALSE FALSE TRUE TRUE FALSE
# [4,] TRUE TRUE FALSE FALSE TRUE
# [5,] TRUE FALSE FALSE FALSE FALSE
# [6,] FALSE FALSE FALSE TRUE TRUE
你可以做一些实验,如:
Y <- X[-6, ]
Y
# X1 X2 X3 X4 X5
# 1 1 0 1 0 1
# 2 1 1 0 0 0
# 3 1 1 0 0 1
# 4 0 0 1 1 0
# 5 1 0 0 0 0
Y == pattern
# X1 X2 X3 X4 X5
# 1 FALSE TRUE TRUE TRUE FALSE
# 2 TRUE FALSE TRUE FALSE TRUE
# 3 FALSE TRUE TRUE TRUE TRUE
# 4 TRUE TRUE TRUE FALSE TRUE
# 5 TRUE TRUE TRUE FALSE TRUE
matrix(c(as.matrix(Y)) == pattern, ncol=ncol(Y))
# [,1] [,2] [,3] [,4] [,5]
# [1,] FALSE TRUE TRUE TRUE FALSE
# [2,] TRUE FALSE TRUE FALSE TRUE
# [3,] FALSE TRUE TRUE TRUE TRUE
# [4,] TRUE TRUE TRUE FALSE TRUE
# [5,] TRUE TRUE TRUE FALSE TRUE
X <- structure(list(X1 = c(1L, 1L, 1L, 0L, 1L, 1L), X2 = c(0L, 1L,
1L, 0L, 0L, 2L), X3 = c(1L, 0L, 0L, 1L, 0L, 3L), X4 = c(0L, 0L,
0L, 1L, 0L, 0L), X5 = c(1L, 0L, 1L, 0L, 0L, 0L)), .Names = c("X1",
"X2", "X3", "X4", "X5"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))