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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:09+00:00 2026-05-27T23:00:09+00:00

note: this is a direct follow up to this previous question I have very

  • 0

note: this is a direct follow up to this previous question


I have very long dataframe consisting of two columns that I am using as arguments for a function that will find the value of a third column using mapply as so:

df$3rd <- mapply(myfunction, A=df$1st, B=df$2nd)

where myfunction has arguments A and B. While this works great for small datasets, it stalls for large datasets so I was thinking a good way to approach the problem would be to apply this function using ddply. I don’t know if ddply is the best approach for this problem but I am also having some trouble with syntax. So suggestions for either would be appreciated.

This is what I am trying:

> df$3rd <- ddply(df, .(1st), function(x) x$3rd <-
> mapply(myfunction, A=x$1st, B=df$second))

and this is the error I am getting:

Error in `$<-.data.frame`(`*tmp*`, "n", value = c(1L, 1L, 1L, 1L, 1L,  : 
  replacement has 112 rows, data has 16

EDIT:



In light of the answer and comments I I am posting a small reproducible example below – it is one of the answers from the previous question. However as the commenters below note, ddply is probably not the way to go. I am trying Ramnath’s solution right now.

library(reshape2)
foo <- data.frame(x = c('a', 'a', 'a', 'b', 'b', 'b'), 
                  y = c('ab', 'ac', 'ad', 'ae', 'fx', 'fy'))
bar <- data.frame(x = c('c', 'c', 'c', 'd', 'd', 'd'), 
                  y = c('ab', 'xy', 'xz', 'xy', 'fx', 'xz'))

nShared <- function(A, B) {
    length(intersect(with(foo, y[x==A]), with(bar, y[x==B])))
}

# Enumerate all combinations of groups in foo and bar
(combos <- expand.grid(foo.x=unique(foo$x), bar.x=unique(bar$x)))

# Find number of elements in common among all pairs of groups
combos$n <- mapply(nShared, A=combos$foo.x, B=combos$bar.x)

# Reshape results into matrix form
dcast(combos, foo.x ~ bar.x)
#   foo.x c d
# 1     a 1 0
# 2     b 0 1
  • 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-05-27T23:00:10+00:00Added an answer on May 27, 2026 at 11:00 pm

    ddply isn’t what you’re after here, ddply(df,.(1st), FUNCTION) is more like:

    for each val in unique(df$1st)
        outdf[nrow(outdf)+1,] = FUNCTION( df[df$1st==val] )
    

    That is, it makes outdf consisting of FUNCTION applied to subsets of df determined by column 1st.

    In any case, I think your error might be because you have df instead of x in function(x) x$3rd<-mapply(myfunction,A=x$1st, B=df$second) (the B argument)? Although it is hard to tell without a working example.

    What exactly does myfunction do? I think your best bet is to vectorise myfunction so that you can just do df$third <- myfunction( A=df$first, B=df$second ).

    For example, if myfunction <- function(A,B) { A+B }, instead of doing mapply(myfunction,df$first,df$second) you could equivalently do myfunction(df$first,df$second) and not even need mapply at all.

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

Sidebar

Related Questions

Note: This question has broadened in scope from previous revisions. I have tried to
(NOTE: This question is not about escaping queries, it's about escaping results) I'm using
Note: I know exactly what a property is. This question is about performance. Using
This question is an updated version of a previous question I have asked on
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Note This is not a REBOL-specific question. You can answer it in any language.
Note: This is the opposite direction to most similar questions! I have an iPhone
Note this question was originally posted in 2009, before C++11 was ratified and before
NOTE: This question has been asked on the kohana forums at: http://forum.kohanaframework.org/comments.php?DiscussionID=6451 Hey everyone!
I have many coordinates that I am plotting using a MKMapOverlay and CoreLocation and

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.