【问题标题】:Need help translating Python into R for text data需要帮助将 Python 翻译成 R 以获取文本数据
【发布时间】:2017-10-26 03:12:10
【问题描述】:

我正在尝试创建一个仅保留一个数据集中存在于另一个数据集中的单词的数据集。我知道如何在 Python 中执行此操作,但需要在 R 中执行此操作。 这是两个示例数据集。我想从 text_A 返回 text_B 中存在的单词。

text_A= ['apple','cherry','kiwi','Tangerine','apple','apple','kiwi']
text_B= ['apple','banana','grapefruit','kiwi','mango','papaya', 'tangerine']

combined_words= [ ]
for x_words in text_A:
    if x_words in text_B:
        combined_words.append(x_words)

print(combined_words)

输出应该是: '苹果','猕猴桃','apple','apple','猕猴桃'

【问题讨论】:

  • 你的意思是intersect(text_A, text_B)

标签: python r text nlp


【解决方案1】:
text_A <- c('apple','cherry','kiwi','Tangerine','apple','apple','kiwi')
text_B <- c('apple','banana','grapefruit','kiwi','mango','papaya', 'tangerine')

text_A[text_A %in% text_B]
## [1] "apple" "kiwi"  "apple" "apple" "kiwi" 

【讨论】:

  • 这正是我想要的。谢谢!
【解决方案2】:
text_A= c('apple','cherry','kiwi','Tangerine','apple','apple','kiwi')
text_B= c('apple','banana','grapefruit','kiwi','mango','papaya', 'tangerine')

intersect(text_A, text_B)
[1] "apple" "kiwi" 

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2018-05-13
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多