#r #dataframe #sorting #match
#r #фрейм данных #сортировка #совпадение
Вопрос:
У меня есть исходный фрейм данных из многих строк (я знаю, что они реплицируются)
> dput(DATA)
structure(list(N_b = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), N_l = c(4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3), S = c(12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9), Proposed.Girder3 = c(0.52, 0.52, 0.52, 0.52, 0.52, 0.52,
0.52, 0.52, 0.52, 0.52, 0.52, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
0.65, 0.65, 0.65, 0.65, 0.65, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
0.51, 0.51, 0.51, 0.51, 0.51, 0.52, 0.52, 0.52, 0.52, 0.52, 0.52,
0.52, 0.52, 0.52, 0.52, 0.52, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
0.65, 0.65, 0.65, 0.65, 0.65, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
0.51, 0.51, 0.51, 0.51, 0.51, 0.52, 0.52, 0.52, 0.52, 0.52, 0.52,
0.52, 0.52, 0.52, 0.52, 0.52, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
0.65, 0.65, 0.65, 0.65, 0.65, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
0.51, 0.51, 0.51, 0.51, 0.51, 0.52, 0.52, 0.52, 0.52, 0.52, 0.52,
0.52, 0.52, 0.52, 0.52, 0.52, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
0.65, 0.65, 0.65, 0.65, 0.65, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
0.51, 0.51, 0.51, 0.51, 0.51, 0.52, 0.52, 0.52, 0.52, 0.52, 0.52,
0.52, 0.52, 0.52, 0.52, 0.52, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
0.65, 0.65, 0.65, 0.65, 0.65, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
0.51, 0.51, 0.51, 0.51, 0.51, 0.52, 0.52, 0.52, 0.52, 0.52, 0.52,
0.52, 0.52, 0.52, 0.52, 0.52, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
0.65, 0.65, 0.65, 0.65, 0.65, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
0.51, 0.51, 0.51, 0.51, 0.51), Lanes = c(4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3), UG = c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 108, 108, 108,
108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 108, 108, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 124, 124,
124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
124, 124, 124, 124, 124, 84, 84, 84, 84, 84, 84, 84, 84, 84,
84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84,
84, 84, 84, 84, 84, 84, 84, 84, 92, 92, 92, 92, 92, 92, 92, 92,
92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
92, 92, 92, 92, 92, 92, 92, 92, 92), CSi = c(0.498857761911128,
0.506171857609652, 0.491697098095741, 0.5060648860829, 0.51602587099039,
0.49808311021839, 0.484326916022697, 0.486261403372008, 0.484696645284676,
0.542438052075464, 0.501306385491762, 0.634937543079967, 0.642078412670016,
0.618943708143363, 0.642001779473278, 0.658268730337476, 0.630133634378208,
0.61289410586889, 0.615963132516221, 0.615769133902813, 0.686518342284576,
0.63848257046785, 0.477839632977349, 0.481308189937141, 0.466821213798956,
0.484416044616133, 0.495362194700848, 0.47320175377938, 0.46075484570102,
0.462933293434182, 0.46296944030225, 0.519970813725933, 0.478798800223883,
0.499649613847278, 0.50349372475143, 0.490922567156329, 0.506660508807011,
0.514932254618641, 0.497406049605651, 0.483910162470329, 0.484700178543721,
0.483690038460146, 0.541097742382397, 0.49864706679875, 0.638103261594521,
0.645030188324246, 0.622321358649241, 0.644932774331382, 0.661080914216008,
0.633266424051986, 0.616403425794446, 0.619662818975923, 0.619368626094409,
0.689062534462536, 0.641640617456748, 0.481809699169021, 0.484780552887199,
0.471041353094871, 0.489040154106175, 0.499309812059152, 0.477094533923277,
0.46479660834156, 0.467205762187312, 0.465930259455921, 0.524495868736496,
0.482408228794972, 0.498202392725583, 0.502618858440184, 0.489487329812287,
0.503840707835284, 0.514021291777706, 0.495297263755732, 0.482202022708633,
0.483839116286323, 0.539456419577533, 0.539456419855875, 0.498082441376597,
0.630858086293792, 0.63756198028618, 0.615358704038841, 0.637489319425201,
0.653397114261802, 0.625957013049853, 0.609464834716713, 0.612676084901444,
0.612530536192196, 0.612530533035217, 0.63430804238461, 0.48126980512503,
0.484644526574109, 0.470238034678857, 0.487935539905689, 0.49887970982208,
0.476533589513863, 0.464212956954452, 0.466465412750473, 0.46642671330667,
0.52379164029609, 0.48210024308779, 0.495313482556363, 0.499430830726606,
0.486650554549094, 0.501074567462105, 0.511559881655238, 0.492318751733689,
0.479463896518796, 0.480962859032664, 0.479819940340815, 0.536420385604673,
0.494978560935791, 0.628848475181058, 0.63411772566777, 0.613650360338718,
0.637687298501148, 0.651062927764633, 0.624780782896341, 0.608015537732378,
0.609978147167127, 0.610267677247537, 0.679026578215092, 0.630653747823922,
0.484330062840347, 0.483272947533652, 0.469030546777778, 0.486654560445457,
0.497498231247353, 0.475287888336171, 0.46299937090013, 0.465252231525678,
0.465143863657343, 0.52242431063692, 0.480777563607102, 0.509393190572395,
0.0306794102100841, 0.499801210623311, 0.514261273631288, 0.524257222129056,
0.507090829156798, 0.492293988923706, 0.494634579696826, 0.492902890462201,
0.551785598208862, 0.510878424089161, 0.639185175219647, 0.646663818507054,
0.622627268125056, 0.646370988091098, 0.662988587960886, 0.634650836091679,
0.616659986042537, 0.619748241531646, 0.61951439818747, 0.691805427278811,
0.64284460028603, 0.484887769151249, 0.48865031929918, 0.473940218959181,
0.484917825918303, 0.496960554187183, 0.473813537802849, 0.461261792526738,
0.463580335134683, 0.463394052048788, 0.520637868360136, 0.479413561159061,
0.503027081012421, 0.508886440468214, 0.493841291641758, 0.508191035709441,
0.517649011896045, 0.500464428119884, 0.486297995480364, 0.48821271352713,
0.486779215307284, 0.544834557703888, 0.503994168801514, 0.63420530022389,
0.641441566573504, 0.617939244523909, 0.641442341788609, 0.657759058843292,
0.629532366426079, 0.611898297010965, 0.615054071992963, 0.614543613798064,
0.686239403738044, 0.637751280776591, 0.476610456219972, 0.480071378890351,
0.465670604256241, 0.483172142840914, 0.494035003336151, 0.472023145046551,
0.459586167724079, 0.461826826254301, 0.461674647472426, 0.518534655365997,
0.477607992305144)), row.names = c(NA, -198L), class = "data.frame")
Я пытаюсь отсортировать его на основе столбца S
с:
target <- c(12,15,9)
DATA <- DATA[match(target, DATA$S),]
В результате получается 3-строчный фрейм данных, но я хочу сохранить одинаковое количество строк и просто отсортировать его
> dput(DATA)
structure(list(N_b = c(5, 5, 5), N_l = c(4, 5, 3), S = c(12,
15, 9), Proposed.Girder3 = c(0.52, 0.65, 0.51), Lanes = c(4,
5, 3), UG = c(100, 100, 100), CSi = c(0.498857761911128, 0.634937543079967,
0.477839632977349)), row.names = c(1L, 12L, 23L), class = "data.frame")
Комментарии:
1.
DATA[order(factor(DATA$S, c(12, 15, 9))), ]
Ответ №1:
Вот обходной путь:
library(dplyr)
data %>%
# convert S to factor and specify order
mutate(S = factor(S, levels = c(12, 15, 9))) %>%
# sort by levels of S factor
arrange(S) %>%
# convert S back to numeric (need to go through character or weirdness happens)
mutate(S = as.numeric(as.character(S)))
Ответ №2:
Порядок в match
должен быть отменен, и вам нужно order
вывести из match
.
target <- c(12,15,9)
DATA <- DATA[order(match(DATA$S, target)),]