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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:31:06+00:00 2026-06-15T02:31:06+00:00

I have a data structure df with some date vectors, one thou five, like

  • 0

I have a data structure df with some date vectors, one thou five, like this,

df <- structure(list(one.date = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, "2012-09-23", "2012-09-23", NA, NA, NA, NA, NA, NA, NA, NA, NA), two.date = c(NA, "2012-11-13", NA, "2012-11-19", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "2012-09-24", NA, NA, NA), three.date = c(NA, NA, "2012-11-19", NA, NA, NA, NA, NA, "2012-09-22", NA, NA, NA, NA, NA, NA, NA, NA, "2012-09-24", NA, NA), four.date = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "2012-09-02", "2012-09-10","2012-09-23", NA, NA, NA, NA, NA, NA), five.date = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "2012-09-24", "2014-09-09", NA, NA, NA, NA)), .Names = c("one.date", "two.date", "three.date", "four.date", "five.date"), row.names = c(NA, 20L), class = "data.frame")
# > df
     one.date   two.date three.date  four.date  five.date
1        <NA>       <NA>       <NA>       <NA>       <NA>
2        <NA> 2012-11-13       <NA>       <NA>       <NA>
3        <NA>       <NA> 2012-11-19       <NA>       <NA>
4        <NA> 2012-11-19       <NA>       <NA>       <NA>
5        <NA>       <NA>       <NA>       <NA>       <NA>
6        <NA>       <NA>       <NA>       <NA>       <NA>
7        <NA>       <NA>       <NA>       <NA>       <NA>
8        <NA>       <NA>       <NA>       <NA>       <NA>
9        <NA>       <NA> 2012-09-22       <NA>       <NA>
10 2012-09-23       <NA>       <NA>       <NA>       <NA>
11 2012-09-23       <NA>       <NA>       <NA>       <NA>
12       <NA>       <NA>       <NA> 2012-09-02       <NA>
13       <NA>       <NA>       <NA> 2012-09-10       <NA>
14       <NA>       <NA>       <NA> 2012-09-23       <NA>
15       <NA>       <NA>       <NA>       <NA> 2012-09-24
16       <NA>       <NA>       <NA>       <NA> 2014-09-09
17       <NA> 2012-09-24       <NA>       <NA>       <NA>
18       <NA>       <NA> 2012-09-24       <NA>       <NA>
19       <NA>       <NA>       <NA>       <NA>       <NA>
20       <NA>       <NA>       <NA>       <NA>       <NA>

Now, I would like to combine, or merge, them into one vector. Like this,

            date  one.date   two.date three.date  four.date  five.date
  1        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
  2  2012-11-13       <NA> 2012-11-13       <NA>       <NA>       <NA>
  3  2012-11-19       <NA>       <NA> 2012-11-19       <NA>       <NA>
  4  2012-11-19       <NA> 2012-11-19       <NA>       <NA>       <NA>
  5        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
  6        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
  7        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
  8        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
  9  2012-09-22       <NA>       <NA> 2012-09-22       <NA>       <NA>
  10 2012-09-23 2012-09-23       <NA>       <NA>       <NA>       <NA>
  11 2012-09-23 2012-09-23       <NA>       <NA>       <NA>       <NA>
  12 2012-09-02       <NA>       <NA>       <NA> 2012-09-02       <NA>
  13 2012-09-10       <NA>       <NA>       <NA> 2012-09-10       <NA>
  14 2012-09-23       <NA>       <NA>       <NA> 2012-09-23       <NA>
  15 2012-09-24       <NA>       <NA>       <NA>       <NA> 2012-09-24
  16 2014-09-09       <NA>       <NA>       <NA>       <NA> 2014-09-09
  17 2012-09-24       <NA> 2012-09-24       <NA>       <NA>       <NA>
  18 2012-09-24       <NA>       <NA> 2012-09-24       <NA>       <NA>
  19       <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
  20       <NA>       <NA>       <NA>       <NA>       <NA>       <NA>

Any help would be appreciated.

  • 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-15T02:31:07+00:00Added an answer on June 15, 2026 at 2:31 am

    I’m assuming that there is at most 1 non-NA entry per row:

    as.vector(apply(df,1,function(x) ifelse(all(is.na(x)),NA,x[!is.na(x)])))
     [1] NA           "2012-11-13" "2012-11-19" "2012-11-19" NA          
     [6] NA           NA           NA           "2012-09-22" "2012-09-23"
    [11] "2012-09-23" "2012-09-02" "2012-09-10" "2012-09-23" "2012-09-24"
    [16] "2014-09-09" "2012-09-24" "2012-09-24" NA           NA
    

    Or to have it as a factor as you have in your original data:

    cbind(list(date=as.factor(apply(df,1,function(x) ifelse(all(is.na(x)),NA,x[!is.na(x)])))),df)
             date   one.date   two.date three.date  four.date  five.date
    1        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    2  2012-11-13       <NA> 2012-11-13       <NA>       <NA>       <NA>
    3  2012-11-19       <NA>       <NA> 2012-11-19       <NA>       <NA>
    4  2012-11-19       <NA> 2012-11-19       <NA>       <NA>       <NA>
    5        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    6        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    7        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    8        <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    9  2012-09-22       <NA>       <NA> 2012-09-22       <NA>       <NA>
    10 2012-09-23 2012-09-23       <NA>       <NA>       <NA>       <NA>
    11 2012-09-23 2012-09-23       <NA>       <NA>       <NA>       <NA>
    12 2012-09-02       <NA>       <NA>       <NA> 2012-09-02       <NA>
    13 2012-09-10       <NA>       <NA>       <NA> 2012-09-10       <NA>
    14 2012-09-23       <NA>       <NA>       <NA> 2012-09-23       <NA>
    15 2012-09-24       <NA>       <NA>       <NA>       <NA> 2012-09-24
    16 2014-09-09       <NA>       <NA>       <NA>       <NA> 2014-09-09
    17 2012-09-24       <NA> 2012-09-24       <NA>       <NA>       <NA>
    18 2012-09-24       <NA>       <NA> 2012-09-24       <NA>       <NA>
    19       <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    20       <NA>       <NA>       <NA>       <NA>       <NA>       <NA>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some C structures related to a 'list' data structure. They look like
Say you have a simple class with some storage data structure (list, vector, queue,
I have some data and need to create a json file with this structure
My table have data structure like this cate_id task_id date_start date_end other 34 14
I have data in my database that looks like this: Date (DateTime) Type (varchar)
i have data structure i am passing this from server to client using data
I have a pipe delimited file with some NULL date fields. I load this
My db structure is as below and I also have some data. How do
I have two data.frames that I want to append together. One data.frame holds past
I have two tables, first table structure (data) : -id -name -title -mail -source_id

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.