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

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

First and last name permutation in datasets

问题

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

  1. 我有一个数据集(近 26,000 行),其中每列显示如下的名字和姓氏:
  2. Id Name Firstname1 Firstname2 Firstname3
  3. 1 AL BE GAM ZET
  4. 2 IO PA TA MA
  5. 我希望在结果列中得到所有可能的名字和姓氏排列,如下所示:
  6. Id Name Firstname1 Firstname2 Firstname3 Result1 Result2... Resultatn
  7. 1 AL BE GAM ZET ALBEGAMZET ALBEZETGAM GAMZETBEAL
  8. 2 IO PA TA MA IOPATAMA IOPAMATA TAMAIOPAIO
  9. 谢谢你的帮助!
  10. 我尝试了以下方法,但我正在寻求通用的帮助:
  11. df$result1<-paste(df$Name, df$Firstname1,df$Firstname2,df$Firstname3)
  12. 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:

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

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

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

Thanks for your help!

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

  1. df$result1&lt;-paste(df$Name, df$Firstname1,df$Firstname2,df$Firstname3)
  2. df$result2&lt;-paste(df$Name, df$Firstname1,df$Firstname3,df$Firstname2)

答案1

得分: 0

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

  1. df1 <- tribble(
  2. ~Id ,~Name ,~Firstname1 ,~Firstname2 ,~Firstname3,
  3. 1 , "AL" , "BE" , "GAM" , "ZET",
  4. 2 , "IO" , "PA" , "TA" , "MA",
  5. )
  6. options <- combinat::permn(colnames(df1)[-(1:2)])
  7. df2 <- map_dfc(options, ~ pmap_chr(df1[,c("Name", .x)], ~ paste0(..., collapse = ""))) %>%
  8. set_names(paste0("Result", 1:length(options))) %>%
  9. 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.

  1. df1 &lt;- tribble(
  2. ~Id ,~Name ,~Firstname1 ,~Firstname2 ,~Firstname3,
  3. 1 , &quot;AL&quot; , &quot;BE&quot; , &quot;GAM&quot; , &quot;ZET&quot;,
  4. 2 , &quot;IO&quot; , &quot;PA&quot; , &quot;TA&quot; , &quot;MA&quot;,
  5. )
  6. options &lt;- combinat::permn(colnames(df1)[-(1:2)])
  7. df2 &lt;- map_dfc(options, ~ pmap_chr(df1[,c(&quot;Name&quot;, .x)], ~ paste0(..., collapse = &quot;&quot;))) %&gt;%
  8. set_names(paste0(&quot;Result&quot;, 1:length(options))) %&gt;%
  9. 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:

确定