Search code examples
rlistdataframeunique

Unique values of a dataframe column to list in R


I have an R table (site_df). Need to convert unique values from a column (site_name) to a list. Any suggestions?

Example:

site_list = site_df %>% unique(site_name) %>% convert this to list

Solution

  • In base R, you can get unique values from site_name and make it a list.

    as.list(unique(site_df$site_name))
    

    For example, with default mtcars this will result into :

    as.list(unique(mtcars$cyl))
    #[[1]]
    #[1] 6
    
    #[[2]]
    #[1] 4
    
    #[[3]]
    #[1] 8