r - Exclude a Specific Value from a Unique Value Counter -
i trying count how many different responses person gives during trial of experiment, there catch.
there supposed 6 possible responses (1,2,3,4,5,6) 0 recorded response (it's glitch / flaw in design).
i need count number of different responses give, counting unique values within range 1-6. helps calculate accuracy.
is there way exclude value 0 contributing unique value counter? other work-arounds?
currently trying method below, includes 0, na, , think other entry in cell in unique value counter column (i have named "span6"), makes me sad.
# span6 calculator: asiximagetrials <- data.frame(esopt_831$t8.resp, esopt_831$t9.resp, esopt_831$t10.resp, esopt_831$t11.resp, esopt_831$t12.resp, esopt_831$t13.resp) asiximagetrials$span6 = apply(asiximagetrials, 1, function(x) length(unique(x)))
use na.omit
inside unique , sum logic vector below
df$res = apply(df, 1, function(x) sum(unique(na.omit(x)) > 0)) df
output:
x1 x2 x3 x4 x5 res 1 2 1 1 2 1 2 2 3 0 1 1 2 3 3 3 na 1 1 3 2 4 3 3 3 4 na 2 5 1 1 0 na 3 2 6 3 na na 1 1 2 7 2 0 2 3 0 2 8 0 2 2 2 1 2 9 3 2 3 0 na 2 10 0 2 3 2 2 2 11 2 2 1 2 1 2 12 0 2 2 2 na 1 13 0 1 4 3 2 4 14 2 2 1 1 na 2 15 3 na 2 2 na 2 16 2 2 na 3 na 2 17 2 3 2 2 2 2 18 2 na 3 2 2 2 19 na 4 5 1 3 4 20 3 1 2 1 na 3
data:
set.seed(752) mat <- matrix(rbinom(100, 10, .2), nrow = 20) mat[sample(1:100, 15)] = na data.frame(mat) -> df df$res = apply(df, 1, function(x) sum(unique(na.omit(x)) > 0))
Comments
Post a Comment