【问题标题】:How to use lapply within dplyr如何在 dplyr 中使用 lapply
【发布时间】:2018-11-03 17:47:43
【问题描述】:

输入

我有一个如下的数据框:

 structure(list(DistalLESfromnarescm = c("31.9", "31.9", "33.1", 
"33.3", "33.8", "34.0"), LESmidpointfromnarescm = c("31.2", "31.2", 
"32.0", "32.0", "33.1", "33.2"), ProximalLESfromnarescm = c("30.1", 
"30.1", "30.9", "30.9", "31.8", "31.9"), LESlengthcm = c("1.8", 
"1.8", "2.2", "2.5", "2.0", "2.1"), EsophageallengthLESUEScenterscm = c("12.1", 
"12.1", "14.0", "15.0", "15.1", NA), PIPfromnarescm = c("37.8", 
"37.8", "No", "No", "34.3", "35.8"), Hosp_Id = c("A", "A", "B", 
"B", "C", "D")), .Names = c("DistalLESfromnarescm", "LESmidpointfromnarescm", 
"ProximalLESfromnarescm", "LESlengthcm", "EsophageallengthLESUEScenterscm", 
"PIPfromnarescm", "Hosp_Id"), row.names = c(NA, -6L), class = "data.frame")

瞄准

如果出现以下情况,我想将任意行中的值与前一行合并: a) 医院号码相同,并且 b)分组行之间的特定列中的值不同

我遇到的问题是如何在dplyr 中使用lapply,因为我不知道在 lapply 语句的左侧要引用什么。

尝试 1

    result2 <- Question %>% 
      group_by(HospNum_Id,DistalLESfromnarescm)%>%
      ifelse(HospNum_Id==lag(HospNum_Id),
lapply(WHAT DO I REFER TO HERE function(x) ifelse(x==lag(x), x,paste0(x,"::",lead(x)),"No")),"No")

期望的输出

structure(list(DistalLESfromnarescm = c("31.9",  
   "33.1:33.3", "33.8", "34.0"), LESmidpointfromnarescm = c("31.2", 
    "32.0",  "33.1", "33.2"), ProximalLESfromnarescm = c( 
    "30.1", "30.9",  "31.8", "31.9"), LESlengthcm = c( 
     "1.8", "2.2:2.5", "2.0", "2.1"), EsophageallengthLESUEScenterscm = c( 
     "12.1", "14.0:15.0", "15.1", NA), PIPfromnarescm = c( 
     "37.8", "No",  "34.3", "35.8"), Hosp_Id = c( "A",  
     "B", "C", "D")), .Names = c("DistalLESfromnarescm", "LESmidpointfromnarescm", 
     "ProximalLESfromnarescm", "LESlengthcm", "EsophageallengthLESUEScenterscm", 
     "PIPfromnarescm", "Hosp_Id"), row.names = c(NA, -4L), class = "data.frame")

【问题讨论】:

标签: r dplyr


【解决方案1】:

这是一个dplyr 选项

library(dplyr)
df1 %>% 
  group_by(Hosp_Id) %>% 
  summarise_all(.funs = function(x) paste(unique(c(dplyr::lag(x, default = NULL), x)), collapse = ":"))
# A tibble: 4 x 7
#  Hosp_Id DistalLESfromnarescm LESmidpointfromnarescm ProximalLESfromnarescm LESlengthcm EsophageallengthLESUEScenterscm PIPfromnarescm
#  <chr>   <chr>                <chr>                  <chr>                  <chr>       <chr>                           <chr>         
#1 A       31.9                 31.2                   30.1                   1.8         12.1                            37.8          
#2 B       33.1:33.3            32.0                   30.9                   2.2:2.5     14.0:15.0                       No            
#3 C       33.8                 33.1                   31.8                   2.0         15.1                            34.3          
#4 D       34.0                 33.2                   31.9                   2.1         NA                              35.8          

数据

df1 <- structure(list(DistalLESfromnarescm = c("31.9", "31.9", "33.1", 
"33.3", "33.8", "34.0"), LESmidpointfromnarescm = c("31.2", "31.2", 
"32.0", "32.0", "33.1", "33.2"), ProximalLESfromnarescm = c("30.1", 
"30.1", "30.9", "30.9", "31.8", "31.9"), LESlengthcm = c("1.8", 
"1.8", "2.2", "2.5", "2.0", "2.1"), EsophageallengthLESUEScenterscm = c("12.1", 
"12.1", "14.0", "15.0", "15.1", NA), PIPfromnarescm = c("37.8", 
"37.8", "No", "No", "34.3", "35.8"), Hosp_Id = c("A", "A", "B", 
"B", "C", "D")), .Names = c("DistalLESfromnarescm", "LESmidpointfromnarescm", 
"ProximalLESfromnarescm", "LESlengthcm", "EsophageallengthLESUEScenterscm", 
"PIPfromnarescm", "Hosp_Id"), row.names = c(NA, -6L), class = "data.frame")

【讨论】:

  • 我认为不需要lag;我只会使用function(x) paste(unique(x), collapse = ":")
  • ...也不是function(x)? d %&gt;% group_by(Hosp_Id) %&gt;% summarise_all(funs(paste(unique(.), collapse = ":")))
  • @Ista Included lag 因为 OP 写道“我想将任何行中的值与前一行合并”。如果每组只有两行,那么您和@Henrik 的方法将是正确的。
  • 这取决于您对问题的理解,但不是常识性的解释是 OP 的意思是“我想将任何行中的值与前面的行合并”(复数)。如果这不是真的,我们的任何解决方案都不会奏效;如果这是真的,我的解决方案会更加简洁。
猜你喜欢
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
相关资源
最近更新 更多