Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate words from cells in R

Tags:

list

r

unique

I have a 2-column data frame, where the first column is a number, and the second column contains a list of research categories. A reduced version of my data:

aa <- data.frame(a=c(1:4),b=c("Fisheries, Fisheries, Geography, Marine Biology", 
"Fisheries", "Marine Biology, Marine Biology, Fisheries, Zoology", "Geography"))

I want to convert column b into a unique list of elements, i.e., remove the duplicates, so that the end result is

    a        b
    1        Fisheries, Geography, Marine Biology
    2        Fisheries
    3        Marine Biology, Fisheries, Zoology
    4        Geography

I am able to do this for individual elements of the list, for example, using unique(unlist(strsplit(aa[1]))) BUT only on individual elements, not the entire column (otherwise it returns a single unique list for the entire column). I can’t figure out how to do this for the entire list, one element at a time. Maybe with lapply and write my own function for *unique(unlist(strsplit()))?

Many thanks!

like image 440
Tessa Francis Avatar asked Sep 11 '25 19:09

Tessa Francis


1 Answers

This should work for you.

aa <- data.frame(a=c(1:4),b=c("Fisheries, Fisheries, Geography, Marine Biology", 
                              "Fisheries", "Marine Biology, Marine Biology, Fisheries, Zoology", "Geography"))

aa$b <- sapply(aa$b, function(x) paste(unique(unlist(str_split(x,", "))), collapse = ", "))
like image 81
Matt Jewett Avatar answered Sep 13 '25 10:09

Matt Jewett