使用dplyr过滤器功能时,如何在列表中过滤不匹配?

时间:2018-03-16 16:54:48

标签: r dplyr

我有一个数据框列表outcome_split[[i]],其中包含状态明智的医院数据,其中每个数据框都有一个等级列。

我想提取匹配等级的医院名称和州,所以我使用下面的代码得到了输出。

> outcome_rank_filter[[i]] <- outcome_split[[i]] %>% filter(rank==num) %>% select(hospital_name, state)
> head(rankall("heart attack", 20), 10)
                                                      hospital_name state
    1                                D W MCMILLAN MEMORIAL HOSPITAL    AL
    2                             ARKANSAS METHODIST MEDICAL CENTER    AR
    3                           JOHN C LINCOLN DEER VALLEY HOSPITAL    AZ
    4                                         SHERMAN OAKS HOSPITAL    CA
    5                                      SKY RIDGE MEDICAL CENTER    CO
    6                                       MIDSTATE MEDICAL CENTER    CT
    7                                SOUTH FLORIDA BAPTIST HOSPITAL    FL
    8                                 UPSON REGIONAL MEDICAL CENTER    GA
    9                                       COVENANT MEDICAL CENTER    IA
    10 JESSE BROWN VA MEDICAL CENTER - VA CHICAGO HEALTHCARE SYSTEM    IL

但我想要&#39; NA&#39;插入没有匹配的地方。由于过滤函数完全匹配,因此忽略了结果。

我稍后会传递outcome_rank_filter[[i]],如下所示,因此可以将其折叠为一个数据框。

 output <- do.call(rbind, outcome_rank_filter)

我希望结果如下所示,它会因为不匹配而返回NA。我尝试了合并功能但无法达到预期效果。

**> head(rankall("heart attack", 20), 10)
hospital                                    state
AK <NA>                                       AK
AL D W MCMILLAN MEMORIAL HOSPITAL             AL
AR ARKANSAS METHODIST MEDICAL CENTER          AR
AZ JOHN C LINCOLN DEER VALLEY HOSPITAL        AZ
CA SHERMAN OAKS HOSPITAL                      CA
CO SKY RIDGE MEDICAL CENTER                   CO
CT MIDSTATE MEDICAL CENTER                    CT
DC <NA>                                       DC
DE <NA>                                       DE
FL SOUTH FLORIDA BAPTIST HOSPITAL             FL**

如果有人可以帮助我,我将不胜感激,

示例数据,

