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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:54:05+00:00 2026-05-23T01:54:05+00:00

I have a dataframe that looks like this DF: V1 V2 V3 V4 V5

  • 0

I have a dataframe that looks like this

DF:

V1  V2          V3  V4  V5  V6  V7  V8      
0   ss66369915  0   0   G   A   A   A
0   ss66112992  0   0   A   A   A   A
0   ss66369329  0   0   A   A   A   A
0   ss66368644  0   0   A   A   A   A
0   ss66368284  0   0   A   A   G   A
0   ss66126380  0   0   A   G   A   G
0   ss66407282  0   0   A   A   A   A
0   ss66405035  0   0   A   A   A   A
0   ss66405148  0   0   G   G   A   G
0   ss66405271  0   0   G   G   G   G

The data in columns V6 through V9 are biallelic genotypes, so I would like to merge every two columns together into one.

For example, it would look like:

V1  V2          V3  V4 V5_V6 V7 V8     
0   ss66369915  0   0   GA  A   A
0   ss66112992  0   0   AA  A   A
0   ss66369329  0   0   AA  A   A
0   ss66368644  0   0   AA  A   A
0   ss66368284  0   0   AA  G   A
0   ss66126380  0   0   AG  A   G
0   ss66407282  0   0   AA  A   A
0   ss66405035  0   0   AA  A   A
0   ss66405148  0   0   GG  A   G
0   ss66405271  0   0   GG  G   G

I was able to do this using:

DF$V5_V6=paste(DF$V5, DF$V6, sep="")

or

within(DF, V5_V6 <- paste(V5, V6, sep=''))

However my actual dataframe consists of 4776 rows and I would have to merge every two columns starting from column 5 to column 4776.

I was wondering how I could achieve this without doing it manually. I tried to use a for loop with no success. I am very new to using R.

Thank you!

  • 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-23T01:54:06+00:00Added an answer on May 23, 2026 at 1:54 am

    Maybe you can show the for loop you tried?

    Here’s one approach using a loop that should do what you want, if I understand what you want. Specifically – this for loop will paste the values of columns 5 & 6, 7 & 8, 9 & 10, etc together. We use the names() function to extract the relevant column names and paste them together. We use [ to index into the object newdat that is created.

    #read in data
    txt <- "V1  V2          V3  V4  V5  V6  V7  V8      
    0   ss66369915  0   0   G   A   A   A
    0   ss66112992  0   0   A   A   A   A
    0   ss66369329  0   0   A   A   A   A
    0   ss66368644  0   0   A   A   A   A
    0   ss66368284  0   0   A   A   G   A
    0   ss66126380  0   0   A   G   A   G
    0   ss66407282  0   0   A   A   A   A
    0   ss66405035  0   0   A   A   A   A
    0   ss66405148  0   0   G   G   A   G
    0   ss66405271  0   0   G   G   G   G"
    
    dat <- read.table(textConnection(txt), header = TRUE)
    
    #Create a new object so as to not interfere with the original
    newdat <- dat[, 1:4]
    
    for (colInd in seq(5, (ncol(dat) - 1), by = 2)) {
      colNames <- paste(names(dat)[colInd], names(dat)[colInd + 1], sep = "_")
      newdat[, colNames] <- paste(dat[, colInd], dat[, colInd + 1], sep = "")
    }
    

    Results in:

    > newdat
       V1         V2 V3 V4 V5_V6 V7_V8
    1   0 ss66369915  0  0    GA    AA
    2   0 ss66112992  0  0    AA    AA
    3   0 ss66369329  0  0    AA    AA
    4   0 ss66368644  0  0    AA    AA
    5   0 ss66368284  0  0    AA    GA
    6   0 ss66126380  0  0    AG    AG
    7   0 ss66407282  0  0    AA    AA
    8   0 ss66405035  0  0    AA    AA
    9   0 ss66405148  0  0    GG    AG
    10  0 ss66405271  0  0    GG    GG
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
I'm working in R. I have a dataframe, df that looks like this: >
I have a dataframe that looks like this: data median min max no_of_threads 2.33
I have a dataframe that looks like this: person n start end 1 sam
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
I have a data frame that looks like this: user1,product1,0 user1,product2,2 user1,product3,1 user1,product4,2 user2,product3,0
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >
I have a large data frame (named z ) that looks like this: RPos

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.