名字和姓氏在数据集中的排列组合

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

First and last name permutation in datasets

问题

Here are the translations for the code part of your request:

我有一个数据集(近 26,000 行),其中每列显示如下的名字和姓氏:

Id       Name  Firstname1 Firstname2 Firstname3 
 1       AL      BE          GAM       ZET
 2        IO      PA          TA        MA

我希望在结果列中得到所有可能的名字和姓氏排列,如下所示:

Id Name Firstname1 Firstname2 Firstname3 Result1 Result2...   Resultatn 
1  AL    BE       GAM         ZET    ALBEGAMZET ALBEZETGAM   GAMZETBEAL
2   IO    PA     TA           MA       IOPATAMA  IOPAMATA     TAMAIOPAIO             

谢谢你的帮助!

我尝试了以下方法,但我正在寻求通用的帮助:

df$result1<-paste(df$Name, df$Firstname1,df$Firstname2,df$Firstname3)

df$result2<-paste(df$Name, df$Firstname1,df$Firstname3,df$Firstname2)

Please note that I've translated only the code part as per your request. If you have any further questions or need assistance with anything else, feel free to ask.

英文:

I have a dataset (nearly 26,000 rows) where I have the first and last names in each column as shown below:

Id       Name  Firstname1 Firstname2 Firstname3 
 1       AL      BE          GAM       ZET
2        IO      PA          TA        MA

I want all possible permutations of the first and last names in the result column as follows:

Id Name Firstname1 Firstname2 Firstname3 Result1 Result2...   Resultatn 
1  AL    BE       GAM         ZET    ALBEGAMZET ALBEZETGAM   GAMZETBEAL
2   IO    PA     TA           ME       IOPATAME  IOPAMETA     TAMEPAIO             

Thanks for your help!

I've tried this but I'm looking for help to generalize

df$result1&lt;-paste(df$Name, df$Firstname1,df$Firstname2,df$Firstname3)

df$result2&lt;-paste(df$Name, df$Firstname1,df$Firstname3,df$Firstname2)

答案1

得分: 0

你可以使用combinat::permngtools::permutations来创建列的排列组合。然后,对于每个组合,你可以按指定的顺序将列粘贴在一起。

df1 <- tribble(
  ~Id       ,~Name  ,~Firstname1 ,~Firstname2 ,~Firstname3, 
  1   ,    "AL"   ,   "BE"    ,      "GAM"   ,    "ZET",
  2   ,     "IO"  ,    "PA"    ,      "TA"    ,    "MA",
)

options <- combinat::permn(colnames(df1)[-(1:2)])

df2 <- map_dfc(options, ~ pmap_chr(df1[,c("Name", .x)], ~ paste0(..., collapse = ""))) %>%
  set_names(paste0("Result", 1:length(options))) %>%
  bind_cols(df1, .)

所以对于每个option .x,我们选择了数据框中的列,并将它们的行粘贴在一起。

英文:

You can create the permutations of the columns with combinat::permn or gtools::permutations. Then, for each combination, you can paste the columns together in the specified oder.

df1 &lt;- tribble(
  ~Id       ,~Name  ,~Firstname1 ,~Firstname2 ,~Firstname3, 
  1   ,    &quot;AL&quot;   ,   &quot;BE&quot;    ,      &quot;GAM&quot;   ,    &quot;ZET&quot;,
  2   ,     &quot;IO&quot;  ,    &quot;PA&quot;    ,      &quot;TA&quot;    ,    &quot;MA&quot;,
)

options &lt;- combinat::permn(colnames(df1)[-(1:2)])

df2 &lt;- map_dfc(options, ~ pmap_chr(df1[,c(&quot;Name&quot;, .x)], ~ paste0(..., collapse = &quot;&quot;))) %&gt;%
  set_names(paste0(&quot;Result&quot;, 1:length(options))) %&gt;%
  bind_cols(df1, .)

So for each option .x, we are selecting the columns of the dataframe in that order (df1[,c(&quot;Name&quot;, .x)]), and pasting their rows together.

huangapple
  • 本文由 发表于 2023年6月13日 06:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460588.html
匿名

发表评论

匿名网友

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

确定