> dput(head(outcome_split,3))
structure(list(AK = structure(list(hospital_name = c("ALASKA NATIVE MEDICAL CENTER", 
"ALASKA REGIONAL HOSPITAL", "BARTLETT REGIONAL HOSPITAL", "CENTRAL PENINSULA GENERAL HOSPITAL", 
"CORDOVA COMMUNITY MEDICAL CENTER", "FAIRBANKS MEMORIAL HOSPITAL", 
"MAT-SU REGIONAL MEDICAL CENTER", "MT EDGECUMBE HOSPITAL", "NORTON SOUND REGIONAL HOSPITAL", 
"PEACEHEALTH KETCHIKAN MEDICAL             CENTER", "PROVIDENCE ALASKA MEDICAL CENTER", 
"PROVIDENCE KODIAK ISLAND MEDICAL CTR", "PROVIDENCE SEWARD HOSPITAL", 
"PROVIDENCE VALDEZ MEDICAL CENTER", "SITKA COMMUNITY HOSPITAL", 
"SOUTH PENINSULA HOSPITAL", "YUKON KUSKOKWIM DELTA REG HOSPITAL"
), state = c("AK", "AK", "AK", "AK", "AK", "AK", "AK", "AK", 
"AK", "AK", "AK", "AK", "AK", "AK", "AK", "AK", "AK"), heart_attack = c(15.7, 
14.5, NA, NA, NA, 15.5, 17.7, NA, NA, NA, 13.4, NA, NA, NA, NA, 
NA, NA), heart_failure = c("11.6", "13.4", "11.6", "11.6", "Not Available", 
"15.6", "11.4", "Not Available", "Not Available", "11.4", "12.4", 
"Not Available", "Not Available", "Not Available", "Not Available", 
"10.8", "11.2"), pneumonia = c("15.5", "12.5", "11.6", "13.8", 
"Not Available", "13.4", "12.1", "14.2", "11.6", "11.3", "10.5", 
"12.0", "Not Available", "Not Available", "11.5", "12.2", "9.7"
), rank = c(4L, 2L, 6L, 7L, 8L, 3L, 5L, 9L, 10L, 11L, 1L, 12L, 
13L, 14L, 15L, 16L, 17L)), class = "data.frame", .Names = c("hospital_name", 
"state", "heart_attack", "heart_failure", "pneumonia", "rank"
), row.names = c(NA, -17L)), AL = structure(list(hospital_name = c("ANDALUSIA REGIONAL HOSPITAL", 
"ATHENS-LIMESTONE HOSPITAL", "ATMORE COMMUNITY HOSPITAL", "BAPTIST MEDICAL CENTER EAST", 
"BAPTIST MEDICAL CENTER SOUTH", "BAPTIST MEDICAL CENTER-PRINCETON", 
"BIBB MEDICAL CENTER", "BIRMINGHAM VA MEDICAL CENTER", "BROOKWOOD MEDICAL CENTER", 
"BRYAN W WHITFIELD MEM HOSP INC", "BULLOCK COUNTY HOSPITAL", 
"CALLAHAN EYE FOUNDATION HOSPITAL", "CHEROKEE MEDICAL CENTER", 
"CHILTON MEDICAL CENTER", "CITIZENS BAPTIST MEDICAL CENTER", 
"CLAY COUNTY HOSPITAL", "COMMUNITY HOSPITAL INC", "COOPER GREEN MERCY HOSPITAL", 
"COOSA VALLEY MEDICAL CENTER", "CRENSHAW COMMUNITY HOSPITAL", 
"CRESTWOOD MEDICAL CENTER", "CULLMAN REGIONAL MEDICAL CENTER", 
"D C H REGIONAL MEDICAL CENTER", "D W MCMILLAN MEMORIAL HOSPITAL", 
"DALE MEDICAL CENTER", "DECATUR GENERAL HOSPITAL", "DEKALB REGIONAL MEDICAL CENTER", 
"EAST ALABAMA MEDICAL CENTER AND SNF", "ELBA GENERAL HOSPITAL", 
"ELIZA COFFEE MEMORIAL HOSPITAL", "ELMORE COMMUNITY HOSPITAL", 
"EVERGREEN MEDICAL CENTER", "FAYETTE MEDICAL CENTER", "FLORALA MEMORIAL HOSPITAL", 
"FLOWERS HOSPITAL", "GADSDEN REGIONAL MEDICAL CENTER", "GEORGE H. LANIER MEMORIAL HOSPITAL", 
"GEORGIANA HOSPITAL", "GREENE COUNTY HOSPITAL", "GROVE HILL MEMORIAL HOSPITAL", 
"HALE COUNTY HOSPITAL", "HELEN KELLER MEMORIAL HOSPITAL", "HIGHLANDS MEDICAL CENTER", 
"HILL HOSPITAL OF SUMTER COUNTY", "HUNTSVILLE HOSPITAL", "INFIRMARY WEST", 
"J PAUL JONES HOSPITAL", "JACK HUGHSTON MEMORIAL HOSPITAL", "JACKSON HOSPITAL & CLINIC INC", 
"JACKSON MEDICAL CENTER", "JACKSONVILLE MEDICAL CENTER", "L V STABLER MEMORIAL HOSPITAL", 
"LAKE MARTIN COMMUNITY HOSPITAL", "LAKELAND COMMUNITY HOSPITAL", 
"LAWRENCE MEDICAL CENTER", "MARION REGIONAL MEDICAL CENTER", 
"MARSHALL MEDICAL CENTER NORTH", "MARSHALL MEDICAL CENTER SOUTH", 
"MEDICAL CENTER BARBOUR", "MEDICAL CENTER ENTERPRISE", "MEDICAL WEST, AN AFFILIATE OF UAB HEALTH SYSTEM", 
"MIZELL MEMORIAL HOSPITAL", "MOBILE INFIRMARY", "MONROE COUNTY HOSPITAL", 
"NORTH BALDWIN INFIRMARY", "NORTHEAST ALABAMA REGIONAL MED CENTER", 
"NORTHWEST MEDICAL CENTER", "PARKWAY MEDICAL CENTER", "PICKENS COUNTY MEDICAL CENTER", 
"PRATTVILLE BAPTIST HOSPITAL", "PROVIDENCE HOSPITAL", "RED BAY HOSPITAL", 
"RIVERVIEW REGIONAL MEDICAL CENTER", "RUSSELL HOSPITAL", "RUSSELLVILLE HOSPITAL", 
"SHELBY BAPTIST MEDICAL CENTER", "SHOALS HOSPITAL", "SOUTH BALDWIN REGIONAL MEDICAL CENTER", 
"SOUTHEAST ALABAMA MEDICAL CENTER", "SPRINGHILL MEDICAL CENTER", 
"ST VINCENT'S BIRMINGHAM", "ST VINCENT'S EAST", "ST VINCENT'S ST CLAIR", 
"ST VINCENTS BLOUNT", "STRINGFELLOW MEMORIAL HOSPITAL", "THOMAS HOSPITAL", 
"TRINITY MEDICAL CENTER", "TROY REGIONAL MEDICAL CENTER", "TUSCALOOSA VA MEDICAL CENTER", 
"UNIV OF S AL CHILDREN'S & WOMEN'S HOS", "UNIV OF SOUTH ALABAMA MEDICAL CENTER", 
"UNIVERSITY OF ALABAMA HOSPITAL", "VA CENTRAL ALABAMA HEALTHCARE SYSTEM - MONTGOMERY", 
"VAUGHAN REG MED CENTER PARKWAY CAMPUS", "WALKER BAPTIST MEDICAL CENTER", 
"WASHINGTON COUNTY HOSPITAL", "WEDOWEE HOSPITAL", "WIREGRASS MEDICAL CENTER"
), state = c("AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL", "AL"), heart_attack = c(NA, 15, NA, 14.2, 17.8, 14.9, NA, 
16.1, 16.5, NA, NA, NA, NA, NA, 17.3, 16.7, 17.1, NA, 15.2, NA, 
13.3, 17.1, 15.8, 15.7, 17.3, 16.8, 18, 16.3, NA, 18.1, NA, NA, 
16.7, NA, 15.2, 16.7, 15.4, 14.5, NA, NA, NA, 19.6, 15, NA, 15.2, 
NA, NA, NA, 17.5, NA, NA, NA, NA, NA, 15.6, NA, NA, 18.5, NA, 
16.6, 15.3, NA, 19.3, NA, NA, 15.6, NA, 15.8, NA, 14.6, 15.2, 
NA, 16.9, 17.1, NA, 15.9, NA, 15.8, 14.3, 16, 16.2, 17.7, NA, 
NA, 16.4, 14.7, 16.8, NA, NA, NA, NA, 15, NA, 14.7, 17, NA, NA, 
NA), heart_failure = c("10.1", "11.7", "10.8", "9.6", "11.8", 
"11.4", "14.0", "10.4", "13.5", "11.7", "12.3", "Not Available", 
"12.1", "11.5", "14.9", "12.6", "12.3", "Not Available", "11.7", 
"13.8", "13.8", "12.1", "11.2", "14.8", "11.8", "10.9", "16.6", 
"12.9", "Not Available", "11.3", "11.3", "9.1", "11.7", "10.4", 
"12.0", "10.7", "8.8", "10.8", "11.2", "10.4", "10.7", "12.6", 
"13.4", "Not Available", "12.4", "12.5", "Not Available", "10.8", 
"10.2", "12.3", "16.4", "11.1", "10.9", "13.6", "9.9", "11.5", 
"12.5", "15.2", "13.5", "12.9", "11.4", "13.6", "10.7", "13.0", 
"11.5", "11.2", "11.8", "10.5", "12.6", "14.8", "13.5", "12.6", 
"10.8", "11.6", "14.8", "13.6", "13.6", "15.1", "11.4", "10.4", 
"10.6", "10.9", "10.8", "13.0", "12.0", "12.8", "12.9", "11.2", 
"Not Available", "Not Available", "12.5", "12.5", "12.2", "12.0", 
"10.8", "Not Available", "10.4", "10.6"), pneumonia = c("11.1", 
"12.1", "13.0", "10.2", "14.3", "11.6", "13.6", "11.0", "13.0", 
"9.1", "12.1", "Not Available", "14.7", "11.2", "12.1", "11.8", 
"11.6", "Not Available", "11.4", "15.8", "10.4", "12.1", "11.3", 
"12.6", "9.9", "11.9", "15.8", "12.1", "12.0", "13.4", "11.2", 
"12.0", "12.9", "12.1", "11.3", "14.6", "10.3", "11.3", "11.5", 
"12.1", "11.5", "15.0", "12.9", "Not Available", "14.1", "13.1", 
"11.4", "10.9", "14.7", "9.3", "19.2", "13.0", "10.8", "10.7", 
"9.8", "10.0", "8.7", "13.9", "15.0", "12.9", "12.1", "14.9", 
"12.5", "15.6", "14.6", "13.2", "13.1", "11.9", "12.4", "14.2", 
"10.6", "11.6", "12.7", "14.9", "11.5", "10.7", "12.8", "9.8", 
"10.9", "13.8", "12.6", "16.2", "11.4", "15.3", "12.0", "13.1", 
"13.9", "11.1", "Not Available", "Not Available", "Not Available", 
"12.7", "11.3", "14.0", "11.9", "Not Available", "13.9", "12.3"
), rank = c(52L, 9L, 53L, 2L, 46L, 8L, 54L, 26L, 30L, 55L, 56L, 
57L, 58L, 59L, 42L, 32L, 39L, 60L, 12L, 61L, 1L, 40L, 21L, 20L, 
43L, 35L, 47L, 28L, 62L, 48L, 63L, 64L, 33L, 65L, 13L, 34L, 17L, 
4L, 66L, 67L, 68L, 51L, 10L, 69L, 14L, 70L, 71L, 72L, 44L, 73L, 
74L, 75L, 76L, 77L, 18L, 78L, 79L, 49L, 80L, 31L, 16L, 81L, 50L, 
82L, 83L, 19L, 84L, 22L, 85L, 5L, 15L, 86L, 37L, 41L, 87L, 24L, 
88L, 23L, 3L, 25L, 27L, 45L, 89L, 90L, 29L, 6L, 36L, 91L, 92L, 
93L, 94L, 11L, 95L, 7L, 38L, 96L, 97L, 98L)), class = "data.frame", .Names = c("hospital_name", 
"state", "heart_attack", "heart_failure", "pneumonia", "rank"
), row.names = c(NA, -98L)), AR = structure(list(hospital_name = c("ARKANSAS CHILDREN'S HOSPITAL", 
"ARKANSAS HEART HOSPITAL", "ARKANSAS METHODIST MEDICAL CENTER", 
"ARKANSAS SURGICAL HOSPITAL", "ASHLEY COUNTY MEDICAL CENTER", 
"BAPTIST HEALTH MEDICAL CENTER HEBER SPINGS", "BAPTIST HEALTH MEDICAL CENTER NORTH LITTLE ROCK", 
"BAPTIST HEALTH MEDICAL CENTER-LITTLE ROCK", "BAPTIST HEALTH MEDICAL CENTER-STUTTGART", 
"BAPTIST HEALTH MEDICSL CENTER-ARKADELPHIA", "BAXTER REGIONAL MEDICAL CENTER", 
"BOONEVILLE COMMUNITY HOSPITAL", "BRADLEY COUNTY MEDICAL CENTER", 
"CHICOT MEMORIAL MEDICAL CENTER", "COMMUNITY MEDICAL CENTER IZARD COUNTY", 
"CONWAY REGIONAL MEDICAL CENTER", "CRITTENDEN MEMORIAL HOSPITAL", 
"CROSSRIDGE COMMUNITY HOSPITAL", "DALLAS COUNTY MEDICAL CENTER", 
"DE QUEEN MEDICAL CENTER, INC", "DELTA MEMORIAL HOSPITAL", "DEWITT HOSPITAL & NURSING HOME, INC", 
"DREW MEMORIAL HOSPITAL", "EUREKA SPRINGS HOSPITAL, LLC", "FAYETTEVILLE AR VA MEDICAL CENTER", 
"FIVE RIVERS MEDICAL CENTER", "FORREST CITY MEDICAL CENTER", 
"GREAT RIVER MEDICAL CENTER", "HARRIS HOSPITAL", "HELENA REGIONAL MEDICAL CENTER", 
"HOT SPRING COUNTY MEDICAL CENTER", "HOWARD MEMORIAL HOSPITAL", 
"JEFFERSON REGIONAL MEDICAL CENTER", "JOHN ED CHAMBERS MEM HOSP, INC", 
"JOHNSON REGIONAL MEDICAL CENTER", "LAWRENCE MEMORIAL HOSPITAL", 
"LEO N LEVI NATIONAL ARTHRITIS HOSPITAL", "LITTLE RIVER MEMORIAL HOSPITAL", 
"MAGNOLIA HOSPITAL", "MCGEHEE HOSPITAL", "MEDICAL CENTER SOUTH ARKANSAS", 
"MEDICAL PARK HOSPITAL", "MENA REGIONAL HEALTH SYSTEM", "MERCY HOSPITAL SCOTT COUNTY", 
"MERCY HOSPITAL TURNER MEMORIAL", "MERCY MEDICAL CENTER", "NATIONAL PARK MEDICAL CENTER", 
"NEA BAPTIST MEMORIAL HOSPITAL", "NORTH ARKANSAS REGIONAL MEDICAL CENTER", 
"NORTH LOGAN MERCY HOSPITAL", "NORTH METRO MEDICAL CENTER", "NW ARKANSAS HOSPITALS, LLC", 
"OUACHITA COUNTY MEDICAL CENTER", "OZARK HEALTH", "OZARKS COMMUNITY HOSPITAL OF GRAVETTE", 
"PHYSICIANS' SPECIALTY HOSPITAL", "PIGGOTT COMMUNITY HOSPITAL", 
"RIVER VALLEY MEDICAL CENTER", "SALINE MEMORIAL HOSPITAL", "SILOAM SPRINGS MEMORIAL HOSPITAL", 
"SOUTH MISSISSISSPI COUNTY REGIONAL MEDICAL CENTER", "SPARKS REGIONAL MEDICAL CENTER", 
"ST BERNARDS MEDICAL CTR", "ST EDWARD MERCY MEDICAL CENTER", 
"ST JOHN'S HOSPITAL-BERRYVILLE", "ST JOSEPHS MERCY HEALTH CENTER INC", 
"ST MARYS REGIONAL MEDICAL CENTER", "ST VINCENT INFIRMARY MEDICAL CENTER", 
"ST VINCENT MEDICAL CENTER/NORTH", "ST VINCENT MORRILTON", "STONE COUNTY MEDICAL CENTER", 
"SUMMIT MEDICAL CENTER", "UAMS MEDICAL CENTER", "VA CENTRAL AR. VETERANS HEALTHCARE SYSTEM LR", 
"WASHINGTON REGIONAL MED CTR AT NORTH HILLS", "WHITE COUNTY MEDICAL CENTER", 
"WHITE RIVER MEDICAL CENTER"), state = c("AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", "AR", 
"AR", "AR", "AR", "AR", "AR", "AR", "AR"), heart_attack = c(NA, 
11.9, 17.1, NA, NA, NA, 14.4, 14.7, 15.7, NA, 16.4, NA, NA, NA, 
NA, 18.9, 14.4, NA, NA, NA, NA, NA, NA, NA, 17.1, NA, NA, NA, 
17.3, NA, 15.8, NA, 16.8, 17.3, 16.9, NA, NA, NA, NA, NA, 21.9, 
17.3, NA, NA, NA, 14.8, 19.1, 14.5, 17.2, NA, 17.1, 17.3, NA, 
NA, NA, NA, NA, NA, 15.7, 15.6, NA, 16.7, 17.7, 14.9, NA, 17.4, 
18.8, 16.9, NA, NA, NA, NA, 16.5, 14.5, 15.2, 17.5, 17.6), heart_failure = c("Not Available", 
"13.3", "14.4", "Not Available", "13.1", "11.5", "13.3", "11.1", 
"11.0", "11.7", "14.7", "15.7", "12.6", "12.3", "14.8", "15.6", 
"10.9", "11.0", "12.1", "13.2", "12.6", "12.5", "12.9", "13.7", 
"10.2", "14.0", "12.8", "14.2", "11.3", "12.1", "14.4", "11.9", 
"13.2", "10.0", "11.8", "10.9", "Not Available", "10.7", "14.9", 
"12.8", "13.6", "12.7", "13.5", "12.5", "Not Available", "13.0", 
"13.5", "17.2", "11.2", "13.8", "9.6", "11.9", "12.5", "12.4", 
"Not Available", "Not Available", "11.6", "11.9", "14.4", "11.8", 
"12.0", "14.0", "14.6", "12.6", "11.7", "14.3", "13.1", "12.8", 
"12.6", "13.2", "12.1", "14.0", "12.1", "9.0", "13.9", "13.7", 
"12.5"), pneumonia = c("Not Available", "10.4", "11.7", "Not Available", 
"14.3", "12.0", "10.2", "11.0", "14.2", "12.6", "16.1", "12.8", 
"12.7", "14.0", "12.3", "14.9", "12.6", "11.2", "15.7", "11.6", 
"11.7", "13.0", "13.5", "13.7", "16.9", "11.4", "15.3", "15.2", 
"12.1", "13.3", "11.5", "16.7", "11.0", "12.1", "13.8", "12.4", 
"Not Available", "12.3", "12.8", "15.3", "14.6", "15.1", "13.3", 
"13.8", "13.0", "13.4", "13.3", "11.3", "11.8", "11.6", "13.4", 
"11.5", "12.4", "13.4", "12.0", "Not Available", "11.3", "17.9", 
"12.2", "13.8", "10.8", "12.2", "13.3", "12.8", "12.4", "11.3", 
"15.4", "12.0", "11.1", "15.2", "9.9", "12.8", "12.6", "12.1", 
"13.4", "12.5", "12.1"), rank = c(36L, 1L, 20L, 37L, 38L, 39L, 
2L, 6L, 11L, 40L, 14L, 41L, 42L, 43L, 44L, 33L, 3L, 45L, 46L, 
47L, 48L, 49L, 50L, 51L, 21L, 52L, 53L, 54L, 24L, 55L, 13L, 56L, 
17L, 25L, 18L, 57L, 58L, 59L, 60L, 61L, 35L, 26L, 62L, 63L, 64L, 
7L, 34L, 4L, 23L, 65L, 22L, 27L, 66L, 67L, 68L, 69L, 70L, 71L, 
12L, 10L, 72L, 16L, 31L, 8L, 73L, 28L, 32L, 19L, 74L, 75L, 76L, 
77L, 15L, 5L, 9L, 29L, 30L)), class = "data.frame", .Names = c("hospital_name", 
"state", "heart_attack", "heart_failure", "pneumonia", "rank"
), row.names = c(NA, -77L))), .Names = c("AK", "AL", "AR"))

