Learn R Programming

groupdata2 (version 1.0.0)

find_starts: Find start positions of groups in data.

Description

Find values or indices of values that are not the same as the previous value. E.g. use with the l_starts method.

Usage

find_starts(data, col = NULL, return_index = FALSE)

Arguments

data

Dataframe or Vector

col

Name of column to find starts in. Used when data is dataframe. (Character)

return_index

Return indices of starts. (Logical)

Value

Vector with either start values or indices of start values.

See Also

Other l_starts tools: find_missing_starts, group_factor, group

Examples

Run this code
# NOT RUN {
# Attach packages
library(groupdata2)

# Create a dataframe
df <- data.frame('a' = c('a','a','b',
                         'b','c','c'))

# Get start values for new groups in column 'a'
find_starts(df, col = 'a')

# Get indices of start values for new groups
# in column 'a'
find_starts(df, col = 'a',
            return_index = TRUE)

## Use found starts with l_starts method
# Notice: This is equivalent to n = 'auto'
# with l_starts method

# Get start values for new groups in column 'a'
starts <- find_starts(df, col = 'a')

# Use starts in group() with 'l_starts' method
group(df, n = starts, method = 'l_starts',
      starts_col = 'a')

# Similar but with indices instead of values

# Get indices of start values for new groups
# in column 'a'
starts_ind <- find_starts(df, col = 'a',
                          return_index = TRUE)

# Use starts in group() with 'l_starts' method
group(df, n = starts_ind, method = 'l_starts',
      starts_col = 'index')


# }

Run the code above in your browser using DataLab