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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:03:07+00:00 2026-05-27T10:03:07+00:00

I am working on a large dataset which comprises travel behaviour data over a

  • 0

I am working on a large dataset which comprises travel behaviour data over a weekly period. Over the course of a week, people have completed a log of the individual trips that they have taken place during that week. Individuals are identified by a unique identification number (ID). What I want to do is ramdonly choose two days of diary data (which may comprise one or many trips) from the weekly data that is available for each unique ID, and put this in a new data frame. An example data frame is detailed below:

Df1 <- data.frame(ID = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3), 
                  date = c("1st Nov", "1st Nov", "3rd Nov", "4th Nov","4th Nov","5th Nov","2nd Nov", "2nd Nov", "3nd Nov", "4th Nov","5th Nov","5th Nov","2nd Nov", "2nd Nov", "3nd Nov", "4th Nov","5th Nov"))

Any help on the above would be gratefully received.

Many thanks,

Katie

  • 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-27T10:03:08+00:00Added an answer on May 27, 2026 at 10:03 am

    Sounds like a job for plyr. To sample two random days for each user:

    library(plyr)
    ddply(Df1, .(ID), function(x) {
      unique_days = as.character(unique(x$date))
      if(length(unique_days) < 2) {
        randomSelDays = unique_days
      } else {
        randomSelDays = sample(unique_days, 2)        
      }
      return(x[x$date %in% randomSelDays,])
    })
    

    This returns all the data for two selected days per unique identifier. In addition, if there is just one day for an ID, that day is returned. For example:

      ID    date
    1  1 1st Nov
    2  1 1st Nov
    3  1 3rd Nov
    4  2 3nd Nov
    5  2 5th Nov
    6  2 5th Nov
    7  3 2nd Nov
    8  3 2nd Nov
    9  3 3nd Nov
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im working with a large dataset of point of interest (POI) which all have
I have two tables: p_group.full_data, which is a large dataset I'm working on (100k
I am working on a large dataset showing how people travel. I need to
Working with a relatively large data set I have a situation where I need
I have a large data set of Course objects in a Django application. courses
I am working on a large dataset, an example of which is shown below:
I am working on a project which will be using large datasets (both 2D
I'm working with large numbers that I can't have rounded off. Using Lua's standard
We are working with large amounts of data, all tagged in UTC (in Java).
I have a set of data from a table ( TableA ) which relates

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.