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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:36:55+00:00 2026-06-05T23:36:55+00:00

I have following dataset: name1 <- c(P1, P2, IndA, IndB, IndC, IndD, IndE, IndF,

  • 0

I have following dataset:

name1 <- c("P1", "P2", "IndA", "IndB", "IndC", "IndD", "IndE", "IndF", "IndG")
name2 <- c("P1", "P2", "IndH", "IndI", "IndJ", "IndK")
name3 <- c("P1", "P2", "IndL", "IndM", "IndN")
name <- c(name1, name2, name3)

A <- c(1, 3, 1, 2, 2, 5, 5, 1, 4, 1, 3, 3, 1, 4, 3, 1, 1, 3,2,1 )
B <- c(2, 4, 3, 4, 2, 2, 6, 2, 2, 1, 4, 3, 1, 1, 5, 2,2, 1, 2, 1 )
family  = c(rep(1, length (name1)), rep(2, length (name2)), rep(3, length (name3)))
mydf <- data.frame (family, name, A, B)

The following is process I want to apply each level of family variable:

dum.match<-rbind(expand.grid(c(mydf[1,3:4]),c(mydf[2,3:4])),
        expand.grid(c(mydf[2,3:4]),  c(mydf  [1,3:4])))
    newmydf<-cbind(mydf, correct = paste(mydf$A,mydf$B)%in%paste(dum.match$Var1,
        dum.match$Var2))

So I generated a function:

err.chk <- function (x) {
    dum.match<-rbind(expand.grid(c(x[1,3:4]),c(x[2,3:4])),
     expand.grid(c(x[2,3:4]),c(x[1,3:4])))
    newmydf<-cbind(x, correct = paste(x$A,mydf$B)%in%paste(dum.match$Var1,
     dum.match$Var2))
    return (newmydf)
    }

Now I want to create seperate 3 dataset for each level of family and apply the above function and combine the results into above dataframe with additional column correct. How can I do it ? I tried following (and results are awaful !)

 require(plyr) 
 aaply(mydf, 1, err.chk)

Edit:

Expected output:

family name A B  correct 
1       1   P1 1 2  FALSE
2       1   P2 3 4  FALSE
3       1 IndA 1 3  TRUE
4       1 IndB 2 4  TRUE
5       1 IndC 2 2  FALSE
6       1 IndD 5 2  FALSE
7       1 IndE 5 6  FALSE
8       1 IndF 1 2  FALSE
9       1 IndG 4 2  TRUE

10      2   P1 1 1  FALSE
11      2   P2 3 4  FALSE
12      2 IndH 3 3  FALSE
13      2 IndI 1 1  FALSE
14      2 IndJ 4 1  TRUE
15      2 IndK 3 5  FALSE 


16      3   P1 1 2  TRUE
17      3   P2 1 2  TRUE
18      3 IndL 3 1  FALSE
19      3 IndM 2 2  TRUE
20      3 IndN 1 1  TRUE

Just for family = 3 (similaly for other datasets)

    # just data for family 3
    name <- c("P1", "P2", "IndL", "IndM", "IndN")
    A <- c(1, 1, 3,2,1 )
    B <- c(2,2, 1, 2, 1)
    mydf <- data.frame (name, A, B)
    err.chk(fam3)

   name A B correct
16   P1 1 2    TRUE
17   P2 1 2    TRUE
18 IndL 3 1   FALSE
19 IndM 2 2    TRUE
20 IndN 1 1    TRUE
  • 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-05T23:36:56+00:00Added an answer on June 5, 2026 at 11:36 pm

    Its hard to follow exactly what you’re doing, but with plyr you want to use a **ply function that accepts the data type you’re giving it and returns the data type your function returns. In this case, ddply looks like the right choice.

    If you fix your function in the 3rd line you have a mydf$B which should be x$B:

    err.chk <- function (x) {
      dum.match <- rbind(expand.grid(c(x[1, 2:3]), c(x[2, 2:3])),
                         expand.grid(c(x[2, 2:3]), c(x[1, 2:3])))
      newmydf <- cbind(x, correct = paste(x$A, x$B) %in% paste(dum.match$Var1, dum.match$Var2))
    
      return (newmydf)
    }
    

    Calling it using ddply gives a reasonable looking result.

    > ddply(mydf, .(family), err.chk)
       family name A B correct
    1       1   P1 1 2   FALSE
    2       1   P2 3 4   FALSE
    3       1 IndA 1 3    TRUE
    4       1 IndB 2 4    TRUE
    5       1 IndC 2 2   FALSE
    6       1 IndD 5 2   FALSE
    7       1 IndE 5 6   FALSE
    8       1 IndF 1 2   FALSE
    9       1 IndG 4 2    TRUE
    10      2   P1 1 1   FALSE
    11      2   P2 3 4   FALSE
    12      2 IndH 3 3   FALSE
    13      2 IndI 1 1   FALSE
    14      2 IndJ 4 1    TRUE
    15      2 IndK 3 5   FALSE
    16      3   P1 1 2    TRUE
    17      3   P2 1 2    TRUE
    18      3 IndL 3 1   FALSE
    19      3 IndM 2 2    TRUE
    20      3 IndN 1 1    TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following dataset Id Title Group 1 title1 A 2 title2 A 3
I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
I have big dataset (but the following is small one for example). I can
I have a dataset with ~20M rows and I'm observing the following behavior. The
If I have the following xml document: <xml> <data> <dataset name=X></dataset> </data> </xml> How
I have a dataset named: FileUploadDataSet.xsd and i have the following table adapter: The
I have the following XML from a .NET web service: <?xml version=1.0 encoding=utf-8?> <DataSet>
i have the following dataset NAME | GP | ORD_GP | EXP | TOTAL
I have the following dataset. structure(list(Rf = c(60.105, 62.205, 64.305, 64.305, 66.405, 66.405), Es
I have the following code to convert my JSON dataset to html table. I

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.