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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:56:36+00:00 2026-05-22T17:56:36+00:00

I have a dataframe like variable x. x<-start.x stop.x strand.x start.y stop.y strand.y 1

  • 0

I have a dataframe like variable x.

x<-"start.x    stop.x strand.x   start.y    stop.y strand.y
1  16954189  16963562        -  16954189  16963562        -
2  16954189  16963562        - 150045170 150065177        -
3 150045170 150065177        -  16954189  16963562        -
4 150045170 150065177        - 150045170 150065177        -
5  97061519  97190927        -  97061519  97190927        -
6  97061519  97190927        - 135190856 135202610        +
7 135190856 135202610        +  97061519  97190927        -
8 135190856 135202610        + 135190856 135202610        +"

dat <- read.table(textConnection(x), header=TRUE)

Normally I calculate for each row the relative distance between start.x and start.y with the following code:

zz <- transform(x, 
  distance_startsite = abs(as.numeric(start.x) - as.numeric(start.y)))

But before calculating this time, we first need to look to the strand.x and strand.y.

  • If the strand.x is “-” the official start site is stop.x
  • If the strand.x is “+” the official start site is start.x
  • If the strand.y is “-” the official start site is stop.y
  • If the strand.y is “+” the official start site is start.y

Row 1 in table dat must calucate this: abs(as.numeric(stop.x) – as.numeric(stop.y) instead of abs(as.numeric(start.x) – as.numeric(start.y).

My question is, is there a way to calculate this for each row like zz?

Thanks

EDIT: my first thought was something like this:

for (i in 1:nrow(dd)){
if (dat$strand.x[i,] == "-" & dat$stand.y[i,] == "-") {
  result[i]<-transform(dat,distance_startsite[i] = abs(as.numeric(stop.x[i,]) - as.numeric(stop.y[i,]))} else
if (dat$strand.x[i,] == "+" & dat$stand.y[i,] == "-") {
  result[i]<-transform(dat,distance_startsite[i] = abs(as.numeric(start.x[i,]) - as.numeric(stop.y[i,]))} else
if (dat$strand.x[i,] == "-" & dat$stand.y[i,] == "+") {
  result[i]<-transform(dat,distance_startsite[i] = abs(as.numeric(stop.x[i,]) - as.numeric(start.y[i,]))} else
if (dat$strand.x[i,] == "+" & dat$stand.y[i,] == "+") {
  result[i]<-transform(dat,distance_startsite[i] = abs(as.numeric(start.x[i,]) - as.numeric(start.y[i,]))} 
 }

But that doesn’t work yet.

  • 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-22T17:56:37+00:00Added an answer on May 22, 2026 at 5:56 pm

    If you do this step by step and use some interim variables, you will save yourself a lot of trouble and your code will become much clearer.

    Here is what I suggest:

    1. Add a column with the start and stop values (using your conditions)
    2. Calculate the absolute difference

    Two further observations:

    • Your start and stop values are integer values, so you don’t need to use as.numeric all the time
    • In your original question you have conflicting conditions for the start site, but no conditions for the stop site, so I took a guess to what you really meant.

    The code:

    dat$start <- with(dat, ifelse(strand.x=="+", start.x, stop.x))
    dat$stop  <- with(dat, ifelse(strand.y=="+", start.y, stop.y))
    dat$dist  <- with(dat, abs(stop-start))
    

    The results:

    dat
    
        start.x    stop.x strand.x   start.y    stop.y strand.y      dist
    1  16954189  16963562        -  16954189  16963562        -         0
    2  16954189  16963562        - 150045170 150065177        - 133101615
    3 150045170 150065177        -  16954189  16963562        - 133101615
    4 150045170 150065177        - 150045170 150065177        -         0
    5  97061519  97190927        -  97061519  97190927        -         0
    6  97061519  97190927        - 135190856 135202610        +  37999929
    7 135190856 135202610        +  97061519  97190927        -  37999929
    8 135190856 135202610        + 135190856 135202610        +         0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe that looks like this: person n start end 1 sam
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a dataframe and would like to add a new column where the
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe and I would like to apply a function that takes
I have a dataframe with a column of integers that I would like to
I have a dataframe with one column that I would like to split into
I'd like to melt the dataframe so that in one column I have dates
I have a big dataframe, but small example would be like this: mydf <-
I have a dataframe of which the columns contain a variable amount of numbers

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.