提取具有相同前7个字符的行和列名称的矩阵中的值。

huangapple go评论53阅读模式
英文:

How do I extract the values in a matrix with same first 7 characters in row and column name

问题

I will provide the translated code portion as requested:

d <- as.data.frame.table(mre)
d <- transform(d, Freq = as.numeric(Freq))
out <- subset(d, Var1 == sub("\\d{7,}", "", Var2),
              select = c(Var1, Var2, Freq))

If you have any further requests or need assistance with this code, please feel free to ask.

英文:

Assuming I have multiple rectangles with one point on each corner and I have created a matrix for distance from all the points to all other points. However, I only need distance points in each rectangle. For example, only when the first 7 characters of the row and column are same. That should give me 6 unique values (considering no two points are on same distance from each other)
I have added a picture to understand the structure
提取具有相同前7个字符的行和列名称的矩阵中的值。

Here is a small sample data

structure(c(0, 364.930299211309, 604.442307105484, 410.991769186984, 
16790.711415192, 16855.1485866349, 364.930299211309, 0, 366.000060144176, 
551.439334796924, 16578.9485598795, 16644.4631060393, 604.442307105484, 
366.000060144176, 0, 507.181333592942, 16223.1121382276, 16288.3157507677, 
410.991769186984, 551.439334796924, 507.181333592942, 0, 16461.5424525418, 
16525.072234626, 16790.711415192, 16578.9485598795, 16223.1121382276, 
16461.5424525418, 0, 88.797749793446, 16855.1485866349, 16644.4631060393, 
16288.3157507677, 16525.072234626, 88.797749793446, 0, 16780.7325183588, 
16569.9453695848, 16213.8248878271, 16450.7578628425, 55.5123088437835, 
74.6879092649611, 16685.4461612851, 16474.3661467207, 16118.3271996141, 
16355.736578168, 111.65891296113, 171.334534364881, 8278.91449042027, 
8307.97025914842, 8660.79734335381, 8689.09430512198, 24176.4642838139, 
24251.0451886659, 8291.2167120843, 8310.57629472996, 8660.74801095243, 
8701.92923897152, 24103.2381070123, 24178.2321727744, 8194.37525889284, 
8213.53053663935, 8563.67862136739, 8605.09994070435, 24011.5124211919, 
24086.4438705306, 8139.1222615634, 8166.56413601811, 8519.01225073755, 
8549.41961746109, 24032.0737743933, 24106.6227859227, 116835.831624242, 
117151.592151693, 117098.246896166, 116627.69115306, 119717.328274311, 
119673.340153011, 116894.895528406, 117210.612188378, 117157.179801737, 
116686.669781877, 119771.907758699, 119727.896450559, 116981.185319202, 
117296.894383494, 117243.446367649, 116772.944642533, 119856.686781357, 
119812.664655996, 116905.156839711, 117220.925401085, 117167.595530678, 
116697.031812041, 119786.70021518, 119742.709163057, 137548.21551912, 
137869.911212853, 137828.552342883, 137352.012529264, 140804.647343423, 
140761.585805883, 137432.061451682, 137753.756916661, 137712.398160743, 
137235.85819163, 140689.277368248, 140646.223285041, 137335.716364217, 
137657.466801762, 137616.224486427, 137139.628763376, 140598.794003302, 
140555.770271277, 137450.620031593, 137772.369381656, 137731.124181241, 
137254.52993692, 140712.796932126, 140669.765239517, 308493.270889503, 
308238.693560841, 307900.923543467, 308200.975526817, 291887.614524464, 
291833.363916705, 308450.712928682, 308196.116928348, 307858.356983648, 
308158.438236869, 291845.240657898, 291790.995444887, 308417.890950377, 
308163.241595123, 307825.510454734, 308125.67526815, 291812.941943133, 
291758.711960823, 308442.306574476, 308187.612662545, 307849.905596447, 
308150.140146077, 291837.793215765, 291783.575846376, 304625.431712835, 
304395.599949664, 304046.435165169, 304308.153381379, 287860.431950347, 
287799.701040423, 304673.973526676, 304444.182131566, 304095.001715416, 
304356.657954127, 287908.837619173, 287848.096909704, 304822.432549619, 
304592.531454456, 304243.393566859, 304505.217989528, 288057.665696087, 
287996.951387891, 304863.043676073, 304633.15006293, 304284.009277822, 
304545.822185079, 288098.251238621, 288037.535086693, 304229.780232868, 
304018.12715714, 303662.778058715, 303896.781648811, 287439.750549196, 
287374.866196844, 304282.703320851, 304070.974937302, 303715.648082599, 
303949.765749462, 287492.634791487, 287427.766691918, 304290.887730087, 
304079.081548834, 303723.77769689, 303958.013255205, 287500.780255042, 
287435.928954353, 304246.05266861, 304034.150823593, 303678.875290813, 
303913.255866132, 287455.898956235, 287391.068324819, 149662.503948563, 
150011.728802967, 150042.307396971, 149540.990756408, 155962.780991847, 
155934.403757724, 149596.404612597, 149945.651129832, 149976.304508479, 
149474.972051766, 155900.141423918, 155871.78427269, 149563.291094581, 
149912.570596444, 149943.337681537, 149441.981250351, 155871.951279968, 
155843.621025952, 149617.906412949, 149967.168569913, 149997.875706539, 
149496.531927463, 155923.785329008, 155895.438885628, 109612.07253716, 
109915.504641077, 109839.223819728, 109381.688022902, 111519.395232088, 
111471.383345024, 294780.162151632, 294555.319300683, 294204.28748574, 
294458.373700857, 278001.210293242, 277939.298074975, 109537.373980891, 
109840.68990978, 109764.204901048, 109306.79423011, 111436.077646899, 
111388.031630041, 109617.459258599, 109920.633554169, 109843.89845903, 
109386.641481509, 111503.738623932, 111455.633867881, 294881.360522429, 
294656.616853575, 294305.549293018, 294559.483787008, 278102.186243936, 
278040.250684806, 109734.110651157, 110037.441344702, 109960.980718578, 
109503.555333472, 111631.804537271, 111583.744066489, 294671.287225042, 
294446.632888291, 294095.533143134, 294349.331266577, 277891.91550314, 
277829.959124555, 294735.945339949, 294511.393087673, 294160.256648029, 
294413.898667183, 277956.349111251, 277894.368762537, 520265.01212354, 
519900.112715735, 519770.092670342, 520273.255807048, 510762.350605606, 
510777.60202084, 519731.632568247, 519366.72990619, 519236.468890384, 
519739.587060819, 510219.328302977, 510234.519452567, 520067.352872377, 
519702.455109509, 519572.552157942, 520075.737072445, 510569.538406276, 
510584.821179342, 519743.107039358, 519378.208494155, 519248.250320994, 
519751.424992716, 510243.15356859, 510258.423458149, 521650.938282182, 
521286.200804094, 521162.878556497, 521667.188301624, 512421.832315294, 
512438.809791122, 522027.323761335, 521662.567899423, 521538.698011763, 
522042.921462464, 512775.617437653, 512792.452290484, 522404.680017575, 
522039.930356359, 521916.248123095, 522420.501272295, 513160.535777932, 
513177.41720843, 522424.649735685, 522059.910717759, 521936.54345096, 
522440.846122743, 513193.419686588, 513210.382054801, 277398.710410213, 
277125.253615472, 277250.157847496, 277672.943023385, 279497.363373899, 
279567.789497135, 276810.260205618, 276536.723616141, 276661.514963087, 
277084.391876995, 278904.477073317, 278974.892294319, 276708.118962939, 
276434.375356784, 276558.871705227, 276981.988256785, 278788.29896639, 
278858.669417802, 276870.904553846, 276597.07382995, 276721.445651372, 
277144.663269572, 278944.808135859, 279015.15733577, 275201.156925613, 
274920.137862327, 275034.014639639, 275465.504722393, 276774.039265969, 
276842.748783494, 275090.125964583, 274808.838162688, 274922.313104215, 
275354.110453953, 276643.971671941, 276712.618057966, 274915.280929442, 
274634.262664382, 274748.141016786, 275179.62985262, 276488.737999414, 
276557.451210162, 274919.615413661, 274638.965742297, 274753.39427793, 
275184.461266036, 276519.402028069, 276588.20333444, 272393.859577198, 
272115.212288446, 272232.610058647, 272661.377616142, 274140.374430648, 
274209.679433557, 272823.499297937, 272544.721570946, 272661.926731667, 
273090.844638155, 274560.015743794, 274629.284987979, 272863.557367621, 
272585.37965681, 272703.466742037, 273131.695190808, 274642.305122298, 
274711.71374702, 272390.614727683, 272112.552780692, 272230.810281142, 
272658.90505137, 274178.376804486, 274247.818101383, 21368.3284627434, 
21307.3663113006, 21622.8598992783, 21773.5335591509, 35161.0534392998, 
35245.695275641, 21443.5474321524, 21382.8039112002, 21698.4012300959, 
21848.7953938805, 35236.3746407676, 35321.0263871004, 21438.6645227168, 
21377.014257332, 21692.1468005365, 21843.7403892556, 35213.2353239803, 
35297.9242851801, 21384.8866172282, 21322.72257453, 21637.5964385716, 
21789.8626979581, 35152.0693816675, 35236.766365204), dim = c(6L, 
68L), dimnames = list(c(&quot;BYWR110A&quot;, &quot;BYWR110B&quot;, &quot;BYWR110C&quot;, &quot;BYWR110D&quot;, 
&quot;BYWR111A&quot;, &quot;BYWR111B&quot;), c(&quot;BYWR110A&quot;, &quot;BYWR110B&quot;, &quot;BYWR110C&quot;, 
&quot;BYWR110D&quot;, &quot;BYWR111A&quot;, &quot;BYWR111B&quot;, &quot;BYWR111C&quot;, &quot;BYWR111D&quot;, &quot;BYWR112D&quot;, 
&quot;BYWR112C&quot;, &quot;BYWR112B&quot;, &quot;BYWR112A&quot;, &quot;BWWR101A&quot;, &quot;BWWR101C&quot;, &quot;BWWR101B&quot;, 
&quot;BWWR101D&quot;, &quot;BWWR100A&quot;, &quot;BWWR100B&quot;, &quot;BWWR100C&quot;, &quot;BWWR100D&quot;, &quot;NWWR130A&quot;, 
&quot;NWWR130B&quot;, &quot;NWWR130C&quot;, &quot;NWWR130D&quot;, &quot;NWWR131A&quot;, &quot;NWWR131B&quot;, &quot;NWWR131C&quot;, 
&quot;NWWR131D&quot;, &quot;NWWR132A&quot;, &quot;NWWR132B&quot;, &quot;NWWR132C&quot;, &quot;NWWR132D&quot;, &quot;BWWR103A&quot;, 
&quot;BWWR103B&quot;, &quot;BWWR103C&quot;, &quot;BWWR103D&quot;, &quot;BWWR102A&quot;, &quot;NWWR133B&quot;, &quot;BWWR102D&quot;, 
&quot;BWWR102C&quot;, &quot;NWWR133A&quot;, &quot;BWWR102B&quot;, &quot;NWWR133C&quot;, &quot;NWWR133D&quot;, &quot;MVWR170-A&quot;, 
&quot;MVWR170-B&quot;, &quot;MVWR170-C&quot;, &quot;MVWR170-D&quot;, &quot;MVWR171-A&quot;, &quot;MVWR171-B&quot;, 
&quot;MVWR171-C&quot;, &quot;MVWR171-D&quot;, &quot;SNWR140a_SF&quot;, &quot;SNWR140b&quot;, &quot;SNWR140c&quot;, 
&quot;SNWR140d&quot;, &quot;SNWR141a&quot;, &quot;SNWR141b&quot;, &quot;SNWR141c&quot;, &quot;SNWR141d_SF&quot;, 
&quot;SNWR142a_SF&quot;, &quot;SNWR142b&quot;, &quot;SNWR142c&quot;, &quot;SNWR142d&quot;, &quot;BYWR113A&quot;, 
&quot;BYWR113B&quot;, &quot;BYWR113C&quot;, &quot;BYWR113D&quot;)), units = structure(list(
numerator = &quot;m&quot;, denominator = character(0)), class = &quot;symbolic_units&quot;), class = &quot;units&quot;)

