如何在dplyr中使用lapply

时间:2018-11-03 17:47:44

标签: r dplyr

输入

我有一个数据框,如下所示:

 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)在分组行之间的特定列中的值不相同

我遇到的问题是如何在lapplydplyr,因为我不知道在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")

1 个答案:

答案 0 :(得分:1)

这是一个state="xyz"选项

function sompret_image_creator(url, ptitle)
    {
    $.ajax(
    { 
    url: url,
    success: function(data) {
        var html = $.parseHTML( data ),
        img = $(html).find('.post').find('img').first().attr('src'),
        len = img.length; 
        if( len > 0 ){
        var src = img.first().attr("src"); // get id of image
        } else {
            console.log("Image not found");
             }
            console.log(src)
            image_tag='<img src="'+src+'" alt="'+ptitle+'"/>'
            return image_tag;
            }
        });
    }

数据

dplyr