R 如何在没有循环的向量中,使用条件从矩阵中过滤一组值?
我有一个矩阵源(a),第一列是键,第二列是每个键的值R 如何在没有循环的向量中,使用条件从矩阵中过滤一组值?,r,matrix,R,Matrix,我有一个矩阵源(a),第一列是键,第二列是每个键的值 __0_1 22034 __1000000000000_1 34310 __1000000000000_2 38608 __1000000000_1 18829 __1000_1 38674 __11_november_1 21566 __11_plus_1 35908 __12_1
__0_1 22034
__1000000000000_1 34310
__1000000000000_2 38608
__1000000000_1 18829
__1000_1 38674
__11_november_1 21566
__11_plus_1 35908
__12_1 25784
__14_july_1 28671
__15_may_organization_1 36358
向量B是需要基于矩阵A赋值的键的子集。
B:
下面是我从矩阵A中找到B值的代码:
for (i in 1:length(B)){
rlst<-A[A[,1]==B[i],2];
}
for(1中的i:长度(B)){
rlst我假设对于B的每个值,您只期望有一个匹配。以下是您的示例数据集
A<-data.frame(
V1 = c("__0_1", "__1000000000000_1", "__1000000000000_2",
"__1000000000_1", "__1000_1", "__11_november_1", "__11_plus_1",
"__12_1", "__14_july_1", "__15_may_organization_1"),
V2 = c(22034L,
34310L, 38608L, 18829L, 38674L, 21566L, 35908L, 25784L, 28671L,
36358L)
)
B<-c("__14_july_1", "__15_may_organization_1", "__1000000000_1")
及
请注意,第一种方法保留A
中的行顺序,而第二种方法按B
的顺序返回行,这似乎最接近循环的操作顺序。使用数据。表包:
require(data.table)
A<-data.frame(
V1 = c("__0_1", "__1000000000000_1", "__1000000000000_2",
"__1000000000_1", "__1000_1", "__11_november_1", "__11_plus_1",
"__12_1", "__14_july_1", "__15_may_organization_1"),
V2 = c(22034L,
34310L, 38608L, 18829L, 38674L, 21566L, 35908L, 25784L, 28671L,
36358L)
)
B<-c("__14_july_1", "__15_may_organization_1", "__1000000000_1")
A <- data.table(A)
setkey(A,V1)
A[B]
V1 V2
1: __14_july_1 28671
2: __15_may_organization_1 36358
3: __1000000000_1 18829
require(data.table)
A或
我想你在B
的第三个条目中有一个输入错误-应该是\uu15\u可能是组织机构\u1
?另外,当你有字符和数字数据时,你确定你想在a
中使用矩阵吗?我假设数据.框架
可能更好。关于:rlst
A[A[,1] %in% B, ]
# V1 V2
# 4 __1000000000_1 18829
# 9 __14_july_1 28671
# 10 __15_may_organization_1 36358
A[match(B,A[,1]),]
# V1 V2
# 9 __14_july_1 28671
# 10 __15_may_organization_1 36358
# 4 __1000000000_1 18829
require(data.table)
A<-data.frame(
V1 = c("__0_1", "__1000000000000_1", "__1000000000000_2",
"__1000000000_1", "__1000_1", "__11_november_1", "__11_plus_1",
"__12_1", "__14_july_1", "__15_may_organization_1"),
V2 = c(22034L,
34310L, 38608L, 18829L, 38674L, 21566L, 35908L, 25784L, 28671L,
36358L)
)
B<-c("__14_july_1", "__15_may_organization_1", "__1000000000_1")
A <- data.table(A)
setkey(A,V1)
A[B]
V1 V2
1: __14_july_1 28671
2: __15_may_organization_1 36358
3: __1000000000_1 18829
library(dplyr)
left_join(data.frame(V1=B), A)
#Joining by: "V1"
# V1 V2
#1 __14_july_1 28671
#2 __15_may_organization_1 36358
#3 __1000000000_1 18829