This apply
function returns a vector of the number of unique values in each row:
apply(example, 1, function(x)length(unique(x)))
You can append it to your data.frame using on of the following two ways (and if you want to name that column as count
):
example <- cbind(example, count = apply(example, 1, function(x)length(unique(x))))
or
example$count <- apply(example, 1, function(x)length(unique(x)))