I am trying to make a new column in my dataset give a single output for each and every row, depending on the inputs from pre-existing columns.
In this output column, I d
You want to check if any of the variables in a row are 0, so you need to use any(x==0)
instead of x == 0
in the ifelse
statement:
apply(data, 1, function(x) {ifelse(any(x == 0), NA, length(unique(x)))})
# [1] 1 NA 2
Basically ifelse
returns a vector of length n if its first argument is of length n. You want one value per row, but are passing more than one with x==0
(the number of values you're passing is equal to the number of columns in your data frame).
Data:
(data <- data.frame(a=c(1, 2, 3), b=c(1, 0, 1)))
# a b
# 1 1 1
# 2 2 0
# 3 3 1