vec_segment: Vector statistics for error bar plotting

View source: R/mt_extra_1.R

vec_segmentR Documentation

Vector statistics for error bar plotting

Description

Calculate vector's standard derivation, standard error of mean and confidence interval.

Usage

vec_segment(x, bar = c("SD", "SE", "CI"))

Arguments

x

an vector.

bar

a character string, supporting "SD", "SE" and "CI".

Value

an vector including lower, center and upper values.

See Also

Other vector stats functions: vec_stats()

Examples

library(plyr)
library(reshape2)
library(dplyr)
library(tidyr)
library(purrr)

vec_segment(iris[,1])

## Use 'plyr' and 'reshape2' for group 
mat <- melt(iris)
ddply(mat, .(Species,variable), function(x,bar) {
  vec_segment(x$value, bar = bar)
}, bar = "SD")

## Use 'tidyverse' for group 
iris %>%
  pivot_longer(cols = !Species, names_to = "variable") %>% 
  group_by(Species, variable) %>%
  nest() %>%
  mutate(map_dfr(.x = data, .f = ~ vec_segment(.x$value))) %>%
  select(!data)

iris %>%
  pivot_longer(cols = !Species, names_to = "variable") %>% 
  group_nest(Species, variable) %>%
  mutate(map_dfr(data, ~ vec_segment(.x$value))) %>%
  select(!data)

wanchanglin/mtExtra documentation built on Aug. 2, 2024, 5:47 p.m.