我有兩個不同的表,我使用table()
命令(在矩陣上)。第一張表格大約有200字以及它們的外觀頻率,第二張表格大約有400字以及它們的外觀頻率。我想知道每個單詞在第一個表格和第二個表格中出現的次數(不是出現的總數量)。如何比較R中的表項?
0
A
回答
1
x <- c("a", "the", "cat", "dog", "money", "dog", "money", "dog", "money")
y <- c("a", "the", "cat", "cat", "cat", "dog", "money", "dog", "money", "women")
xx <- table(x)
yy <- table(y)
xx <- data.frame(xx) # Get it out of the table class
colnames(xx) <- c("word", "table1_freq") # name columns appropriately
yy <- data.frame(yy) # Get it out of the table class
colnames(yy) <- c("word", "table2_freq") # name columns appropriately
pacman::p_load(rowr)
result <- cbind.fill(xx,yy, fill=NA)
# Now to replace the NA's with what you requested in the comment:
result$table1_freq <- as.numeric(result$table1_freq)
result$table2_freq <- as.numeric(result$table2_freq)
result$table1_freq[is.na(result$table1_freq)] <- 0
result$table2_freq[is.na(result$table2_freq)] <- 0
result[,1] <- as.character(result[,1])
result[,3] <- as.character(result[,3])
result[is.na(result[,1]),1] <- result[is.na(result[,1]),3]
result[is.na(result[,3]),3] <- result[is.na(result[,3]),1]
result
word table1_freq word table2_freq
1 a 1 a 1
2 cat 1 cat 3
3 dog 2 dog 2
4 money 2 money 2
5 the 1 the 1
6 women 0 women 1
>
我用pacman
這裏,但你也可以只安裝包一般來說,如果你沒有它,使用require
或library
+0
現在假設x也出現了「蝙蝠」這個詞。我希望字樣在下表中出現時對齊,所以如果表2中沒有出現蝙蝠,則頻率應該顯示爲0.我該怎麼做? – Green
+0
@ A.Manimaran現在確定它已經更新(「女性」一詞僅在表2中)。如果有幫助,請隨時註冊並選擇此解決方案作爲解決方案。 :) –
相關問題
- 1. 比較R中
- 2. 比較R中
- 3. 比較R中
- 4. 比較中的R
- 5. 比較R中的列表元素
- 6. 比較R中的3列列表
- 7. 在R中的矢量表比較
- 8. 比較R組中的行
- 9. 比較R中的多列
- 10. 比較R中的數據
- 11. 如何比較Sharepoint列表項目值?
- 12. 如何比較表
- 13. 如何比較表?
- 14. 如何在r中爲組比較創建表?
- 15. 如何比較C中鏈接列表中的每個項目?
- 16. R中比較小時
- 17. 分組,比較和r中
- 18. Python:比較列表中的項目
- 19. 比較列表中的項目?
- 20. 比較列表中的項目?方案
- 21. 比較列表中的項目
- 22. 比較Python中的兩個列表項
- 23. 如何在列表中的項目上運行比較功能?
- 24. 如何比較DataTable中的行與列表框項目
- 25. 如何比較兩個列表中的項目?
- 26. 如何比較列表中的項目與用戶輸入?
- 27. 如何比較列表中的項目與子字符串?
- 28. 如何在比較列表框中找到多餘的項目?
- 29. 如何獲得通用列表中的比較項目
- 30. 如何比較C#中兩個列表框的項目名稱?
嘗試'合併(表1,表2,通過=「字」,全部= T)'。 – Psidom
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –