简体   繁体   中英

How can I select the values that only appear once in the column of a data table in R?

Like the title, the question is very straightforward. (pardon my ignorance)

I have a column, character type, in a data table. And there are several different words/values stored, some of them only appear once, others appear multiple times.

How can I select out the ones that only appear once?? Any help is appreciated! Thank you!

One option would be to do a group by and then select the groups having single row

library(data.table)
dt1  <- dt[, .SD[.N == 1], .(col)]
library(dplyr)

df %>%
    group_by(column) %>%
    dplyr::filter(n() == 1) %>%
    ungroup()

Example:

data = tibble(text = c("a","a","b","c","c","c"))

data %>%
    group_by(text) %>%
    dplyr::filter(n() == 1) %>%
    ungroup()

# A tibble: 1 x 1
  text 
  <chr>
1 b   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM