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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:57:10+00:00 2026-06-02T15:57:10+00:00

I have a data frame, with only one column, and I want to turn

  • 0

I have a data frame, with only one column, and I want to turn it into a two column by selecting only certain rows, here…let me show you

I want to go from this

       V1   
1    one
2    two
3    &&three
4    four
5    &&five
6    six

to this

     V1      V2
1    one     NA
2    two     three
3    four    five
4    six     NA

So the ones that have a && are placed in column two, to the right of the above row (hope that makes sense). Is there any way I can do this?

  • 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-02T15:57:11+00:00Added an answer on June 2, 2026 at 3:57 pm

    Here’s my approach:

    Read in the data:

    dat <- read.table(text="       V1   
    1    one
    2    two
    3    &&three
    4    four
    5    &&five
    6    six", head=T, stringsAsFactors = FALSE)
    

    The reshaping:

    j <- grep("&&", dat$V1)                                 #find && rows
    l <- j-1                                                #find rows above && rows
    dat$V2 <- rep(NA, nrow(dat))                            #create NA vector
    dat$V2[l] <- gsub("&&", "", dat[grep("&&", dat$V1), 1]) #see what it does :)
    dat2 <- dat[-j, ]                                       #get rid of the && rows
    rownames(dat2) <- 1:nrow(dat2)                          #rename rows
    

    Which yields:

        V1    V2
    1  one  <NA>
    2  two three
    3 four  five
    4  six  <NA>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two columns. First column contains categories such as
I have a data frame where one particular column has a set of specific
I have a data frame containing 10k rows, for a given column X I
I have two data.frames, one with only characters and the other one with characters
I have a 114 row by 16 column data frame where the rows are
I have a JSON character string that I put into a data frame. I
I have the following matching problem: I have two data.frames, one with an observation
Let's say I have a list of data frames. Where each data frame has
I often have data where I want to compare the value of one level
I often run into an issue where I have a data frame that has

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.