Reputation: 2310
I would like to compare to a string frequence between two dataframes in R.
My first dataframe (X):
List1
Engl001
Engl002
Engl003
My second dataframe (Y):
List1 ram
Engl001 noi2
Engl001 oui5
Engl003 ki4
My expected output:
List1 Count
Engl001 2
Engl002 0
Engl003 1
Thank you!
Upvotes: 1
Views: 346
Reputation: 56004
Using factors and table:
# Dummy data
X <- read.table(text=" List1
Engl001
Engl002
Engl003", header=TRUE,colClasses = "character")
Y <- read.table(text=" List1 ram
Engl001 noi2
Engl001 oui5
Engl003 ki4", header=TRUE,colClasses = "character")
# get counts, add colnames
result <- as.data.frame(table(factor(Y$List1,levels=unique(X$List1))))
colnames(result) <- c("List1","Count")
# Output
result
# List1 Count
# 1 Engl001 2
# 2 Engl002 0
# 3 Engl003 1
Thanks to @hardingnj at Biostar
Upvotes: 0
Reputation: 21497
This would be a solution
A<-read.table(text=" List1
Engl001
Engl002
Engl003", header=TRUE,colClasses = "character")
B <- read.table(text=" List1 ram
Engl001 noi2
Engl001 oui5
Engl003 ki4", header=TRUE,colClasses = "character")
CAUTION: A$List1
and B$List1
are of class character!
tmp<-sapply(A$List1,function(x){sum(B$List1==x)})
data.frame(List1=names(tmp),Count=tmp)
Result:
List1 Count
Engl001 Engl001 2
Engl002 Engl002 0
Engl003 Engl003 1
Upvotes: 2