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
Let n = length(x)
. ifelse
will return rep(NA, n)
if TRUE
otherwise rep(length(unique(x)), n)
. Therefore apply
will output a matrix. data$output <- apply(...
tries assign a matrix (your result) into a column in your data.frame
, data$output
. This is the cause of your error.
Your code will run if you just assign your output to a variable instead
out <- apply(data, 1, function(x) {ifelse(x == 0, NA, length(unique(x)))})
If you are not expecting a class(matrix)
as your output, but rather a vector, then there is something wrong with the logic of your function.