[英]Split dataframe into list of multiple dataframes based on multiple columns and select specific columns for each nested dataframe
我有一個像這樣的 dataframe:
|DOI | WoS| Scopus| Dim| WoS_Year| Scopus_Year| Dim_Year|
|:-----------------------------|---:|------:|---:|--------:|-----------:|--------:|
|10.1515/jag-2017-0010 | NA| 1| 1| NA| 2017| 2017|
|10.1007/978-3-662-55771-6_9 | NA| NA| 1| NA| NA| 2020|
|10.1088/1361-6668/30/2/024004 | 1| 1| NA| 2017| 2017| NA|
|10.3390/ma12010124 | 1| 1| NA| 2019| 2019| NA|
|10.1002/ppsc.201700109 | 1| 1| 1| 2017| 2017| 2017|
我想將其拆分為 3 個數據幀( list_of_df
)的列表,其中:
list_of_df$WoS
應該包含所有具有WoS = 1
的DOI
,以及類似於舊WoS_Year
的列Year
;list_of_df$Scopus
應該包含所有具有Scopus= 1
的DOI
,以及類似於舊Scopus_Year
的列Year
;list_of_df$Dim
應該包含所有具有Dim= 1
的DOI
,以及類似於舊Dim_Year
的Year
列。 (實際上有多個以WoS_*
或Scopus_*
或Dim_*
開頭的列,我希望將每一列都保留在新的相應list_of_df
中,但是通過刪除WoS_
等起始字符串。
例如,所有starting_with("Scopus_")
都應該在list_of_df$Scopus
,但列名中沒有Scopus_
。)
實現這一目標的最佳方法是什么?
我對list_of_df <- split(df, with(df, interaction(WoS,Scopus,Dim)), drop = TRUE)
或dplyr::nest(df, WoS:Dim)
沒有結果...
謝謝您的幫助!
> dput(df)
structure(list(DOI = c("10.1515/jag-2017-0010", "10.1007/978-3-662-55771-6_9",
"10.1088/1361-6668/30/2/024004", "10.3390/ma12010124", "10.1002/ppsc.201700109"
), WoS = c(NA, NA, 1L, 1L, 1L), Scopus = c(1L, NA, 1L, 1L, 1L
), Dim = c(1L, 1L, NA, NA, 1L), WoS_Year = c(NA, NA, 2017L, 2019L,
2017L), Scopus_Year = c(2017L, NA, 2017L, 2019L, 2017L), Dim_Year = c(2017L,
2020L, NA, NA, 2017L)), row.names = c(2186L, 9505L, 12281L, 11882L,
874L), class = "data.frame")
在基礎 R 中,您可以執行以下操作:
df1 <- subset(reshape(df, matrix(2:ncol(df),2, byrow=TRUE), dir="long", idvar = "DOI",
times = c("WoS","Scopus","Dim")), WoS==1)
rownames(df1)<-NULL
split(df1, df1$time)
$Dim
DOI time WoS WoS_Year
8 10.1515/jag-2017-0010 Dim 1 2017
9 10.1007/978-3-662-55771-6_9 Dim 1 2020
10 10.1002/ppsc.201700109 Dim 1 2017
$Scopus
DOI time WoS WoS_Year
4 10.1515/jag-2017-0010 Scopus 1 2017
5 10.1088/1361-6668/30/2/024004 Scopus 1 2017
6 10.3390/ma12010124 Scopus 1 2019
7 10.1002/ppsc.201700109 Scopus 1 2017
$WoS
DOI time WoS WoS_Year
1 10.1088/1361-6668/30/2/024004 WoS 1 2017
2 10.3390/ma12010124 WoS 1 2019
3 10.1002/ppsc.201700109 WoS 1 2017
您可以更改列名以匹配您想要的
另一種方式:
lapply(split.default(df[-1],sub("_.*","",names(df[-1]))),
function(x)na.omit(cbind(df[1], x)[x[[1]]==1,]))
$Dim
DOI Dim Dim_Year
2186 10.1515/jag-2017-0010 1 2017
9505 10.1007/978-3-662-55771-6_9 1 2020
874 10.1002/ppsc.201700109 1 2017
$Scopus
DOI Scopus Scopus_Year
2186 10.1515/jag-2017-0010 1 2017
12281 10.1088/1361-6668/30/2/024004 1 2017
11882 10.3390/ma12010124 1 2019
874 10.1002/ppsc.201700109 1 2017
$WoS
DOI WoS WoS_Year
12281 10.1088/1361-6668/30/2/024004 1 2017
11882 10.3390/ma12010124 1 2019
874 10.1002/ppsc.201700109 1 2017
基礎 R 中的另一個,如果這是預期的 output
res=list()
for (k in c("WoS","Scopus","Dim")) {
res[[k]]=df[df[,k]==1 & !is.na(df[,k]),grepl(k,colnames(df)) | c(TRUE,rep(FALSE,ncol(df)-1))]
colnames(res[[k]])=gsub(paste0(k,"_"),"",colnames(res[[k]]))
}
$WoS
DOI WoS Year
12281 10.1088/1361-6668/30/2/024004 1 2017
11882 10.3390/ma12010124 1 2019
874 10.1002/ppsc.201700109 1 2017
$Scopus
DOI Scopus Year
2186 10.1515/jag-2017-0010 1 2017
12281 10.1088/1361-6668/30/2/024004 1 2017
11882 10.3390/ma12010124 1 2019
874 10.1002/ppsc.201700109 1 2017
$Dim
DOI Dim Year
2186 10.1515/jag-2017-0010 1 2017
9505 10.1007/978-3-662-55771-6_9 1 2020
874 10.1002/ppsc.201700109 1 2017
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.