Elizabeth
Elizabeth

Reputation: 6561

Filtering a data frame by factors in R

I have the following dataframe:

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 dataframe returned containing all possible combinations of x1 and x2 and the freq from dataframe d associated with this combination.

The returned dataframe would look like this:

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

I have tried:

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

but get the error

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

Any ideas on how to solve this problem?

Upvotes: 5

Views: 10595

Answers (2)

John
John

Reputation: 43199

Do not make x1 and x2 factors. Just use vectors. Use %in% for the logical 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,]

Upvotes: 6

Julius Vainora
Julius Vainora

Reputation: 48201

You are almost there:

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

Upvotes: 5

Related Questions