Sorting Multiple Data Tables Based On Character Type Column With Sanity Check

巧了我就是萌 提交于 2020-04-18 06:20:18

问题


I have two data table and want to sort them such that row value of first table is same as row value of second table based on a column named Parameter.

Using order(), the problem is that it will only sort based on the data table input. But to ensure that not only both data tables are sorted based on Parameter column, but are also sorted correctly by comparing the column Parameter row by row between two data tables as a sanity check.

Please share any faster way to do this.

First Table Input

Parameter   Data
R-1          1
R-2          1
R-3          1
P-11         1
P-12         1
P-8          2
P-9          1
P-10         1
R-4          1
R-5          1
P-14         1
P-15         1

Second Table Input

Parameter   Data
R-1          2
P-9          2
R-3          2
P-11         2
P-12         2
P-8          2
R-2          2
P-10         2
R-4          2
R-5          3
P-14         2
P-15         2

Desired Outputs

First Table Output

Parameter   Data
R-1         1
R-2         1
R-3         1
R-4         1
R-5         1
P-8         2
P-9         1
P-10        1
P-11        1
P-12        1
P-14        1
P-15        1

Second Table Output

Parameter   Data
R-1         2
R-2         2
R-3         2
R-4         2
R-5         3
P-8         2
P-9         2
P-10        2
P-11        2
P-12        2
P-14        2
P-15        2

回答1:


Here's an approach using dplyr and tidyr::separate.

library(dplyr); library(tidyr)

# Function to split up parameter into two pieces and sort like example
sort_table <- function(df) {
  df %>%  separate(Parameter, c("letter", "num"), remove = F) %>%
  arrange(desc(letter), as.numeric(num))
}

# Join the two sorted tables
full_join(
  sort_table(table_1),
  sort_table(table_2),
  by = c("Parameter", "letter", "num")
)



#   Parameter letter num Data.x Data.y
#1        R-1      R   1      1      2
#2        R-2      R   2      1      2
#3        R-3      R   3      1      2
#4        R-4      R   4      1      2
#5        R-5      R   5      1      3
#6        P-8      P   8      2      2
#7        P-9      P   9      1      2
#8       P-10      P  10      1      2
#9       P-11      P  11      1      2
#10      P-12      P  12      1      2
#11      P-14      P  14      1      2
#12      P-15      P  15      1      2


来源:https://stackoverflow.com/questions/57829899/sorting-multiple-data-tables-based-on-character-type-column-with-sanity-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!