textshape (version 1.6.0)

unnest_text: Un-nest Nested Text Columns

Description

Un-nest nested text columns in a data.frame. Attempts to locate the nested text column without specifying.

Usage

unnest_text(dataframe, column, integer.rownames = TRUE, ...)

Arguments

dataframe

A dataframe object.

column

Column name to search for markers/terms.

integer.rownames

logical. If TRUE then the rownames are numbered 1 through number of rows, otherwise the original row number is retained followed by a period and the element number from the list.

ignored.

Value

Returns an un-nested data.frame.

Examples

Run this code
# NOT RUN {
dat <- DATA

## Add a nested/list text column
dat$split <- lapply(dat$state, function(x) {
    unlist(strsplit(x, '(?<=[?!.])\\s+', perl = TRUE))
})

unnest_text(dat)
unnest_text(dat, integer.rownames = FALSE)

## Add a second nested integer column
dat$d <- lapply(dat$split, nchar)
# }
# NOT RUN {
unnest_text(dat) # causes error, must supply column explicitly
# }
# NOT RUN {
unnest_text(dat, 'split')

## As a data.table
library(data.table)
dt_dat <- data.table::as.data.table(data.table::copy(dat))
unnest_text(dt_dat, 'split')
# }
# NOT RUN {
unnest_text(dt_dat, 'd')
# }
# NOT RUN {
# }
# NOT RUN {
## As a tibble
library(tibble)
t_dat <- tibble:::as_tibble(dat)
unnest_text(t_dat, 'split')
# }

Run the code above in your browser using DataLab