如何在R中检查类别是否匹配,其中类别存储在不同的文件中,并且元素可以有多个类别?

时间:2017-05-03 06:25:41

标签: r match categories

我有两个表,Catdata和ElementData。如果ElementData$MatchCatdata$categoryfictionsuspense,我希望thriller返回1,否则返回0。如果没有循环,我该怎么做?

Catdata

Element  Category  
abc123   thriller  
abc123   horror  
def456   fiction  
def456   suspense  
def456   thriller  
pqr789   romance  
pqr789   fiction  
xyz123   thriller  

elementData中

Element                     Match  
def456  
abc123  
xyz123  
pqr789  

1 个答案:

答案 0 :(得分:0)

如果ElementData$Match的{​​{1}}对应Catdata$category的{​​{1}}的任何值与Catdata$Element匹配,则library(dplyr) ## Create lookup table of all elements matching the criteria: matchData <- Catdata %>% group_by(Element) %>% dplyr::summarise(Match = (Category %in% c("fiction", "suspense", "thriller")) %>% any %>% as.integer) ## Merge your second table with the lookup table ElementData <- ElementData %>% left_join(matchData, by = "Element") 返回1,您可以执行以下操作:

`<?php for($i=1; $i<=30; $i++){ ?>
<div class="form-group c">
    <div class="col-xs-12 col-sm-12">
        <div class="input-group">
            <select name="istbs_code_<?php echo $i; ?>" class="dropchange col-xs-12 col-sm-6 required-field form-control" id="istbs_code_<?php echo $i; ?>" placeholder="ISTBS Code" tabindex="1" data-style="form-control">
                <option value="">Select Option</option>
            </select>
        </div>
    </div>
</div>
<?php } ?>`

        $(document).on("change",".dropchange ",function(){
           var thisid=this.id;
            var json = {};
            var abc = json['iat_code_1'] = $(this).val();
            var request = callajax(thisid);
        });

    function callajax(thisid){
    $.ajax({
                url: "<?php echo base_url($controller.'/get_product_attributes'); ?>",
                type: "POST",
                data: json,
                dataType: "html",
                success : function(response){
                    $("#"+thisid).html(response);
                }
            });   
    }