row_means | R Documentation |
This function is similar to the SPSS MEAN.n
function and computes
row means from a data frame or matrix if at least min_valid
values of a row are
valid (and not NA
).
row_means(
data,
select = NULL,
exclude = NULL,
min_valid = NULL,
digits = NULL,
ignore_case = FALSE,
regex = FALSE,
remove_na = FALSE,
verbose = TRUE
)
data |
A data frame with at least two columns, where row means are applied. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
exclude |
See |
min_valid |
Optional, a numeric value of length 1. May either be
If a row's sum of valid values is less than |
digits |
Numeric value indicating the number of decimal places to be
used for rounding mean values. Negative values are allowed (see 'Details').
By default, |
ignore_case |
Logical, if |
regex |
Logical, if |
remove_na |
Logical, if |
verbose |
Toggle warnings. |
Rounding to a negative number of digits
means rounding to a power of
ten, for example row_means(df, 3, digits = -2)
rounds to the nearest hundred.
For min_valid
, if not NULL
, min_valid
must be a numeric value from 0
to ncol(data)
. If a row in the data frame has at least min_valid
non-missing values, the row mean is returned. If min_valid
is a non-integer
value from 0 to 1, min_valid
is considered to indicate the proportion of
required non-missing values per row. E.g., if min_valid = 0.75
, a row must
have at least ncol(data) * min_valid
non-missing values for the row mean
to be calculated. See 'Examples'.
A vector with row means for those rows with at least n
valid values.
dat <- data.frame(
c1 = c(1, 2, NA, 4),
c2 = c(NA, 2, NA, 5),
c3 = c(NA, 4, NA, NA),
c4 = c(2, 3, 7, 8)
)
# default, all means are shown, if no NA values are present
row_means(dat)
# remove all NA before computing row means
row_means(dat, remove_na = TRUE)
# needs at least 4 non-missing values per row
row_means(dat, min_valid = 4) # 1 valid return value
# needs at least 3 non-missing values per row
row_means(dat, min_valid = 3) # 2 valid return values
# needs at least 2 non-missing values per row
row_means(dat, min_valid = 2)
# needs at least 1 non-missing value per row, for two selected variables
row_means(dat, select = c("c1", "c3"), min_valid = 1)
# needs at least 50% of non-missing values per row
row_means(dat, min_valid = 0.5) # 3 valid return values
# needs at least 75% of non-missing values per row
row_means(dat, min_valid = 0.75) # 2 valid return values
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.