colsplit: Split a vector into multiple columns

Description Usage Arguments Examples

View source: R/helper-colsplit.r

Description

Useful for splitting variable names that a combination of multiple variables. Uses type.convert to convert each column to correct type, but will not convert character to factor.

Usage

1

Arguments

string

character vector or factor to split up

pattern

regular expression to split on

names

names for output columns

Examples

1
2
3
4
x <- c("a_1", "a_2", "b_2", "c_3")
vars <- colsplit(x, "_", c("trt", "time"))
vars
str(vars)

Example output

  trt time
1   a    1
2   a    2
3   b    2
4   c    3
'data.frame':	4 obs. of  2 variables:
 $ trt : chr  "a" "a" "b" "c"
 $ time: int  1 2 2 3

reshape2 documentation built on April 14, 2020, 5:26 p.m.