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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:34:18+00:00 2026-06-06T00:34:18+00:00

Here’s a sample column of my data frame, RR is a header : RR

  • 0

Here’s a sample column of my data frame, RR is a header :

RR
Cvv  
Cvv  
Caa 

What I need is to “invert” the datas, so to get substrings vv and aa as headers, and RR in the data frame. The resulting matrix would be :

vv  | aa  
CRR |  
CRR |  
    | CRR  

So we get the same relationships in both matrix. On the first and second row, vv is coupled with RR. On the third row, aa is coupled with RR.

Is this achievable with R ? Any ideas ?

Thanks for looking !

I oversimplified my datas in the example above. So here’s a sample of my actual dataset :

> dput(head(A1F[4:15],n=20))
structure(list(RR = structure(c(15L, 15L, 15L, 27L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
" ", "Caa", "Caj", "Cbb", "Cbb ", "Cbv", "Cja", "Cjr", "Crj", 
"Crr", "Crv", "Cvb", "Cvr", "Cvv", "Gaa", "Gaj", "Gbb", "Gbv", 
"Gja", "Gjr", "Grj", "Grr", "Grv", "Gvb", "Gvr", "Gvv"), class = "factor"), 
    AA = structure(c(13L, 13L, 13L, 1L, 1L, 1L, 1L, 15L, 27L, 
    27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 1L), .Label = c("", 
    "Caa", "Caj", "Car", "Cbb", "Cbv", "Cja", "Cjr", "Cjr ", 
    "Crj", "Crr", "Crv", "Cvb", "Cvr", "Cvv", "Gaa", "Gaj", "Gbb", 
    "Gbv", "Gja", "Gjr", "Grj", "Grr", "Grv", "Gvb", "Gvr", "Gvv"
    ), class = "factor"), BB = structure(c(9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L
    ), .Label = c("", "?", "Caa", "Caj", "Cbv", "Cja", "Cjr", 
    "Crj", "Crr", "Crv", "Cvb", "Cvr", "Cvv", "Gaa", "Gaj", "Gbv", 
    "Gja", "Gjr", "Grj", "Grr", "Grv", "Gvb", "Gvr", "Gvv"), class = "factor"), 
    VV = structure(c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 1L, 1L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("", 
    " ", "Caa", "Caj", "Caj+", "Cbb", "Cbv", "Cja", "Cjr", "Crv", 
    "Cvb", "Cvr", "Cvv", "Gaa", "Gbb", "Gja", "Gjr", "Grv", "Gvb", 
    "Gvr"), class = "factor"), RJ = structure(c(8L, 3L, 3L, 1L, 
    1L, 12L, 12L, 12L, 12L, 12L, 1L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L), .Label = c("", "Caa", "Caj", "Cbv", 
    "Ccrj", "Cja", "Cjr", "Crj", "Crj ", "Crr", "Crv", "Cvr", 
    "Cvv", "Gaa", "Gaj", "Gbv", "Gja", "Gjr", "Grj", "Grr", "Grv", 
    "Gvr", "Gvv"), class = "factor"), JR = structure(c(7L, 7L, 
    18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
    18L, 18L, 18L, 18L, 18L, 18L), .Label = c("", "Caa", "Caj", 
    "Cbv", "Cja", "Cjr", "Crj", "Crr", "Crv", "Cvb", "Cvr", "Cvv", 
    "Gaa", "Gaj", "Gbv", "Gja", "Gjr", "Grj", "Grr", "Grv", "Grv ", 
    "Gvb", "Gvb ", "Gvr", "Gvv"), class = "factor"), BV = structure(c(4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L), .Label = c("", "Caa", "Caj", "Cbb", "Cbv", 
    "Cja", "Cjr", "Crj", "Crr", "Crv", "Cvb", "Cvr", "Cvv", "Gaa", 
    "Gaj", "Gbb", "Gbv", "Gja", "Gjr", "Grj", "Grv", "Gvb", "Gvr", 
    "Gvv", "R"), class = "factor"), VB = structure(c(1L, 1L, 
7L, 7L, 18L, 18L, 1L, 1L, 10L, 10L, 21L, 21L, 21L, 1L, 21L, 
21L, 21L, 21L, 21L, 1L), .Label = c("", "Caa", "Caj", "Cbb", 
"Cbv", "Cja", "Cjr", "Crj", "Crr", "Crv", "Cvb", "Cvv", "Gaa", 
"Gaj", "Gbb", "Gbv", "Gja", "Gjr", "Grj", "Grr", "Grv", "Gvb", 
"Gvr", "Gvv"), class = "factor"), AJ = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 
1L, 10L, 10L), .Label = c("", "Caa", "Caj", "Cbb", "Cbv", 
"Cja", "Cjr", "Crj", "Crv", "Cvb", "Cvr", "Cvv", "Gaa", "Gaj", 
"Gbb", "Gbv", "Gja", "Gjr", "Grj", "Grj ", "Grr", "Grv", 
"Gvb", "Gvr", "Gvv"), class = "factor"), JA = structure(c(10L, 
10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 6L, 6L, 6L, 6L), .Label = c("", "Caa", "Caj", "Cbv", 
"Cja", "Cjr", "Crr", "Crv", "Cvb", "Cvr", "Cvv", "Gaa", "Gaj", 
"Gbv", "Gja", "Gjr", "Grr", "Grv", "Gvb", "Gvv"), class = "factor"), 
VR = structure(c(1L, 5L, 5L, 5L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("", 
"Caa", "Caj", "Caj ", "Cbv", "Cja", "Cjr", "Crj", "Crr", 
"Crv", "Cvb", "Cvr", "Cvv", "Gaa", "Gaj", "Gbv", "Gja", "Gjr", 
"Grj", "Grr", "Grv", "Gvb", "Gvr", "Gvv"), class = "factor"), 
RV = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 1L, 1L), .Label = c("", 
"Caa", "Caj", "Cbb", "Cbv", "Cja", "Cjr", "Crj", "Crr", "Crv", 
"Cvr", "Cvv", "Cvv ", "Gaa", "Gaj", "Gbb", "Gbv", "Gja", 
"Gjr", "Grj", "Grr", "Grv", "Gvr", "Gvv"), class = "factor")), .Names = c("RR", 
"AA", "BB", "VV", "RJ", "JR", "BV", "VB", "AJ", "JA", "VR", "RV"
), row.names = c(NA, 20L), class = "data.frame")

