英文:
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<-paste(df$Name, df$Firstname1,df$Firstname2,df$Firstname3)
df$result2<-paste(df$Name, df$Firstname1,df$Firstname3,df$Firstname2)
答案1
得分: 0
你可以使用combinat::permn
或gtools::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 <- 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, .)
So for each option
.x
, we are selecting the columns of the dataframe in that order (df1[,c("Name", .x)]
), and pasting their rows together.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论