Learn R Programming

gdata (version 3.0.1)

nobs: Compute the Number of Non-Missing Observations

Description

Compute the number of non-missing observations. Provides a new default method to handle numeric and logical vectors, and a method for data frames.

Usage

nobs(object, ...)
# S3 method for default
nobs(object, ...)
# S3 method for data.frame
nobs(object, ...)
# S3 method for lm
nobs(object, ...)
n_obs(object, ...)

Value

Either single numeric value (for vectors) or a vector of numeric values (for data frames) giving the number of non-missing values.

Arguments

object

Numeric or logical vector, data frame, or a model object.

...

Further arguments to be passed to methods.

Author

Gregory R. Warnes greg@warnes.net

See Also

nobs in package 'stats' for the base R implementation, is.na, length

Examples

Run this code
x <- c(1, 2, 3, 5, NA, 6, 7, 1, NA)
length(x)
nobs(x)

df <- data.frame(x=rnorm(100), y=rnorm(100))
df[1,1] <- NA
df[1,2] <- NA
df[2,1] <- NA
nobs(df)

fit <- lm(y~x, data=df)
nobs(fit)
n_obs(fit)

# Comparison
# gdata
nobs(x)
nobs(df)
# stats
length(na.omit(x))
sapply(df, function(x) length(na.omit(x)))

Run the code above in your browser using DataLab