为二元组创建唯一的ID。非定向

时间:2018-09-13 15:27:54

标签: r uniqueidentifier id

我有一个数据框架,其中包括对其他国家/地区的国家/地区年度进出口。与示例数据集中的数据一样,二元导入和导出的数据也不完全重叠。

例如

library(tidyverse)

df <- data.frame("Reporter" = c("USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "Africa","Africa", "Africa","Africa", "Africa","Africa", "Africa","Africa", "EU", "EU","EU", "EU", "EU", "EU","EU", "EU"), 
                 "Partner" = c("Africa","Africa", "Africa","Africa","EU", "EU","EU", "EU", "USA", "USA", "USA", "USA", "EU", "EU","EU", "EU","USA", "USA", "USA", "USA","Africa","Africa", "Africa","Africa"),
                 "Year" = c(1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980,  1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980), 
                 "Flow" = c("Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export"),
                 "Val" = runif(24, min=0, max=100), stringsAsFactors = FALSE)                    

然后我创建此数据的宽版。

wide_df <- df %>% spread ("Flow", "Val")

我能够为二元组创建方向ID。

wide_df$ReporterID  <- as.numeric(factor(wide_df$Reporter, levels=unique(wide_df$Reporter)))
但是,得到的数据被认为是不同的,例如美国和非洲的二分体,以及非洲和美国。

问题:如何为每个二元组创建唯一的ID?

谁能想到一种允许我将这些双字母分解为一个ID码的方法

感谢您的考虑!

2 个答案:

答案 0 :(得分:0)

library(tidyverse)

# vectorised function to order and combine values
f = function(x,y) paste(sort(c(x, y)), collapse="_")
f = Vectorize(f)

df %>% 
  spread ("Flow", "Val") %>%
  mutate(ID1 = f(Reporter, Partner),
         ID2 = as.numeric(as.factor(ID1)))

#   Reporter Partner  Year Export Import ID1         ID2
# 1 Afica    EU       1970  56.6  98.9   Afica_EU      1
# 2 Afica    EU       1980  95.3   2.25  Afica_EU      1
# 3 Afica    USA      1970  50.4  10.3   Afica_USA     2
# 4 Afica    USA      1980  29.4   3.08  Afica_USA     2
# 5 EU       Afica    1970  88.8  56.3   Afica_EU      1
# 6 EU       Afica    1980  53.6  48.0   Afica_EU      1
# 7 EU       USA      1970   4.50 83.8   EU_USA        3
# 8 EU       USA      1980  79.1   0.473 EU_USA        3
# 9 USA      Afica    1970  61.9  37.2   Afica_USA     2
#10 USA      Afica    1980   9.88 39.6   Afica_USA     2
#11 USA      EU       1970  10.4  29.3   EU_USA        3
#12 USA      EU       1980  21.1  35.3   EU_USA        3

一个选项是ID1,它结合了实际值。

另一个选项是ID2,它基于ID1创建一个数字。

这些ID2数字背后的逻辑是factor变量ID1的级别顺序(在这种情况下,即字母顺序)。

如果不需要原始列ReporterPartner,则可以在过程结束时使用unite(ID1, Reporter, Partner, remove = T)select(-Reporter, -Partner)排除它们。

答案 1 :(得分:0)

我们通过paste为每一行(pminpmax)的“ Reporter”,“ Partner”的相应元素的最小值和最大值创建唯一的“ id”,并将其转换到factor并强迫到numeric or using tidyverse`

library(tidyverse)
wide_df %>%
   mutate(newid = as.numeric(factor(paste(pmin(Reporter, Partner), 
                           pmax(Reporter, Partner), sep="_"))))
#   Reporter Partner Year    Export   Import newid
#1     Afica      EU 1970 23.494073 62.50156     1
#2     Afica      EU 1980 18.808975 52.17495     1
#3     Afica     USA 1970 23.679063 37.02527     2
#4     Afica     USA 1980  2.346382 21.69631     2
#5        EU   Afica 1970 73.075570 78.00496     1
#6        EU   Afica 1980 69.620370 60.24295     1
#7        EU     USA 1970 89.163190 80.78952     3
#8        EU     USA 1980 77.462146 48.51146     3
#9       USA   Afica 1970 18.285198 99.99596     2
#10      USA   Afica 1980 26.119664 40.51762     2
#11      USA      EU 1970 78.307579 70.91757     3
#12      USA      EU 1980 41.067151 84.06877     3