【问题标题】:Filter rows in dataset for distinct words in r过滤数据集中的行以查找 r 中的不同单词
【发布时间】:2021-05-05 01:09:57
【问题描述】:

目标:过滤数据集中的行,以便只保留不同的单词 目前,我使用inner_join 保留 2 个数据集中的行,这使我在这个数据集中的行重复。

尝试 1:我尝试使用distinct 仅保留那些唯一的行,但这没有奏效。我可能用错了。

到目前为止,这是我的代码;以 png 格式附加的输出:


# join warriner emotion lemmas by `word` column in collocations data frame to see how many word matches there are

warriner2 <- dplyr::inner_join(warriner, coll, by = "word") # join data; retain only rows in both sets (works both ways)
warriner2 <- distinct(warriner2)
warriner2

coll2 <- dplyr::semi_join(coll, warriner, by = "word") # join all rows in a that have a match in b

# There are 8166 lemma matches (including double-ups)
# There are XXX unique lemma matches

【问题讨论】:

标签: r data-wrangling


【解决方案1】:

你可以试试:

library(dplyr)

warriner2 <- inner_join(warriner, coll, by = "word") %>%
                distinct(word, .keep_all = TRUE)

【讨论】:

    【解决方案2】:

    为了进一步阐明 Ronak 的答案,这里有一个带有一些模拟数据的示例。请注意,如果这是您想要的,您可以在管道的末尾使用 distinct() 来保留不同的列。您的错误很可能已经发生,因为您执行了两次操作,并且两次都将结果分配给了相同的名称 (warriner2)。

    library(dplyr)
    
    # Here's a couple sample tibbles
    name <- c("cat", "dog", "parakeet")
    
    df1 <- tibble(
            x = sample(5, 99, rep = TRUE),
            y = sample(5, 99, rep = TRUE),
            name = rep(name, times = 33))
    df2 <- tibble(
            x = sample(5, 99, rep = TRUE),
            y = sample(5, 99, rep = TRUE),
            name = rep(name, times = 33))
    
    # It's much less confusing if you do this in one pipe
    p <- df1 %>%
            inner_join(df2, by = "name") %>%
            distinct()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 2020-05-04
      • 2020-12-22
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多