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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:02+00:00 2026-06-17T07:41:02+00:00

I have a for loop that is awfully slow and doesnt work proper, it

  • 0

I have a for loop that is awfully slow and doesnt work proper, it looks in 1 data.frame for a barcode and than searches for that barcode in another data.frame. The bar_code of the 2nd data.frame can be there multiple times. Every time it finds a barcode a counter should count the amount of times the barcode is there and write the number of barcodes to the 1st data frame.

My try:

for(i in 1:length(tcgadataUniek$Tumor_Sample_Barcode)){
  for(j in 1:length(hprdDataSorted$Samples.Int1)){
  count<-0
  if(i==j){
    count<-count+1
  } else {
    count<-count+0
  }
  hprdDataSorted$Samples.Int2<-count[j]
  }
}

1st Data.Frame looks as follows (csv):

HUGO.Int1,HUGO.Int2,barcode.Int1
A1CF,APOBEC1,TCGA-B6-A0RS-01A-11D-A099-09
A1CF,TNPO2,TCGA-B6-A0RS-01A-11D-A099-09
A1CF,SYNCRIP,TCGA-B6-A0RS-01A-11D-A099-09
A1CF,KHSRP,TCGA-B6-A0RS-01A-11D-A099-09
A2M,SHBG,TCGA-D8-A1JK-01A-11D-A13L-09
A2M,C11orf58,TCGA-D8-A1JK-01A-11D-A13L-09
A2M,ATF7IP,TCGA-D8-A1JK-01A-11D-A13L-09
AAMP,TH1L,TCGA-A8-A08S-01A-11W-A050-09
AARS,EEF1B2,TCGA-AO-A0JC-01A-11W-A071-09

2nd Data.frame which holds the duplicated barcodes (csv)

Sample_Barcode
TCGA-A8-A08G-01A-11W-A019-09
TCGA-AO-A03O-01A-11W-A019-09
TCGA-AO-A03O-01A-11W-A019-09
TCGA-B6-A0RS-01A-11D-A099-09
TCGA-BH-A0HP-01A-12D-A099-09
TCGA-BH-A0HP-01A-12D-A099-09
TCGA-BH-A18H-01A-11D-A12B-09
TCGA-BH-A18H-01A-11D-A12B-09
TCGA-BH-A18J-01A-11D-A12B-09
TCGA-D8-A1JK-01A-11D-A13L-09
TCGA-E2-A1BC-01A-11D-A14G-09
TCGA-E2-A1BC-01A-11D-A14G-09
TCGA-E9-A1NH-01A-11D-A14G-09
TCGA-E9-A22B-01A-11D-A159-09

If the barcode from barcode.Int1 (dataframe 1) is 3 times in Sample_barcode the script should add a 3 next to the barcode.Int1 the script is looking for. for example:

