两列之间的唯一值列

时间:2017-05-05 15:00:13

标签: r dataframe

示例数据:

col1    col2
<NA>    cc
a       a
ab      a
z       a

我想添加一个包含这些值的列unique - 任何未在col1和col2之间共享的值。

col1    col2    unique
<NA>    cc      cc
a       a   
ab      a       b
z       a       za 

我尝试使用setdiff

(用于复制目的:)

df <- read.table(header=TRUE, stringsAsFactors = FALSE, text = 
                   "col1    col2
    NA  cc
                 a      a
                 ab     a
                 z      a
                 ")

像这样:

df$unique <- paste0(setdiff(df$col1, df$col2), setdiff(df$col2, df$col1))

但它返回

Error in `$<-.data.frame`(`*tmp*`, "unique", value = c("<NA>cc", "abcc" : 
  replacement has 2 rows, data has 3

从错误看,它看起来像生成了列之间差异的向量,而不是元素之间的差异......

修改:在最后一行添加了za样本数据。

2 个答案:

答案 0 :(得分:1)

这是一个apply的长度方法。

apply(df, 1, function(i) {
              i <- i[!is.na(i)] # remove NAs
              if(length(i[!is.na(i)]) == 1) i # check length and return singletons untouched
              else { # for non-singletons
                i <- unlist(strsplit(i, split="")) # strsplit and turn into a vector
                i <- i[!(duplicated(i) | duplicated(i, fromLast=TRUE))] # drop duplicates
                paste(i, collapse="")}}) # return collapsed singleton set of characters
[1] "cc" ""   "b" 

请注意,对于c(“cc”,“a”,“c”),这将返回“a”,因为“cc”和“c”将被标记为重复。

答案 1 :(得分:1)

我们需要先拆分字符串:

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<button class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal">
  Open Modal
</button>

<div class="checkbox">
  <label>
      <input type="checkbox" id="checkbox" name="checkbox"> Check me out
    </label>
</div>

<div id="myModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>Ang Lorem Ipsum ay ginagamit na modelo ng industriya ng pagpriprint at pagtytypeset. Ang Lorem Ipsum ang naging regular na modelo simula pa noong 1500s, noong may isang di kilalang manlilimbag and kumuha ng galley ng type at ginulo ang pagkaka-ayos
          nito upang makagawa ng libro ng mga type specimen. Nalagpasan nito hindi lang limang siglo, kundi nalagpasan din nito ang paglaganap ng electronic typesetting at nanatiling parehas. Sumikat ito noong 1960s kasabay ng pag labas ng Letraset sheets
          na mayroong mga talata ng Lorem Ipsum, at kamakailan lang sa mga desktop publishing software tulad ng Aldus Pagemaker ginamit ang mga bersyon ng Lorem Ipsum.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-success " data-dismiss="modal" id="agree-button">Agree</button>
      </div>
    </div>
  </div>
</div>