매핑 파일을 따라 환경에있는 파일 목록을 병합하는 방법
Nov 19 2020
내 R 환경에 파일 목록이 있습니다. 매핑 파일을 사용하여 일부를 병합하고 싶습니다.
매핑 파일의 이름은 map_rule1이며 다음과 같습니다.
map_rule1
# A tibble: 8 x 4
EDC_file_name Tab DatasetName GroupVar1
<chr> <chr> <chr> <chr>
1 e1 Demographics Demographics Merged Subject
2 e2 Demographics NA NA
3 e3 PatientRegister Patient Register Subject
4 e4 PatientRegister NA NA
5 e5 PatientRegister NA NA
6 e6 PatientRegister NA NA
7 e7 PatientConsent Patient Consent NA
8 e8 PatientConsent NA NA
Data col에 나열된 항목은 현재 r 환경에있는 파일입니다. Group_V1에 나열된 변수와 New_data_Name에 나열된 새 데이터 이름으로 동일한 도메인으로 분류 된 파일을 하나의 파일로 병합하려고합니다. 병합해야 할 파일이 100 개 이상 있습니다. 그렇기 때문에 루핑 메서드 또는 해당 파일을 자동으로 병합하는 다른 방법을 만들고 싶습니다.
샘플 데이터 및 Map_Rule은 코드를 사용하여 빌드 할 수 있습니다.
e1<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), SEX = structure(c(2L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male"), class = "factor")), class = "data.frame", row.names = c(NA,
-27L))
e2<-
structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), RACE = structure(c(2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c("Black (including African, Caribbean descent)",
"Caucasian"), class = "factor")), class = "data.frame", row.names = c(NA,
-27L))
e3<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), ETHNIC_STD = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L)), class = "data.frame", row.names = c(NA,
-27L))
e4<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), subjectId = c(168L,
171L, 174L, 175L, 196L, 199L, 207L, 208L, 213L, 209L, 210L, 212L,
283L, 325L, 329L, 527L, 315L, 316L, 320L, 334L, 339L, 582L, 319L,
523L, 526L, 601L, 532L)), class = "data.frame", row.names = c(NA,
-27L))
e5<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), siteid = c(9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L)), class = "data.frame", row.names = c(NA,
-27L))
e5<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), siteid = c(9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L)), class = "data.frame", row.names = c(NA,
-27L))
e7<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0007", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), Location = structure(c(2L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Urban", "Ural"), class = "factor")), class = "data.frame", row.names = c(NA,
-27L))
e8<-structure(list(Subject = structure(c(1L, 2L, 3L, 5L, 6L, 4L,
7L, 8L, 9L, 21L, 22L, 23L, 24L, 25L, 27L, 26L, 10L, 11L, 12L,
13L, 14L, 15L, 17L, 19L, 18L, 20L, 16L), .Label = c("300-0001",
"300-0002", "300-0003", "300-0004", "300-0005", "300-0006", "300-0007",
"300-0008", "300-0009", "301-0001", "301-0002", "301-0003", "301-0004",
"301-0005", "301-0006", "302-0001", "303-0001", "303-0002", "303-0003",
"303-0004", "304-0001", "304-0002", "304-0003", "304-0004", "304-0005",
"304-0006", "304-0007"), class = "factor"), SEX = structure(c(2L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male"), class = "factor")), class = "data.frame", row.names = c(NA,
-27L))
map_rule1<-structure(list(EDC_file_name = c("e1", "e2", "e3",
"e4", "e5", "e6", "e7", "e8"), Tab = c("Demographics",
"Demographics", "PatientRegister", "PatientRegister", "PatientRegister",
"PatientRegister", "PatientConsent", "PatientConsent"), DatasetName = c("Demographics Merged",
NA, "Patient Register", NA, NA, NA, "Patient Consent", NA), GroupVar1 = c( "Subject",
NA, "Subject", NA, NA, NA,
NA, NA)), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"))
방법에 대한 조언이 있습니까? 감사
답변
1 IRTFM Nov 19 2020 at 20:04
여기에 효과가 있다고 생각합니다. map_rule1
규칙 세트의 정리 된 버전에서 테스트했습니다. 트랩하거나 사전 e6
정리 해야 할 두 가지 오류 소스가 있습니다. 1) 정의되지 않았고 2) 누락 된 병합을 처리하는 방법을 알아 내기로 결정했습니다. - by
열은 내가 느끼지 못했던 추가적인 수준의 복잡성이었습니다.
temp <- lapply( split(map_rule1, map_rule1$Tab) , # breaks into groups by Domain function( d){ assign( d$DatasetName[1],
# names= first items in col
# I don't generally use assign but seems reasonable here
Reduce( function(x,y){ merge(x,y, by=d$GroupVar1[1])}, lapply(d$EDC_file_name, get) ) ,
#use first item as named by-argument
envir=globalenv() )}
# named objects need to appear outside this function
)
#need to run this before calculating `temp`
map_rule1 <-
structure(list(EDC_file_name = c("e1", "e2", "e3", "e4", "e5"
), Tab = c("Demographics", "Demographics", "PatientRegister",
"PatientRegister", "PatientRegister"), DatasetName = c("Demographics Merged",
NA, "Patient Register", NA, NA), GroupVar1 = c("Subject", NA,
"Subject", NA, NA)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
----------- 결과 -------
# First what was in temp
str(temp)
List of 2
$ Demographics :'data.frame': 27 obs. of 3 variables: ..$ Subject: Factor w/ 27 levels "300-0001","300-0002",..: 1 2 3 4 5 6 7 8 9 10 ...
..$ SEX : Factor w/ 2 levels "Female","Male": 2 1 2 1 2 1 2 2 2 2 ... ..$ RACE : Factor w/ 2 levels "Black (including African, Caribbean descent)",..: 2 2 2 2 2 1 2 2 2 2 ...
$ PatientRegister:'data.frame': 27 obs. of 4 variables: ..$ Subject : Factor w/ 27 levels "300-0001","300-0002",..: 1 2 3 4 5 6 7 8 9 10 ...
..$ ETHNIC_STD: int [1:27] 2 2 2 2 2 2 2 2 2 2 ... ..$ subjectId : int [1:27] 168 171 174 199 175 196 207 208 213 315 ...
..$ siteid : int [1:27] 9 9 9 9 9 9 9 9 9 15 ...
# Second the results in the global environment
# with the weird un-Rish names containing spaces
`Demographics Merged`
Subject SEX RACE
1 300-0001 Male Caucasian
2 300-0002 Female Caucasian
3 300-0003 Male Caucasian
4 300-0004 Female Caucasian
5 300-0005 Male Caucasian
6 300-0006 Female Black (including African, Caribbean descent)
7 300-0007 Male Caucasian
8 300-0008 Male Caucasian
9 300-0009 Male Caucasian
10 301-0001 Male Caucasian
11 301-0002 Female Caucasian
12 301-0003 Male Caucasian
13 301-0004 Male Caucasian
14 301-0005 Male Black (including African, Caribbean descent)
15 301-0006 Male Caucasian
16 302-0001 Male Caucasian
17 303-0001 Male Caucasian
18 303-0002 Male Black (including African, Caribbean descent)
19 303-0003 Male Caucasian
20 303-0004 Male Caucasian
21 304-0001 Male Caucasian
22 304-0002 Male Caucasian
23 304-0003 Female Black (including African, Caribbean descent)
24 304-0004 Male Black (including African, Caribbean descent)
25 304-0005 Male Black (including African, Caribbean descent)
26 304-0006 Female Caucasian
27 304-0007 Male Caucasian
결과를에 lapply
할당하지 않고 코드를 실행하기 만하면 작업 공간에서 Rish 이름이 지정되지 않은 결과를 얻을 수 temp
있습니다.