Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:39:42+00:00 2026-06-18T11:39:42+00:00

How to count frequencies occurring in two columns ? Sample datas : > sample

  • 0

How to count frequencies occurring in two columns ?
Sample datas :

> sample <- dput(df)
structure(list(Nom_xp = c("A1FAA", "A1FAJ", "A1FBB", "A1FJA", 
"A1FJR", "A1FRJ"), GB05.x = c(100L, 98L, NA, 100L, 102L, 98L), 
    GB05.1.x = c(100L, 106L, NA, 100L, 102L, 98L), GB18.x = c(175L, 
    173L, 177L, 177L, 173L, 177L), GB18.1.x = c(177L, 175L, 177L, 
    177L, 177L, 177L)), .Names = c("Nom_xp", "GB05.x", "GB05.1.x", 
"GB18.x", "GB18.1.x"), row.names = c(NA, 6L), class = "data.frame")

Counting frequencies :

apply(sample[,2:5],2,table)

Now, how to combine the count by prefix of columns, or by every two columns ? The expected output, for the four first columns would be a list:

$GB05
98 100 102 106
3  4   2   1
$GB18
173 175 177
2   2   8

One way to get the count for the first two columns :

  table(c(apply(sample[,2:3],2,rbind)))
98 100 102 106 
  3   4   2   1

But how to apply this to a whole data.frame ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:39:43+00:00Added an answer on June 18, 2026 at 11:39 am

    If you want to apply table to your whole data frame, you can use :

    table(unlist(sample[,-1]))
    

    Which gives :

     98 100 102 106 173 175 177 
      3   4   2   1   2   2   8 
    

    If you want to group by column name prefix, for example the 4th first characters, you can do something like this :

    cols <- names(sample)[-1]
    groups <- unique(substr(cols,0,4))
    sapply(groups, function(name) table(unlist(sample[,grepl(paste0("^",name),names(sample))])))
    

    Which gives :

    $GB05
    
     98 100 102 106 
      3   4   2   1 
    
    $GB18
    
    173 175 177 
      2   2   8 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this F# code to count word frequencies in a list and
I have a list of integers and I need to count how many of
I'm trying to count up ip addresses found in a log file on two
I want to scan through a huge corpus of text and count word frequencies
SELECT count(*) from table gives me 3296869 rows. The table only contains 4 columns,
Count Number of days by comapaing two date, When you want to compare two
I want to count how frequent values appear in certain columns and create a
count the occurrences of each symbol and the location they appear in the text,
Count the longest sequence of heads and tails in 200 coin flips. I did
Does COUNT(*) have any significant impact for MySQL performance if query already has GROUP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.