The desired matrix would keep the relationships and row order, as stated above. GSee provided an answer that I could apply, but only to one column of my matrix, as [[ select for just specific entry and selecting multiple entry’s with [ doesn’t work. I’m not sure if I’m heading in the right direction with this…

Here’s what the desired output (the first three rows) would look like, based on the actual dataset (as above):

structure(list(vv = structure(c(1L, 1L, 1L), .Label = "CRR", class = "factor"), 
    rv = c(NA, NA, NA), ja = structure(c(1L, 1L, 1L), .Label = "CVV", class = "factor"), 
    aa = structure(c(1L, 1L, 1L), .Label = "CAJ", class = "factor"), 
    bv = structure(c(1L, 2L, 2L), .Label = c("", "CVR"), class = "factor"), 
    aj = structure(c(1L, 2L, 2L), .Label = c("", "CRJ"), class = "factor"), 
    vb = structure(c(1L, 1L, 1L), .Label = "CAA", class = "factor"), 
    rj = structure(c(2L, 1L, 1L), .Label = c("", "CRJ"), class = "factor"), 
    rr = structure(c(1L, 1L, 1L), .Label = "CBB", class = "factor"), 
    vr = structure(c(1L, 1L, 1L), .Label = "CJA", class = "factor"), 
    bb = structure(c(1L, 1L, 1L), .Label = "CBV", class = "factor"), 
    jr = c(NA, NA, NA)), .Names = c("vv", "rv", "ja", "aa", "bv", 
"aj", "vb", "rj", "rr", "vr", "bb", "jr"), class = "data.frame", row.names = c(NA, 
-3L))

I hope this make more sense.

  • 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-06T00:34:19+00:00Added an answer on June 6, 2026 at 12:34 am

    Alright. i haz teh codez 4 u.

    #dat is the data.frame that was created from the `dput` output in the question
    m <- as.matrix(dat) #convert to matrix
    m[10, "AJ"] <- "" # Fix the typo/error in your data
    

    Find the names of the output matrix, and make the matrix (filled with NA for now)

    ocn <- unique(substr(paste(m[m!=""]), 2, 3)) #out column names
    out <- matrix(NA, nrow(m), length(ocn))
    colnames(out) <- ocn
    

    loop through each row of each column

    for (i in seq_len(NCOL(m))) { #for each column
      cn <- colnames(m)[i] #this will become the second 2 characters of new value
      for (j in seq_along(m[, i])) { # for each row of this column
        if (nzchar(m[j, i])) { # if there is something there (i.e. it is not "")
          # do the substitution
          out[j, substr(m[j, i], 2, 3)] <- paste0(substr(m[j, i], 1, 1), cn)   
        }
      }
    }
    out
    #      vv    vb    rr    ja    rj    aj    vr    bb    jr    rv    aa    bv   
    # [1,] "CRR" "CAA" "CBB" "CVV" "CJR" NA    "CJA" "CBV" NA    NA    "CAJ" NA   
    # [2,] "CRR" "CAA" "CBB" "CVV" "CJR" "CRJ" "CJA" "CBV" NA    NA    "CAJ" "CVR"
    # [3,] "CRR" "CAA" "CBB" "CVV" "GJR" "CRJ" "CJA" "CBV" "CVB" NA    "CAJ" "CVR"
    # [4,] "GRR" NA    "CBB" "CVV" "GJR" NA    NA    "CBV" "CVB" NA    "CAJ" "CVR"
    # [5,] NA    NA    "CBB" "CVV" "GJR" NA    NA    "CBV" "GVB" NA    "CAJ" "GVR"
    # [6,] NA    NA    "CBB" "CVV" "GJR" NA    "CRJ" "CBV" "GVB" NA    "CAJ" "GVR"
    # [7,] NA    NA    "CBB" "CVV" "GJR" NA    "CRJ" "CBV" NA    NA    "CAJ" "GVR"
    # [8,] "CAA" NA    "CBB" "CVV" "GJR" NA    "CRJ" "CBV" NA    NA    "CAJ" "GVR"
    # [9,] "GAA" NA    "CBB" "CVV" "GJR" "CRV" "CRJ" "CBV" NA    "CVB" NA    "GVR"
    # [10,] "GAA" NA    "CBB" "CVV" "GJR" "CRV" "CRJ" "CBV" NA    "CVB" NA    "GVR"
    # [11,] "GAA" NA    "CBB" "CVV" "GJR" "GRV" NA    "CBV" NA    "GVB" NA    "GVR"
    # [12,] "GAA" NA    "CBB" NA    "GJR" "GRV" "CRJ" "CBV" NA    "GVB" NA    "GVR"
    # [13,] "GAA" NA    "CBB" NA    "GJR" "GRV" "CRJ" "CBV" NA    "GVB" NA    "GVR"
    # [14,] "GAA" NA    "CBB" "GVV" "GJR" "GRV" "CRJ" "CBV" NA    NA    NA    "GVR"
    # [15,] "GAA" NA    "CBB" "GVV" "GJR" "GRV" "CRJ" "CBV" NA    "GVB" NA    "GVR"
    # [16,] "GAA" NA    "CBB" "GVV" "GJR" "GRV" "CRJ" "CBV" NA    "GVB" NA    "GVR"
    # [17,] "GAA" "CAJ" "CBB" "GVV" "GJR" "GRV" "CRJ" "CBV" "CJA" "GVB" NA    "GVR"
    # [18,] "GAA" NA    "CBB" "GVV" "GJR" "GRV" "CRJ" "CBV" "CJA" "GVB" NA    "GVR"
    # [19,] "GAA" "CAJ" "CBB" "GVV" "GJR" NA    "CRJ" "CBV" "CJA" "GVB" NA    "GVR"
    # [20,] NA    "CAJ" "CBB" "GVV" "GJR" NA    "CRJ" "CBV" "CJA" NA    NA    "GVR"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code sample, let me know if it can be further improved?
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here is an example of what I need http://code.google.com/webtoolkit/doc/latest/tutorial/gettingstarted.html I need interactive list to
Here's my situation: I have a Data Template set up which contains a ToggleButton
Here's some CSS and HTML to make a textarea below a list of data
Here is a sample of text that I’m working with: Word1 Word2 ... Word4
Here's the question, I need to connect to a remote MySQL database from my
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works

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.