Reputation: 423
I have two different tables that I got using the table()
command (on a matrix). The first table has around 200 words and their frequencies of appearance, and the second table has around 400 words and their frequencies of appearance. I want to know how many times each word appeared in the 1st table and the 2nd table (not the total amount of appearances).
Upvotes: 0
Views: 78
Reputation: 23241
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
>
I used pacman
here, but you could also just install the package normally if you don't have it and use require
or library
Upvotes: 1