Filtering the data frame by factors in R

I have the following framework:

sp <- combn(c("sp1","sp2","sp3","sp4"),2) d <- data.frame(t(sp),"freq"=sample(0:100,6)) 

and two factors

 x1 <- as.factor(c("sp1","sp2")) x2 <- as.factor(c("sp3","sp4")) 

I need a returned dataframe containing all possible combinations of x1 and x2 and freq from dataframe d associated with this combination.

The returned data structure will look like this:

 data.frame("X1" = c("sp1","sp1","sp2","sp2"), "X2" = c("sp3","sp4","sp3","sp4"), "freq" = c(4,94,46,74)) 

I tried:

 sub <- d[d$X1 == x1 & d$X2 == x2,] 

but get an error

 Error in Ops.factor(d$X1, x1) : level sets of factors are different 

Any ideas on how to solve this problem?

+4
source share
2 answers

Do not use the coefficients x1 and x2 . Just use vectors. Use %in% for a logic test.

 sp <- combn(c("sp1","sp2","sp3","sp4"),2) d <- data.frame(t(sp),"freq"=sample(0:100,6)) x1 <- c("sp1","sp2") x2 <- c("sp3","sp4") sub <- d[d$X1 %in% x1 & d$X2 %in% x2,] 
+3
source

You are almost there:

 d[d$X1 %in% x1 & d$X2 %in% x2,] 
+5
source

All Articles