Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Calculate the highest text frequency by column in R

I have a data like this:

enter image description here

how to change become:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I need some helps to fit it…

>Solution :

Here is a dplyr option

library(dplyr)
df %>%
    group_by(X1) %>%
    summarise(
        X3 = names(sort(table(X2), decreasing = TRUE))[1],
        X2 = toString(X2),
        .groups = "drop") %>%
    select(X1, X2, X3)
## A tibble: 3 × 3
#  X1    X2                        X3    
#  <chr> <chr>                     <chr> 
#1 A     high, high                high  
#2 B     middle, middle, high      middle
#3 C     low, middle, middle, high middle

Note: Since you’re overwriting X2, we need to carry out the calculation for X3 first and then summarise X2. The last select call reorders columns to match your expected output.

Note also that this might need some careful checking of edge cases on your side to deal with ties in word frequencies.


Sample data

df <- data.frame(
    X1 = c("A", "A", "B", "B", "B", "C", "C", "C", "C"),
    X2 = c("high", "high", "middle", "middle", "high", "low", "middle", "middle", "high"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading