Unique values of several columns (basic question)

You need to “apply” or “map” the unique function to each column,

lapply(select(data, starts_with("column")), unique)

You can use sapply or purrr::map instead of lapply for slight variations in behavior. This FAQ gives an excellent overview of the base options and when they are applicable. See the purrr package documentation for information about that.

As for “why this one is wrong” – unique() applied to a data frame will give you the unique rows of the data frame. This is different than the unique values of each column. Generally, functions may need to work differently on data frames than on vectors (columns), so foo(dataframe) cannot be assumed to be foo() applied to each column. So we use lapply or similar functions to specifically apply foo() to each column.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top