0

I am trying to sub set out rows which contain certain combinations of elements, where the combinations come from another data frame.

The first data frame shows all the animals and their weights owned by a group of farmers, the second data frame says that actually a farmer has sold a all of a particular type of animal and so they should all be removed from the set. In my example James sold all his Deer and Alice sold all her Giga chickens, but Schubert did not sell his Deer so nothing needs to be done to him. If there had only been one variable I could have used %in% but I couldn't make that work with two variables. The way I have solved it is with messy nested if and for loops, but I imagine there is a much more efficient method.

owner <-c("Fred", "Mary", "James", "Ingrid", "Schubert", "Alice") #owner names
animal <-c("Cow", "Giant sheep", "Deer", "Giga chicken") #Animal types
data <- data.frame(owner= sample(owner, 1000, replace= TRUE), animal=sample(animal, 1000, replace= TRUE), weight=rnorm(1000,mean=250, sd=50)) #data set

sub.set <- data.frame(cbind(c("James","Alice", "Schubert"),c("Deer","Giga chicken", "Deer"), c(0,0,1)))

for (i in unique(sub.set[,1])) {
    for (y in unique(sub.set[,2])) {

        #first if statement prevents error that occur if the subset data doesn't have one of the loop combinations
        if(length(sub.set[sub.set$X1 ==i & sub.set$X2 ==y,3])>0){
            if (sub.set[sub.set$X1 ==i & sub.set$X2 ==y,3]==0)
            { data <- data[!(data$owner==i & data$animal==y),]}
        }
    }
}
xtabs(weight ~., data)

As can be seen in the cross tabulation the correct elements have been subsetted out but in a horrible way, help in doing this operation in a simpler way would be very much appreciated!

1 Answer 1

1

This produces the identical result to your code, and does not use loops.

set.seed(1)   # need reproducible sample data
owner <-c("Fred", "Mary", "James", "Ingrid", "Schubert", "Alice") #owner names
animal <-c("Cow", "Giant sheep", "Deer", "Giga chicken") #Animal types
data <- data.frame(owner= sample(owner, 1000, replace= TRUE), 
                   animal=sample(animal, 1000, replace= TRUE), 
                   weight=rnorm(1000,mean=250, sd=50)) #data set
# note the column names in sub.set
sub.set <- data.frame(owner=c("James","Alice", "Schubert"),
                      animal=c("Deer","Giga chicken", "Deer"), 
                      count=c(0,0,1))
# this is the code to exclude rows where there are no animals left
data <- merge(data,sub.set,by=c("owner","animal"),all.x=T)
data <- with(data,data[count!=0 | is.na(count),])
data <- data[,-4]
xtab.2 <- xtabs(weight~.,data)

This code merges data and sub.set on the columns owner and animal to create a new column count. Then it includes only rows where count!=0 or !is.na(count). Then it removes the count column and calculates the cross tab as before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.