expanddf: Data Frame Expand

Description Usage Arguments Details Value Examples

View source: R/expand_df.r

Description

Expand a data frame by splitting one column

Usage

1
expanddf(df, column, sep)

Arguments

df

data frame. The input to be expanded.

column

character. The column to be splitted.

sep

character. Separator for splitting a column.

Details

This function implements the data frame expansion if you need to split one column by the specific characters. The new data frame will generate the new columns named as 'level' appended by position-indexing numbers, such as 'level1', 'level2'. The maximum of appended numbers indicates the most splitting pieces for one cell. If the splitting results of one cell are fewer than the maximum, the row will be padded and corresponding cells will be filled with NAs.

Value

data frame. The resultant data frame with new columns.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
library(mojson)
# levels are identical.
df1 <- data.frame(a = c('ab@gmail.com', 'cd@gmail.com'),
                  b = c(TRUE, FALSE))
expanddf(df1, 'a', '@')

# change the separator and treat various levels.
df2 <- data.frame(a = c('1-2-0', '1-2-0-3', '1-2'),
                  b = c(TRUE, FALSE, TRUE))
expanddf(df2, 'a', '-')

mojson documentation built on Feb. 11, 2021, 5:07 p.m.