A simple class for weighted data.

weighted(x, w)

Arguments

x

An atomic vector or data.frame.

w

An numeric vector of weights.

Value

An object identical to x but with the additional class weighted and a weights attribute.

Examples

x <- c(3.7, 3.3, 3.5, 2.8)
y <- c(1, 2, 1, 2)
w <- c(5, 3, 4, 1)

z <- weighted(x=x, w=w)
weights(z)
#> [1] 5 3 4 1
weights(z[2:3])  # Weights are preserved
#> [1] 3 4

d <- weighted(
  data.frame(
    x=x,
    y=y
  ),
  w
)

weights(d)
#> [1] 5 3 4 1
weights(d[[1]])
#> [1] 5 3 4 1
weights(d$x)
#> [1] 5 3 4 1
weights(subset(d, y==1))
#> [1] 5 4
lapply(split(d, d$y), weights)
#> $`1`
#> [1] 5 4
#> 
#> $`2`
#> [1] 3 1
#>