当某些元素是彼此的同义词时计算唯一元素

时间:2017-12-11 18:19:23

标签: r unique overlap synonym

我正在尝试计算此列表中的独特药物数量。

my_drugs=c('a', 'b', 'd', 'h', 'q')

我有以下字典,它给了我药物同义词,但它没有设置,因此定义仅适用于独特的药物:

dictionary <- read.table(header=TRUE, text="
  drug   names
  a    b;c;d;x
  x    b;c;q
  r    h;g;f
  l   m;n
")

因此,在这种情况下,列表中有2种独特的药物(因为a,直接或间接地具有同义词b,d,q)。同义词的同义词计为同义词。

我尝试过的方法是首先制作一本左侧只有独特药物的字典。要做到这一点,我会循环通过词典$ drug,grep in dictionary $ drug和dictionary $ synonyms,取出那些联合并替换药物$同义词,然后从字典中删除其他行。

bigdf=dictionary

  small_df=data.frame("drug"=NA,"names"=NA)

  for(i in 1:nrow(bigdf)){
    search_term=sprintf("*%s*",bigdf$drug[i])
    index=grep(search_term,bigdf$names)
    list=bigdf$names[index]
    list=Reduce(union,list)
    list=paste(list, collapse=";")

    if(!list==""){

    new_row=data.frame("drug"=bigdf$drug[index][1],"names"=list)
    small_df=rbind(small_df,new_row)
    #small_df
    bigdf=bigdf[-index,]
    #dim(bigdf)

    }
    else{
      new_row=data.frame("drug"=bigdf$drug[index][1],"names"="alreadycounted")
      small_df=rbind(small_df,new_row)
    }    
  }

这不起作用(small_df中缺少一些药物),即使它我不知道如何使用我的新词典来计算我列表中的独特药物数量。

如何计算my_drugs中独特药物的数量?

感谢您的帮助,如果需要进一步说明,请与我们联系。

数据集大小:my_drugs中有200个元素,字典中有2000个元素,每个药物有10-12个同义词。

1 个答案:

答案 0 :(得分:1)

$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 26214400) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "pdf"  && $imageFileType != "JPG"
&& $imageFileType != "PNG"  && $imageFileType != "JPEG"  && $imageFileType != "GIF"
&& $imageFileType != "PDF" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}