如何使用R中的另一列重塑数据帧以创建二进制组合
我有一个数据集,其中包括名称和他们曾经工作过的公司如何使用R中的另一列重塑数据帧以创建二进制组合,r,sorting,dplyr,group-by,reshape,R,Sorting,Dplyr,Group By,Reshape,我有一个数据集,其中包括名称和他们曾经工作过的公司 name company A Mazda B Benz C Mazda D Toyota E Benz F Mazda E BMW G Benz A Toyota C Toyota B BMW 我想使用dplyr并通过显示曾在同一家公司工作过的两个姓名的任意组合
name company
A Mazda
B Benz
C Mazda
D Toyota
E Benz
F Mazda
E BMW
G Benz
A Toyota
C Toyota
B BMW
我想使用dplyr并通过显示曾在同一家公司工作过的两个姓名的任意组合来对数据进行排序。名称的顺序并不重要,例如,A和C组合与C和A组合没有区别。因此,有利的结果将是
name 1 name 2 company
A C Mazda
A C Toyota
A F Mazda
B E Benz
B E BMW
C F Mazda
B G Benz
E G Benz
D C Toyota
A C Toyota
下面是一个使用
purrr
library(dplyr)
library(purrr)
# Function that take in a df that only contains one company names
company_combination <- function(data) {
stopifnot(length(unique(data$company)) == 1)
# generate a empty tibble if there only one record for the company
if (nrow(data) == 1) {
names_comb <- tibble()
} else {
# get the company name
company <- first(data[["company"]])
# generate name combination from the name list and put it into tibble of two columns
names_comb <- as_tibble(t(combn(x = unique(data[["name"]]), m = 2)))
# change column names to name_1 & name_2
names(names_comb) <- c("name_1", "name_2")
# Add the company name into the new name combination df and return the result
names_comb %<>% mutate(company = company)
}
names_comb
}
# Here there are three steps
df %>%
# 1st split original data into list of dataframe group by company
split(.$company) %>%
# Then apply the company_combination function for each company df
# Result of this command is a new list of dataframe as result of the function
map(., company_combination) %>%
# Then bind them all together back to one dataframe.
bind_rows()
库(dplyr)
图书馆(purrr)
#函数,该函数接受仅包含一个公司名称的df
公司组合您的输出与我的不同,您是否创建了不同的df?我使用的数据与您提供的相同。输出是不同的,但我认为我的是更完整的-如果你有三个人在一家公司,你应该有3个2的组合。虽然您的示例输出仅为2 combination.thnaks,但您是对的,我将运行您的代码并随时向您报告。我收到此错误`error in.f(.x[[I]],…):length(unique(data$company))==1不是真的`。我看到你提到了同样的错误,但很明显,我在一些公司有超过1个错误,或者说我在一些公司有重复的错误。你能分享导致这一错误的代码吗?您有拆分的行吗?并确保将函数调整为与数据中的列名相匹配。
# A tibble: 10 x 3
name_1 name_2 company
<chr> <chr> <chr>
1 B E Benz
2 B G Benz
3 E G Benz
4 B E BMW
5 A C Mazda
6 A F Mazda
7 C F Mazda
8 A C Toyota
9 A D Toyota
10 C D Toyota