Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Collapse factor levels based on matching characters

$
0
0

I have many factor vectors in a tibble. It's a survey, so the levels are letter codes.

They survey tool incorporates order of letter chosen at the time of survey (from a clicker), which may or may not be useful depending on the question.

I am seeking a tidy function or a process by which to collapse the factor levels with matching letters. I.e., "B,A" = "A,B" and this collapses to just "A,B".

Or "B,C,A" = "C,A,B" = "A,B,C" or any combination of the letters A,B,C. I can have up to 5 letters max in a factor level, so it can get complicated quickly.

Should I covert it to a character string and then use stringi or grepl to break it into multiple columns? I have numerous columns, so I am looking for a slick solution. Any ideas?

Here is an example of a simple string in my data:

string<-c("E","C","A","A,B","A,B,C","B,A","C,A,B") %>% as.factor()

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>