HUGO.Int1,HUGO.Int2,barcode.Int1, number_of_times
A1CF,APOBEC1,TCGA-B6-A0RS-01A-11D-A099-09,5
  • 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-17T07:41:03+00:00Added an answer on June 17, 2026 at 7:41 am

    Paul’s comment is very appropriate, it will speed up the merge step significantly. I would use table to get the counts of the unique barcodes in your second data.frame and merge it onto your first, see below:

    dat <- structure(list(HUGO.Int1 = c("A1CF", "A1CF", "A1CF", "A1CF", 
    "A2M", "A2M", "A2M", "AAMP", "AARS"), HUGO.Int2 = c("APOBEC1", 
    "TNPO2", "SYNCRIP", "KHSRP", "SHBG", "C11orf58", "ATF7IP", "TH1L", 
    "EEF1B2"), barcode.Int1 = c("TCGA-B6-A0RS-01A-11D-A099-09", "TCGA-B6-A0RS-01A-11D-A099-09", 
    "TCGA-B6-A0RS-01A-11D-A099-09", "TCGA-B6-A0RS-01A-11D-A099-09", 
    "TCGA-D8-A1JK-01A-11D-A13L-09", "TCGA-D8-A1JK-01A-11D-A13L-09", 
    "TCGA-D8-A1JK-01A-11D-A13L-09", "TCGA-A8-A08S-01A-11W-A050-09", 
    "TCGA-AO-A0JC-01A-11W-A071-09")), .Names = c("HUGO.Int1", "HUGO.Int2", 
    "barcode.Int1"), class = "data.frame", row.names = c(NA, -9L))
    
    dat2 <- structure(list(Sample_Barcode = c("TCGA-A8-A08G-01A-11W-A019-09", 
    "TCGA-AO-A03O-01A-11W-A019-09", "TCGA-AO-A03O-01A-11W-A019-09", 
    "TCGA-B6-A0RS-01A-11D-A099-09", "TCGA-BH-A0HP-01A-12D-A099-09", 
    "TCGA-BH-A0HP-01A-12D-A099-09", "TCGA-BH-A18H-01A-11D-A12B-09", 
    "TCGA-BH-A18H-01A-11D-A12B-09", "TCGA-BH-A18J-01A-11D-A12B-09", 
    "TCGA-D8-A1JK-01A-11D-A13L-09", "TCGA-E2-A1BC-01A-11D-A14G-09", 
    "TCGA-E2-A1BC-01A-11D-A14G-09", "TCGA-E9-A1NH-01A-11D-A14G-09", 
    "TCGA-E9-A22B-01A-11D-A159-09")), .Names = "Sample_Barcode", class = "data.frame", row.names = c(NA, 
    -14L))
    
    foo <- as.data.frame(table(dat2))
    merge(dat, foo, by.x='barcode.Int1', by.y='dat2', all.x=TRUE)
    
    
    #                   barcode.Int1 HUGO.Int1 HUGO.Int2 Freq
    # 1 TCGA-A8-A08S-01A-11W-A050-09      AAMP      TH1L   NA
    # 2 TCGA-AO-A0JC-01A-11W-A071-09      AARS    EEF1B2   NA
    # 3 TCGA-B6-A0RS-01A-11D-A099-09      A1CF     TNPO2    1
    # 4 TCGA-B6-A0RS-01A-11D-A099-09      A1CF   SYNCRIP    1
    # 5 TCGA-B6-A0RS-01A-11D-A099-09      A1CF   APOBEC1    1
    # 6 TCGA-B6-A0RS-01A-11D-A099-09      A1CF     KHSRP    1
    # 7 TCGA-D8-A1JK-01A-11D-A13L-09       A2M  C11orf58    1
    # 8 TCGA-D8-A1JK-01A-11D-A13L-09       A2M    ATF7IP    1
    # 9 TCGA-D8-A1JK-01A-11D-A13L-09       A2M      SHBG    1
    

    The data.table version:

    library(data.table)
    
    foo <- data.table(as.data.frame(table(dat2)))
    setnames(foo, c('barcode.Int1', 'Freq'))
    setkey(foo, barcode.Int1)
    
    dat <- data.table(dat, key='barcode.Int1')
    
    foo[dat]
    
    #                    barcode.Int1 Freq HUGO.Int1 HUGO.Int2
    # 1:                           NA   NA      AAMP      TH1L
    # 2:                           NA   NA      AARS    EEF1B2
    # 3: TCGA-B6-A0RS-01A-11D-A099-09    1      A1CF   APOBEC1
    # 4: TCGA-B6-A0RS-01A-11D-A099-09    1      A1CF     TNPO2
    # 5: TCGA-B6-A0RS-01A-11D-A099-09    1      A1CF   SYNCRIP
    # 6: TCGA-B6-A0RS-01A-11D-A099-09    1      A1CF     KHSRP
    # 7: TCGA-D8-A1JK-01A-11D-A13L-09    1       A2M      SHBG
    # 8: TCGA-D8-A1JK-01A-11D-A13L-09    1       A2M  C11orf58
    # 9: TCGA-D8-A1JK-01A-11D-A13L-09    1       A2M    ATF7IP
    

    in “Pure” data.table:

    dat <- data.table(dat, key='barcode.Int1')
    dat2 <- data.table(dat2)
    setnames(dat2, 'barcode.Int1')
    setkey(dat2, barcode.Int1)
    
    counts <- dat2[, list(count= .N), by=barcode.Int1]
    
    counts[dat]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop that looks like this def slow_loop(array) array.each_with_index do |item, i|
Say I have a loop that looks like this: for(int i = 0; i
I have a loop that reads plenty of data from an external source. The
I have a loop that looks like this: for (int i = 0; i
I have a loop that looks something like this while(condition){ read_some_data(source, buf, BUFSIZE); printf(buf);
I have a loop that enters each non-empty text field into a database: foreach
I have a loop that finds duplicate lines in a .ini file. I can
I have a loop that runs through a variety of websites and I'd like
I have a loop that runs for approx. 25 minutes i.e 1500 seconds. [100
I have a loop that calculates the similarity between two documents. It collects all

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.