% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mean_n.R
\name{mean_n}
\alias{mean_n}
\title{Row Means with Optional Minimum Valid Values}
\usage{
mean_n(
  data = NULL,
  select = tidyselect::everything(),
  exclude = NULL,
  min_valid = NULL,
  digits = NULL,
  regex = FALSE,
  verbose = FALSE
)
}
\arguments{
\item{data}{A \code{data.frame} or \code{matrix}.}

\item{select}{Columns to include. If \code{regex = FALSE}, use tidyselect syntax (default: \code{tidyselect::everything()}).
If \code{regex = TRUE}, provide a regular expression pattern (character string).}

\item{exclude}{Columns to exclude (default: \code{NULL}).}

\item{min_valid}{Minimum number of valid (non-NA) values required per row.
If a proportion, it's applied to the number of selected columns.
Defaults to \code{NULL} (all values must be valid).}

\item{digits}{Optional number of decimal places to round the result.
Defaults to \code{NULL} (no rounding).}

\item{regex}{Logical. If \code{FALSE} (the default), uses tidyselect helpers.
If \code{TRUE}, the \code{select} argument is treated as a regular expression.}

\item{verbose}{Logical. If \code{FALSE} (the default), messages are suppressed.
If \code{TRUE}, prints a message about non-numeric columns excluded.}
}
\value{
A numeric vector of row-wise means.
}
\description{
\code{mean_n()} computes row means from a \code{data.frame} or \code{matrix}, handling missing values (\code{NA}s) automatically.
Row-wise means are calculated across selected numeric columns, with an optional condition on the minimum number (or proportion) of valid (non-missing) values required for a row to be included.
Non-numeric columns are excluded automatically and reported.
}
\examples{
library(dplyr)

# Create a simple numeric data frame
df <- tibble(
  var1 = c(10, NA, 30, 40, 50),
  var2 = c(5, NA, 15, NA, 25),
  var3 = c(NA, 30, 20, 50, 10)
)

# Compute row-wise mean (all values must be valid by default)
mean_n(df)

# Require at least 2 valid (non-NA) values per row
mean_n(df, min_valid = 2)

# Require at least 50\% valid (non-NA) values per row
mean_n(df, min_valid = 0.5)

# Round the result to 1 decimal
mean_n(df, digits = 1)

# Select specific columns
mean_n(df, select = c(var1, var2))

# Select specific columns using a pipe
df |>
  select(var1, var2) |>
  mean_n()

# Exclude a column
mean_n(df, exclude = "var3")

# Select columns ending with "1"
mean_n(df, select = ends_with("1"))

# Use with native pipe
df |> mean_n(select = starts_with("var"))

# Use inside dplyr::mutate()
df |> mutate(mean_score = mean_n(min_valid = 2))

# Select columns directly inside mutate()
df |> mutate(mean_score = mean_n(select = c(var1, var2), min_valid = 1))

# Select columns before mutate
df |>
  select(var1, var2) |>
  mutate(mean_score = mean_n(min_valid = 1))

# Show verbose processing info
df |> mutate(mean_score = mean_n(min_valid = 2, digits = 1, verbose = TRUE))

# Add character and grouping columns
df_mixed <- mutate(df,
  name = letters[1:5],
  group = c("A", "A", "B", "B", "A")
)
df_mixed

# Non-numeric columns are ignored
mean_n(df_mixed)

# Use within mutate() on mixed data
df_mixed |> mutate(mean_score = mean_n(select = starts_with("var")))

# Use everything() but exclude non-numeric columns manually
mean_n(df_mixed, select = everything(), exclude = "group")

# Select columns using regex
mean_n(df_mixed, select = "^var", regex = TRUE)
mean_n(df_mixed, select = "ar", regex = TRUE)

# Apply to a subset of rows (first 3)
df_mixed[1:3, ] |> mean_n(select = starts_with("var"))

# Store the result in a new column
df_mixed$mean_score <- mean_n(df_mixed, select = starts_with("var"))
df_mixed

# With a numeric matrix
mat <- matrix(c(1, 2, NA, 4, 5, NA, 7, 8, 9), nrow = 3, byrow = TRUE)
mat
mat |> mean_n(min_valid = 2)

}
