根据条件创建二元成对数据集
Create dyadic pairwise dataset based on conditions
我有一个看起来像这样的单子数据集:
df<-structure(list(Number = c("375_1", "375_1", "375_1", "375_1",
"375_1", "375_1", "375_1", "375_1", "647_1", "647_1", "647_1",
"647_1", "647_1", "647_1", "647_1", "647_1", "647_1", "647_1",
"647_1", "647_1"), year = c(1973, 1973, 1973, 1973, 1973, 1973,
1973, 1973, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981,
1981, 1981, 1981), Country = c("AUT", "PRT", "CHE", "NOR", "SWE",
"ISL", "DNK", "GBR", "BRA", "CHL", "EGY", "IND", "ISR", "MEX",
"PER", "KOR", "PAK", "PHL", "TUN", "TUR")), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
我想将此数据集转换为具有基于 Number
列的二元结构。换句话说,对于每个不同的 Number
,我想为所有国家组合创建成对的观察结果。
最终数据集的 "head" 应如下所示:
final <- data.frame(Number = c("375_1", "375_1", "375_1", "375_1", "375_1", "375_1", "375_1","375_1", "375_1", "375_1", "375_1","375_1"),
year = c(1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973,1973, 1973, 1973, 1973),
Country1 = c("AUT", "AUT", "AUT", "AUT", "AUT", "AUT", "AUT","PRT","PRT","PRT","PRT","PRT"),
Country2 = c("PRT", "CHE", "NOR", "SWE", "ISL", "DNK", "GBR","CHE","NOR","SWE", "ISL","DNK"),
stringsAsFactors = FALSE)
然后对组内的每个国家二元组继续这样。
我想找到一种简洁明了的方法
非常感谢您的帮助
为了以后参考,我认为解决方案如下
final <- df %>% group_by(Number, year)%>% expand(Country1 = Country, Country2= Country) %>% filter(Country1!=Country2)
使用tidyverse
:
library(tidyverse)
df %>%
full_join(df, by=c("Number", "year")) %>%
filter(Country.x != Country.y)
我有一个看起来像这样的单子数据集:
df<-structure(list(Number = c("375_1", "375_1", "375_1", "375_1",
"375_1", "375_1", "375_1", "375_1", "647_1", "647_1", "647_1",
"647_1", "647_1", "647_1", "647_1", "647_1", "647_1", "647_1",
"647_1", "647_1"), year = c(1973, 1973, 1973, 1973, 1973, 1973,
1973, 1973, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981,
1981, 1981, 1981), Country = c("AUT", "PRT", "CHE", "NOR", "SWE",
"ISL", "DNK", "GBR", "BRA", "CHL", "EGY", "IND", "ISR", "MEX",
"PER", "KOR", "PAK", "PHL", "TUN", "TUR")), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
我想将此数据集转换为具有基于 Number
列的二元结构。换句话说,对于每个不同的 Number
,我想为所有国家组合创建成对的观察结果。
最终数据集的 "head" 应如下所示:
final <- data.frame(Number = c("375_1", "375_1", "375_1", "375_1", "375_1", "375_1", "375_1","375_1", "375_1", "375_1", "375_1","375_1"),
year = c(1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973,1973, 1973, 1973, 1973),
Country1 = c("AUT", "AUT", "AUT", "AUT", "AUT", "AUT", "AUT","PRT","PRT","PRT","PRT","PRT"),
Country2 = c("PRT", "CHE", "NOR", "SWE", "ISL", "DNK", "GBR","CHE","NOR","SWE", "ISL","DNK"),
stringsAsFactors = FALSE)
然后对组内的每个国家二元组继续这样。 我想找到一种简洁明了的方法
非常感谢您的帮助
为了以后参考,我认为解决方案如下
final <- df %>% group_by(Number, year)%>% expand(Country1 = Country, Country2= Country) %>% filter(Country1!=Country2)
使用tidyverse
:
library(tidyverse)
df %>%
full_join(df, by=c("Number", "year")) %>%
filter(Country.x != Country.y)