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 7976047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:48:51+00:00 2026-06-04T08:48:51+00:00

I have a three dataframes, and I want to add some columns to the

  • 0

I have a three dataframes, and I want to add some columns to the first dataframe which counts the number of times the first two columns in the first dataframe appear in the other dataframes e.g.

dataframe – x
a b
1 1
1 2
2 1
2 2

dataframe – y
a b
1 1
1 1
1 2
2 2
2 2

dataframe – z
a b
1 2
2 1
2 1
2 2

So the first dataframe would become
a b y z
1 1 2 0
1 2 1 1
2 1 0 2
2 2 2 1

I have ways to do this, e.g. I am currently doing

x$y<- sapply(1:nrow(x), function(i){
    sum(y$a == x$a[i] & y$b == x$b[i])
  }

x$z<- sapply(1:nrow(x), function(i){
    sum(z$a == x$a[i] & z$b == x$b[i])
  }

But my dataframe is very large and my way takes a while to complete so I was wondering of the quickest way to do this.

Please ask if anything is unclear.

Thanks in advance

  • 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-04T08:48:52+00:00Added an answer on June 4, 2026 at 8:48 am

    To avoid the double loop, I would use the function match, which is optimized for finding elements in another list. To count how many elements, I propose to tabulate the variables first, and then to match against the table.

    My guess is that it would significantly reduce the time complexity, because the method you propose is quadratic (one loop goes over x rows and for each an inner loop goes over y rows) whereas the functions match and table are based on sorts (I think) which are rather n*log(n).

    We first turn the data frames into vectors with paste, taken from the answer of Josh:

    # Recreate your data
    x <- data.frame(a=c(1,1,2,2), b=c(1,2,1,2))
    y <- data.frame(a=c(1,1,1,2,2), b=c(1,1,2,2,2))
    z <- data.frame(a=c(1,2,2,2), b=c(2,1,1,2))
    
    # Use paste to combine the two columns
    X <- do.call(paste, c(x, sep="_"))
    Y <- do.call(paste, c(y, sep="_"))
    Z <- do.call(paste, c(z, sep="_"))
    

    Then we tabulate and match against the tabluation.

    x$y <- table(Y)[match(X, names(table(Y)))]
    x$y[is.na(x$y)] <- 0
    
    x$z <- table(Z)[match(X, names(table(Z)))]
    x$z[is.na(x$z)] <- 0
    
    x  
    a b y z
    1 1 1 2 0
    2 1 2 1 1
    3 2 1 0 2
    4 2 2 2 1
    

    You could put table(Y) in an intermediate variable if you want to avoid tabulating two times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe with columns labeled A,B & C. I want to add
I have a dataframe with three columns: Id , Date and Value and want
Say I have a dataframe df with two or more columns, is there an
I have a data.frame with columns a and b. I want to add columns
I have a dataframe df. I want to create two subsets (a & b)
I have two data.frames in R, one of which has two columns and of
I have two data.frames each with three columns: chrom, start & stop, let's call
I have a small dataframe from which I am plotting 3 columns in order
I have a three part question based on a dataframe (df is example rows)
Have three divs in a container that I want to float over a large

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.