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

  • Home
  • SEARCH
  • 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 6219195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:44:15+00:00 2026-05-24T07:44:15+00:00

I managed to write a for loop to compare letters in the following vector:

  • 0

I managed to write a for loop to compare letters in the following vector:

bases <- c("G","C","A","T")
test <- sample(bases, replace=T, 20)

test will return

[1] "T" "G" "T" "G" "C" "A" "A" "G" "A" "C" "A" "T" "T" "T" "T" "C" "A" "G" "G" "C"

with the function Comp() I can check if a letter is matching to the next letter

Comp <- function(data)
{
    output <- vector()
    for(i in 1:(length(data)-1))
    {
    if(data[i]==data[i+1])
        {
        output[i] <-1
        }
        else
        {
        output[i] <-0
        }
    }
    return(output)
}

Resulting in;

> Comp(test)
 [1] 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 1 0

This is working, however its verry slow with large numbers. Therefor i tried sapply()

Comp <- function(x,i) if(x[i]==x[i+1]) 1 else 0
unlist(lapply(test, Comp, test))

Unfortunately its not working… (Error in i + 1 : non-numeric argument to binary operator) I have trouble figuring out how to access the preceding letter in the vector to compare it. Also the length(data)-1, to “not compare” the last letter might become a problem.

Thank you all for the help!

Cheers
Lucky

  • 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-24T07:44:17+00:00Added an answer on May 24, 2026 at 7:44 am

    Just “lag” test and use ==, which is vectorized.

    bases <- c("G","C","A","T")
    set.seed(21)
    test <- sample(bases, replace=TRUE, 20)
    lag.test <- c(tail(test,-1),NA)
    #lag.test <- c(NA,head(test,-1))
    test == lag.test
    

    Update:

    Also, your Comp function is slow because you don’t specify the length of output when you initialize it. I suspect you were trying to pre-allocate, but vector() creates a zero-length vector that must be expanded during every iteration of your loop. Your Comp function is significantly faster if you change the call to vector() to vector(length=NROW(data)-1).

    set.seed(21)
    test <- sample(bases, replace=T, 1e5)
    system.time(orig <- Comp(test))
    #    user  system elapsed 
    #  34.760   0.010  34.884 
    system.time(prealloc <- Comp.prealloc(test))
    #    user  system elapsed 
    #    1.18    0.00    1.19 
    identical(orig, prealloc)
    # [1] TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a managed library in C# that will act as an
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
So far I have managed to write some code that should print the source
Can .NET (managed code) read and write to CouchDB? I would like to build
I am looking for a managed code library/API or samples that let me write
What are cons to write and manage CSS in notepad? in compare to any
I want to write a unit test for a Django manage.py command that does
I am pretty new to WinMo development. I managed to write my first app,
I thought I finally had managed to write my first init.d script properly but
Hey guys, with a lot of help from you i was managed to write

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.