2016-06-09 62 views
0

我有兩個不同的表,我使用table()命令(在矩陣上)。第一張表格大約有200字以及它們的外觀頻率,第二張表格大約有400字以及它們的外觀頻率。我想知道每個單詞在第一個表格和第二個表格中出現的次數(不是出現的總數量)。如何比較R中的表項?

+0

嘗試'合併(表1,表2,通過=「字」,全部= T)'。 – Psidom

+2

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

回答

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這裏,但你也可以只安裝包一般來說,如果你沒有它,使用requirelibrary

+0

現在假設x也出現了「蝙蝠」這個詞。我希望字樣在下表中出現時對齊,所以如果表2中沒有出現蝙蝠,則頻率應該顯示爲0.我該怎麼做? – Green

+0

@ A.Manimaran現在確定它已經更新(「女性」一詞僅在表2中)。如果有幫助,請隨時註冊並選擇此解決方案作爲解決方案。 :) –