1 个答案:

答案 0 :(得分:1)

你想要的不是过滤,你只是想根据我想的另一列改变一个列。因此,您只需将mutateifelse

一起使用即可
df[[i]] %>% 
  mutate(hospital_name = ifelse(rank == num, hospital_name, NA)) %>% 
  select(hospital_name, state)

编辑:按州,您可以先获得结果。然后转换为一个因子(如果它还没有),然后使用NA填充缺失的因子。 E.g。

df[[3]] %>% filter(rank == 20) %>% select(hospital_name, state) %>%
  mutate(state = factor(state, levels = state.abb)) %>%
  complete(state, fill = list(hospital_name = NA))

如果您从多个列表加入,当然您应该在最后一步中进行因子转换+ complete

# A tibble: 50 x 2
   state hospital_name                    
   <fct> <chr>                            
 1 AL    NA                               
 2 AK    NA                               
 3 AZ    NA                               
 4 AR    ARKANSAS METHODIST MEDICAL CENTER
 5 CA    NA                               
 6 CO    NA                               
 7 CT    NA  
....

编辑2:

所以,通过查看RPubs上的函数,我写了与你的问题相关的部分如下:

rankall <- function(data, outcome, num = "best") {
  outcome_split <- data

  output <- do.call(
    rbind, lapply(outcome_split, function(x) {
    x <- x[order(x$hospital_name), ]
    x <- x %>% mutate(rank = rank(x[, outcome], ties.method = "first"))
    x %>% filter(rank == num) %>% select(hospital_name, state)
    })
  )

  output <- output %>% mutate(state = factor(state, levels = state.abb)) %>%
    complete(state, fill = list(hospital_name = NA))

  return(output)
}

我直接用lapplyrbind结果替换了for循环。 然后,在我们获得过滤器所在州的结果后,我们用NA填写缺失的状态。

然后,

rankall(df, "rank", 20)

其中df是此问题的数据,产量

# A tibble: 50 x 2
   state hospital_name                    
   <fct> <chr>                            
 1 AL    D W MCMILLAN MEMORIAL HOSPITAL   
 2 AK    NA                               
 3 AZ    NA                               
 4 AR    ARKANSAS METHODIST MEDICAL CENTER
 5 CA    NA                               
 6 CO    NA                               
 7 CT    NA                               
 8 DE    NA                               
 9 FL    NA                               
10 GA    NA                               
# ... with 40 more rows

请注意,您不必在此处拆分数据。

同样,将问题中的数据用作df

full <- do.call(rbind, df)

我认为,在您拆分数据之前,这就是数据的形状。然后,仅使用dplyr

num = 20
full %>% 
  group_by(state) %>% 
  filter(rank == num) %>% 
  slice(1:1) %>%
  ungroup() %>%
  select(hospital_name, state) %>%
  mutate(state = factor(state, levels = state.abb)) %>%
  complete(state, fill = list(hospital_name = NA))

会给我完全相同的结果。