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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:07:20+00:00 2026-05-29T10:07:20+00:00

I know my problem is simple but not for me. Here is small dataset.

  • 0

I know my problem is simple but not for me. Here is small dataset.

mark1 <- c("AB", "BB", "AB", "BB", "BB", "AB", "--", "BB")
mark2 <- c("AB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark3 <- c("BB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark4 <- c("AA", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark5 <- c("AB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark6 <- c("--", "BB", "AA", "BB", "BB", "AA", "--", "BB")
mark7 <- c("AB", "--", "AA", "BB", "BB", "AA", "--", "BB")
mark8 <- c("BB", "AA", "AA", "BB", "BB", "AA", "--", "BB")
mymark <- data.frame (mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8)
tmymark <- data.frame (t(mymark))
names (tmymark) <- c("P1", "P2","I1", "I2", "I3", "I4", "KL", "MN")

Thus dataset becomes:

      P1 P2 I1 I2 I3 I4 KL MN
mark1 AB BB AB BB BB AB -- BB
mark2 AB AB AA BB BB AA -- BB
mark3 BB AB AA BB BB AA -- BB
mark4 AA AB AA BB BB AA -- BB
mark5 AB AB AA BB BB AA -- BB
mark6 -- BB AA BB BB AA -- BB
mark7 AB -- AA BB BB AA -- BB
mark8 BB AA AA BB BB AA -- BB

I want to classify mark1:8 based on the P1 and P2 comparision and provide a code, which will make a new variable:

loctype <- NULL

if (tmymark$P1 == "AB" &  tmymark$P2 == "AB"){
       loctype = "<hkxhk>"
       } else {
if (tmymark$P1== "AB" & tmymark$P2 == "BB") {
      loctype = "<lmxll>"
      } else {
      if (tmymark$P1 == "AA" & tmymark$P2 == "AB") {
       loctype = "<nnxnp>"
       } else {
        if (tmymark$P1 == "AA" & tmymark$P2 == "BB") {
        loctype = "MN"
        } else {
        if (tmymark$P1 == "BB" & tmymark$P2 == "AA"){
         loctype = "MN"
         } else {
         if (tmymark$P1 == "--" & tmymark$P2 == "AA"){
         loctype = "NR"
         }  else {
if (tmymark$P1 == "AA" & tmymark$P2 == "--"){
          loctype = "NR"
} else {
    cat ("error wrong input in P1 or P2")
    }} }}}}}

Here what I am trying to do it compare P1 and P2 values and generated a new variable.
for examp, if tmymark$P1 == “AB” & tmymark$P2 == “AB” the loctype should be “”. If not the second condition will be application and so on.

Here is my error message.

Warning messages:
1: In if (tmymark$P1 == "AB" & tmymark$P2 == "AB") { :
  the condition has length > 1 and only the first element will be used
2: In if (tmymark$P1 == "AB" & tmymark$P2 == "BB") { :
  the condition has length > 1 and only the first element will be used

Once loctype vector is generated I want to recode the tmymark with the information in this variable:

tmymark1 <- data.frame (loctype, tmymark)      
require(car) 
for(i in 2:length(tmymark)){

        if (loctype = "<hkxhk>") {
       tmymark[[i]] <- recode (x, "AB" = "hk", "BA" = "hk", "AA" = "hh", "BB" = "kk")
       } else {
       if (loctype = "<lmxll>") {
       tmymark[[i]] <- recode ((x, "AB" = "lm", "BA" = "lm", "AA" = "--", "BB" = "kk")
       } else {

        if (loctype = "<nnxnp>") {
       tmymark[[i]] <- recode ((x, "AB" = "np", "BA" = "np", "AA" = "nn", "BB" = "--")
             } else {
       if (loctype = "MN") {
        tmymark[[i]] <- "--"
       } esle {
      if (loctype = "NR") {
        tmymark[[i]] <- "NA"
       } else {
       cat ("error wrong input code")
      } } }}} 

Am I on right track ?

Edits: Expected output

      loctype  P1 P2 I1 I2 I3 I4 KL MN 
mark1  <lmxmm> lm mm lm mm mm lm -- mm 
mark2  <hkxhk> hk hk hh kk kk hh -- kk 
mark3 <nnxnp> nn np nn -- -- nn -- -- 
 and so on 
  • 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-29T10:07:20+00:00Added an answer on May 29, 2026 at 10:07 am

    match is definitely the way to go. I’d make two data frames as keys, like this:

    key <- data.frame(
                 P1=c("AB", "AB", "AA", "AA", "BB", "--", "AA"),
                 P2=c("AB", "BB", "AB", "BB", "AA", "AA", "--"),
                 loctype=c("<hkxhk>", "<lmxll>", "<nnxnp>", "MN", "MN", "NR", "NR"))
    
    key2 <- cbind(
      `<hkxhk>` = c("hk","hk","hh","kk"),
      `<lmxll>` = c("lm", "lm", "--", "kk"),
      `<nnxnp>` = c("np", "np", "nn", "--"),
      MN = rep("--", 4),
      NR = rep("NA", 4) )
    rownames(key2) = c("AB","BA", "AA", "BB")
    

    and then use match on key1 to get the loctype (as Justin also recommends), and also on both the rownames and columns of key2 to get the desired substitution, using matrix indexing to get the desired value from the key.

    loctype <- key$loctype[match(with(tmymark, paste(P1, P2, sep="\b")), 
                                 with(key, paste(P1, P2, sep="\b")))]
    ii <- match(as.vector(as.matrix(tmymark)), rownames(key2))
    jj <- rep(match(loctype, colnames(key2)), nrow(tmymark))
    out <- as.data.frame(matrix(key2[cbind(ii,jj)], nrow=nrow(tmymark)))
    colnames(out) <- colnames(tmymark)
    rownames(out) <- rownames(tmymark)
    out$loctype <- loctype
    

    The result then looks like this, where the missing values are because I don’t have values for those combinations in my keys.

    > print(out, na="")
          P1 P2 I1 I2 I3 I4 KL MN loctype
    mark1 lm kk lm kk kk lm    kk <lmxll>
    mark2 hk hk hh kk kk hh    kk <hkxhk>
    mark3                                
    mark4 nn np nn -- -- nn    -- <nnxnp>
    mark5 hk hk hh kk kk hh    kk <hkxhk>
    mark6                                
    mark7                                
    mark8 -- -- -- -- -- --    --      MN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem, looked here & other forums, but could not find
I know I've solved this problem before, but I can't remember or find the
does anybody know such a problem, window.open() do not work with Firefox suddenly. <a
I've encountered a small problem building a web application. My thinking was simple: application
I know it's a fat chance I'll find an answer here, but since devexpress
I got a simple problem. Here's my Android.mk: LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE
Most of you probably know the following problem: You edit an HTML, view the
I don't know the exact problem. I am using flex mx:tree component in flex
I realy dont know what the problem is with VS2010. I created a class,
How do I let Microsoft know about a problem I've found in one of

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.