The code I have been trying

d &lt;- as.data.frame.table(mre)
d &lt;- transform(d, Freq = as.numeric(Freq))
out &lt;- subset(d, Var1 == sub(&quot;\\d{7,}&quot;, &quot;&quot;, Var2),
select =c(Var1, Var2, Freq))

答案1

得分: 2

Extracting the values from the whole data frame into a list of data frames. Assuming 4 letters and 3 digits, based on the example.

sapply(unique(sub("([[:alpha:]]{4}\\d{3}).*", "\\1", colnames(df))), function(x) 
  df[grepl(x, rownames(df)), grepl(x, colnames(df))])
$BYWR110
         BYWR110A BYWR110B BYWR110C BYWR110D
BYWR110A   0.0000 364.9303 604.4423 410.9918
BYWR110B 364.9303   0.0000 366.0001 551.4393
BYWR110C 604.4423 366.0001   0.0000 507.1813
BYWR110D 410.9918 551.4393 507.1813   0.0000

$BYWR111
         BYWR111A BYWR111B BYWR111C BYWR111D
BYWR111A  0.00000 88.79775 55.51231 111.6589
BYWR111B 88.79775  0.00000 74.68791 171.3345

$BYWR112
     BYWR112D BYWR112C BYWR112B BYWR112A

