我有以下问题。我想通过矩阵 B 中的坐标来对data.frame A 中的数据进行子集化。匹配必须在data.frame A 和矩阵 B 的坐标之间成对,例如:
lon lat
315 10.47 52.26
342 10.47 52.37
314 10.36 52.26
341 10.36 52.37
316 10.58 52.26
288 10.47 52.15
343 10.58 52.37
287 10.36 52.15
369 10.47 52.48
这些是坐标,我想用来选择行,这些行的坐标与 data.frame A 中的坐标相同。
data.frame A:
的子集structure(list(V13138 = c(-15.0545539855957, -15.0118608474731,
-14.8698711395264, -14.7185792922974, -14.5449771881104, -14.3298683166504,
-14.093412399292, -13.8265686035156, -13.5609474182129, -13.2979116439819,
-12.9872589111328, -16.1379947662354, -16.0786437988281, -16.0000076293945,
-15.9013233184814, -15.8254327774048, -15.7928791046143, -15.7776985168457,
-15.7719392776489, -15.6555442810059, -15.5040102005005, -15.299674987793,
-15.1203699111938, -14.9610414505005, -14.9183511734009, -14.8809566497803,
-14.8522186279297, -14.7452983856201, -14.6159820556641, -14.4459781646729,
-14.2792196273804, -14.1135988235474, -13.919620513916, -13.7100219726562,
-13.4647169113159, -13.2096605300903, -12.9506988525391, -12.6352672576904,
-16.0543613433838, -15.9694194793701, -15.8757200241089, -15.7634477615356,
-15.6610631942749, -15.5705652236938, -15.4900894165039, -15.4035835266113,
-15.2854766845703, -15.1539916992188, -15.0065212249756, -14.8876695632935,
-14.7782440185547, -14.7028961181641, -14.6101722717285, -14.512882232666,
-14.3619556427002, -14.2041110992432, -14.0312938690186, -13.8672027587891,
-13.7057323455811, -13.5090990066528, -13.2931470870972, -13.0507898330688,
-12.7776670455933, -12.495795249939, -12.1937398910522, -15.8203887939453,
-15.7082033157349, -15.5987091064453, -15.4917774200439, -15.390435218811,
-15.2902202606201, -15.1883487701416, -15.0730466842651, -14.9352264404297,
-14.8101224899292, -14.6887359619141, -14.5857553482056, -14.479567527771,
-14.3734302520752, -14.2455368041992, -14.1091232299805, -13.9528331756592,
-13.7913122177124, -13.6249303817749), V13139 = c(-7.07704830169678,
-7.36577892303467, -7.60899782180786, -7.85753965377808, -8.1030740737915,
-8.29149341583252, -8.45194625854492, -8.52234935760498, -8.58086585998535,
-8.62695598602295, -8.63118934631348, -3.46103024482727, -3.60997128486633,
-3.75486493110657, -3.90544772148132, -4.07107162475586, -4.2713623046875,
-4.53771257400513, -4.8378758430481, -5.18377256393433, -5.52214574813843,
-5.85517549514771, -6.14670562744141, -6.42839002609253, -6.70926380157471,
-6.97871208190918, -7.23635053634644, -7.43477869033813, -7.61982440948486,
-7.77778148651123, -7.9507007598877, -8.140061378479, -8.27810287475586,
-8.39971256256104, -8.4821662902832, -8.54337215423584, -8.59362697601318,
-8.6127290725708, -3.92732691764832, -4.10400390625, -4.28167676925659,
-4.4712872505188, -4.68312835693359, -4.91524791717529, -5.20708131790161,
-5.51510334014893, -5.85246753692627, -6.17613887786865, -6.49274349212646,
-6.75846433639526, -7.00491952896118, -7.21479940414429, -7.39973735809326,
-7.57557010650635, -7.69487333297729, -7.81140756607056, -7.91438579559326,
-8.01808547973633, -8.12346649169922, -8.21732807159424, -8.30572509765625,
-8.38486099243164, -8.45881938934326, -8.52587699890137, -8.57262134552002,
-4.34183073043823, -4.54555749893188, -4.76105737686157, -4.99724388122559,
-5.26278305053711, -5.53893136978149, -5.85093879699707, -6.16052055358887,
-6.46362257003784, -6.73704147338867, -6.99943161010742, -7.20827674865723,
-7.38761377334595, -7.53203630447388, -7.64012908935547, -7.74198341369629,
-7.82141494750977, -7.89632749557495, -7.96634721755981), V13140 = c(2.38613152503967,
2.37324142456055, 2.38662815093994, 2.38441777229309, 2.35186982154846,
2.31384658813477, 2.2728853225708, 2.23825240135193, 2.20144987106323,
2.15977454185486, 2.13386940956116, 2.9677951335907, 2.92966151237488,
2.8759753704071, 2.8214259147644, 2.76297402381897, 2.70412373542786,
2.6543300151825, 2.61105895042419, 2.60811114311218, 2.60745763778687,
2.60840320587158, 2.60350298881531, 2.59271574020386, 2.54244041442871,
2.47917294502258, 2.40393853187561, 2.35462546348572, 2.31852698326111,
2.31014728546143, 2.29225921630859, 2.26293158531189, 2.23773765563965,
2.21094441413879, 2.18223357200623, 2.15001082420349, 2.11311554908752,
2.08600211143494, 2.89936757087708, 2.89069938659668, 2.87734007835388,
2.85613536834717, 2.8233802318573, 2.78204131126404, 2.73941993713379,
2.70192885398865, 2.68653988838196, 2.66766142845154, 2.64770603179932,
2.62153196334839, 2.5882031917572, 2.53414297103882, 2.46355938911438,
2.38226866722107, 2.31638383865356, 2.26739454269409, 2.24680852890015,
2.23866200447083, 2.23337078094482, 2.22489714622498, 2.21137762069702,
2.19127559661865, 2.16362285614014, 2.13074207305908, 2.08486270904541,
2.85043382644653, 2.87193655967712, 2.88829565048218, 2.89718008041382,
2.88119888305664, 2.85316681861877, 2.81990385055542, 2.7852942943573,
2.75193023681641, 2.72042202949524, 2.6889750957489, 2.650550365448,
2.60602164268494, 2.55256152153015, 2.48241400718689, 2.40686845779419,
2.32716631889343, 2.27034878730774, 2.23567771911621), lon = structure(c(10.36,
10.47, 10.58, 10.69, 10.8, 10.91, 11.02, 11.13, 11.24, 11.35,
11.46, 8.6, 8.71, 8.82, 8.93, 9.04, 9.15, 9.26, 9.37, 9.48, 9.59,
9.7, 9.81, 9.92, 10.03, 10.14, 10.25, 10.36, 10.47, 10.58, 10.69,
10.8, 10.91, 11.02, 11.13, 11.24, 11.35, 11.46, 8.6, 8.71, 8.82,
8.93, 9.04, 9.15, 9.26, 9.37, 9.48, 9.59, 9.7, 9.81, 9.92, 10.03,
10.14, 10.25, 10.36, 10.47, 10.58, 10.69, 10.8, 10.91, 11.02,
11.13, 11.24, 11.35, 11.46, 8.6, 8.71, 8.82, 8.93, 9.04, 9.15,
9.26, 9.37, 9.48, 9.59, 9.7, 9.81, 9.92, 10.03, 10.14, 10.25,
10.36, 10.47, 10.58), .Dim = 84L), lat = structure(c(52.15, 52.15,
52.15, 52.15, 52.15, 52.15, 52.15, 52.15, 52.15, 52.15, 52.15,
52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26,
52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26,
52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26, 52.26,
52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37,
52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37,
52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37, 52.37,
52.48, 52.48, 52.48, 52.48, 52.48, 52.48, 52.48, 52.48, 52.48,
52.48, 52.48, 52.48, 52.48, 52.48, 52.48, 52.48, 52.48, 52.48,
52.48), .Dim = 84L)), .Names = c("V13138", "V13139", "V13140",
"lon", "lat"), row.names = 287:370, class = "data.frame")
矩阵B ,坐标为:
structure(list(lon = structure(c(10.47, 10.47, 10.36, 10.36,
10.58, 10.47, 10.58, 10.36, 10.47), .Dim = 9L), lat = structure(c(52.26,
52.37, 52.26, 52.37, 52.26, 52.15, 52.37, 52.15, 52.48), .Dim = 9L)), .Names = c("lon",
"lat"), out.attrs = structure(list(dim = c(27L, 23L), dimnames = structure(list(
Var1 = c("Var1= 8.60", "Var1= 8.71", "Var1= 8.82", "Var1= 8.93",
"Var1= 9.04", "Var1= 9.15", "Var1= 9.26", "Var1= 9.37", "Var1= 9.48",
"Var1= 9.59", "Var1= 9.70", "Var1= 9.81", "Var1= 9.92", "Var1=10.03",
"Var1=10.14", "Var1=10.25", "Var1=10.36", "Var1=10.47", "Var1=10.58",
"Var1=10.69", "Var1=10.80", "Var1=10.91", "Var1=11.02", "Var1=11.13",
"Var1=11.24", "Var1=11.35", "Var1=11.46"), Var2 = c("Var2=51.05",
"Var2=51.16", "Var2=51.27", "Var2=51.38", "Var2=51.49", "Var2=51.60",
"Var2=51.71", "Var2=51.82", "Var2=51.93", "Var2=52.04", "Var2=52.15",
"Var2=52.26", "Var2=52.37", "Var2=52.48", "Var2=52.59", "Var2=52.70",
"Var2=52.81", "Var2=52.92", "Var2=53.03", "Var2=53.14", "Var2=53.25",
"Var2=53.36", "Var2=53.47")), .Names = c("Var1", "Var2"))), .Names = c("dim",
"dimnames")), row.names = c(315L, 342L, 314L, 341L, 316L, 288L,
343L, 287L, 369L), class = "data.frame")
答案 0 :(得分:0)
矩阵B 必须为具有lon和lat坐标的列具有相同的列名称 data.frame A 。在给定的数据中,列名称在两个对象中都是lon和lat:
head.matrix(data.frame.A)
V13138 V13139 V13140 lon lat
287 -15.05455 -7.077048 2.386132 10.36 52.15
288 -15.01186 -7.365779 2.373241 10.47 52.15
289 -14.86987 -7.608998 2.386628 10.58 52.15
290 -14.71858 -7.857540 2.384418 10.69 52.15
291 -14.54498 -8.103074 2.351870 10.80 52.15
292 -14.32987 -8.291493 2.313847 10.91 52.15
matrix.B
lon lat
315 10.47 52.26
342 10.47 52.37
314 10.36 52.26
341 10.36 52.37
316 10.58 52.26
288 10.47 52.15
343 10.58 52.37
287 10.36 52.15
369 10.47 52.48
要通过矩阵B 中的坐标对来自 data.frame A 的数据进行子集化,只需使用以下代码:
subset.A <- merge(data.frame.A, matrix.B)
lon lat V13138 V13139 V13140
1 10.36 52.15 -15.05455 -7.077048 2.386132
2 10.36 52.26 -14.74530 -7.434779 2.354625
3 10.36 52.37 -14.36196 -7.694873 2.316384
4 10.47 52.15 -15.01186 -7.365779 2.373241
5 10.47 52.26 -14.61598 -7.619824 2.318527
6 10.47 52.37 -14.20411 -7.811408 2.267395
7 10.47 52.48 -13.79131 -7.896327 2.270349
8 10.58 52.26 -14.44598 -7.777781 2.310147
9 10.58 52.37 -14.03129 -7.914386 2.246809
这是一个非常简单的解决方案。带坐标的列将放在子集的前2列中。