| rowsum2 | R Documentation |
Count the sum of values, sum of zeros, or sum of NAs in the
each row of a data.frame or matrix. Optionally count only certain columns
and/or compare the row sums to a desired value.
rowsum2(data, x = c("na", "zero", "sum"), op, value, pattern, ...)
data |
|
x |
|
op |
Optional |
value |
Optional |
pattern |
Optional |
... |
Other arguments to be passed into |
Returns a logical vector if 'op' and 'value' have been specified.
Otherwise returns a numeric vector.
mat <- matrix(c(NA, 1:6, 0), nrow = 4, ncol = 2,
dimnames = list(NULL, c("sample1", "sample2")))
df <- data.frame(mat)
# works with data.frame or matrix
rowsum2(mat, "na")
rowsum2(df, "na")
# get just the sums (should use rowSums directly in this particular case),
# or sum of na/zero for each row
rowsum2(mat, "sum")
rowsum2(mat, "na")
rowsum2(mat, "zero")
# compare the row sums against a value of interest to
# see which rows 'pass'
rowsum2(mat, "sum", ">", 5)
# get row sums for only certain columns based on
# matching columns with grep
rowsum2(mat, "na", pattern = "sample1")
# combine row sum comparison and column matching
rowsum2(df, "na", "==", 0, "sample1")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.