$BWWR101
     BWWR101A BWWR101C BWWR101B BWWR101D
...

If you only wanted the unique vector

sapply(unique(sub("([[:alpha:]]{4}\\d{3}).*", "\\1", colnames(df))), function(x){
  res <- df[grepl(x, rownames(df)), grepl(x, colnames(df))]
  res[upper.tri(res)]})
$BYWR110
[1] 364.9303 604.4423 366.0001 410.9918 551.4393 507.1813

$BYWR111
[1]  88.79775  55.51231  74.68791 111.65891 171.33453

$BYWR112
numeric(0)

$BWWR101
numeric(0)
...
英文:

Extracting the values from the whole data frame into a list of data frames. Assuming 4 letters and 3 digits, based on the example.

sapply(unique(sub(&quot;([[:alpha:]]{4}\\d{3}).*&quot;, &quot;\\1&quot;, colnames(df))), function(x) 
df[grepl(x, rownames(df)), grepl(x, colnames(df))])
$BYWR110
BYWR110A BYWR110B BYWR110C BYWR110D
BYWR110A   0.0000 364.9303 604.4423 410.9918
BYWR110B 364.9303   0.0000 366.0001 551.4393
BYWR110C 604.4423 366.0001   0.0000 507.1813
BYWR110D 410.9918 551.4393 507.1813   0.0000
$BYWR111
BYWR111A BYWR111B BYWR111C BYWR111D
BYWR111A  0.00000 88.79775 55.51231 111.6589
BYWR111B 88.79775  0.00000 74.68791 171.3345
$BYWR112
BYWR112D BYWR112C BYWR112B BYWR112A
$BWWR101
BWWR101A BWWR101C BWWR101B BWWR101D
...

