我有更大的数据集如下:
l = 9
loc <- c(paste('Loc', 1:l, sep = ''))
vloc <- c(rep(loc, each=2))
qi <- c( 13, 12, 27, 20, 16, 18, 14, 17, 15, 22, 21, 26,12, 14, 11,
18, 8, 24 )
afreq <- c( 0.308, 0.4, 0.041, 0.5, 0.125, 0.5,0.139, 0.2, 0.219, 0.2,0.176,
0.33,0.358, 0.4, 0.274, 0.5, 0.173, 0.15)
loctab <- data.frame(vloc = vloc, qi = qi, afreq = afreq)
loctab
vloc qi afreq
1 Loc1 13 0.308
2 Loc1 12 0.400
3 Loc2 27 0.041
4 Loc2 20 0.500
5 Loc3 16 0.125
6 Loc3 18 0.500
7 Loc4 14 0.139
8 Loc4 17 0.200
9 Loc5 15 0.219
10 Loc5 22 0.200
11 Loc6 21 0.176
12 Loc6 26 0.330
13 Loc7 12 0.358
14 Loc7 14 0.400
15 Loc8 11 0.274
16 Loc8 18 0.500
17 Loc9 8 0.173
18 Loc9 24 0.150
较小的数据集如下:
ex1 <-data.frame (loc, qi = c(13, 27, 16, 14, 15, 21, 12, 11, 8)
ex1
loc qi
1 Loc1 13
2 Loc2 27
3 Loc3 16
4 Loc4 14
5 Loc5 15
6 Loc6 21
7 Loc7 12
8 Loc8 11
9 Loc9 8
对于每个loc,我需要匹配ex1(small)和loctab(big)之间的qi值并创建一个newtable。
我尝试了以下但未提供正确答案。
nloct <- loctab[loctab$qi %in% ex1$qit, ]
预期输出
nloct <- data.frame (loc, qi = c(13, 27, 16, 14, 15, 21, 12, 11, 8),
afreq = c( 0.308, 0.041, 0.125, 0.139, 0.219,0.176, 0.358, 0.274, 0.173))
loc qi afreq
1 Loc1 13 0.308
2 Loc2 27 0.041
3 Loc3 16 0.125
4 Loc4 14 0.139
5 Loc5 15 0.219
6 Loc6 21 0.176
7 Loc7 12 0.358
8 Loc8 11 0.274
9 Loc9 8 0.173
答案 0 :(得分:3)
您正在寻找某种对merge()
的电话:
## e.g. :
## merge(ex1, loctab)[c(2,1,4)]
## OR
merge(ex1, loctab, by.x=c("loc", "qi"), by.y=c("vloc", "qi"))
loc qi afreq
1 Loc1 13 0.308
2 Loc2 27 0.041
3 Loc3 16 0.125
4 Loc4 14 0.139
5 Loc5 15 0.219
6 Loc6 21 0.176
7 Loc7 12 0.358
8 Loc8 11 0.274
9 Loc9 8 0.173
答案 1 :(得分:0)
merge(loctab,ex1,by.x=c('vloc','qi'),by.y=c('loc','qi'))
但如果你将列'vloc'和'loc'命名为同样的东西,你可以说:
merge(loctab,ex1)