If you only wanted the unique vector

sapply(unique(sub(&quot;([[:alpha:]]{4}\\d{3}).*&quot;, &quot;\\1&quot;, colnames(df))), function(x){
res &lt;- df[grepl(x, rownames(df)), grepl(x, colnames(df))]
res[upper.tri(res)]})
$BYWR110
[1] 364.9303 604.4423 366.0001 410.9918 551.4393 507.1813
$BYWR111
[1]  88.79775  55.51231  74.68791 111.65891 171.33453
$BYWR112
numeric(0)
$BWWR101
numeric(0)
...

答案2

得分: 1

你可以使用 substr() 函数将点的名称分割为矩形的名称和角落的ID。然后,你可以在 subset() 函数中过滤出属于同一个矩形的距离,并按角落的ID排序,以避免重复:

d$Rectangle <- substr(d$Var1, 1, 7)
d$Rectangle2 <- substr(d$Var2, 1, 7)
d$corner <- substr(d$Var1, 8, 8)
d$corner2 <- substr(d$Var2, 8, 8)

out <- subset(d, Rectangle == Rectangle2 & corner < corner2,
              select =c(Rectangle, Var1, Var2, Freq))

out
#    Rectangle     Var1     Var2      Freq
# 7    BYWR110 BYWR110A BYWR110B 364.93030
# 13   BYWR110 BYWR110A BYWR110C 604.44231
# 14   BYWR110 BYWR110B BYWR110C 366.00006
# 19   BYWR110 BYWR110A BYWR110D 410.99177
# 20   BYWR110 BYWR110B BYWR110D 551.43933
# 21   BYWR110 BYWR110C BYWR110D 507.18133
# 35   BYWR111 BYWR111A BYWR111B  88.79775
# 41   BYWR111 BYWR111A BYWR111C  55.51231
# 42   BYWR111 BYWR111B BYWR111C  74.68791
# 47   BYWR111 BYWR111A BYWR111D 111.65891
# 48   BYWR111 BYWR111B BYWR111D 171.33453
英文:

You can use substr() to split the names of the points into the names of the rectangle and the ids of the corners. Then you can filter in subset() for those distances belonging to the same rectangle, sorted by the corner IDs to avoid duplicates:

d$Rectangle &lt;- substr(d$Var1, 1, 7)
d$Rectangle2 &lt;- substr(d$Var2, 1, 7)
d$corner &lt;- substr(d$Var1, 8, 8)
d$corner2 &lt;- substr(d$Var2, 8, 8)
out &lt;- subset(d, Rectangle == Rectangle2 &amp; corner &lt; corner2,
select =c(Rectangle, Var1, Var2, Freq))
out
#    Rectangle     Var1     Var2      Freq
# 7    BYWR110 BYWR110A BYWR110B 364.93030
# 13   BYWR110 BYWR110A BYWR110C 604.44231
# 14   BYWR110 BYWR110B BYWR110C 366.00006
# 19   BYWR110 BYWR110A BYWR110D 410.99177
# 20   BYWR110 BYWR110B BYWR110D 551.43933
# 21   BYWR110 BYWR110C BYWR110D 507.18133
# 35   BYWR111 BYWR111A BYWR111B  88.79775
# 41   BYWR111 BYWR111A BYWR111C  55.51231
# 42   BYWR111 BYWR111B BYWR111C  74.68791
# 47   BYWR111 BYWR111A BYWR111D 111.65891
# 48   BYWR111 BYWR111B BYWR111D 171.33453

huangapple
  • 本文由 发表于 2023年4月17日 22:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定