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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:38:17+00:00 2026-06-16T10:38:17+00:00

I have a database that looks like this: start<-as.POSIXct("2012-01-15") interval<-60 end<-start+as.difftime(31,units="days") date<-seq(from=start,by=interval*60, to=end) #

  • 0

I have a database that looks like this:

start<-as.POSIXct("2012-01-15")
interval<-60
end<-start+as.difftime(31,units="days")
date<-seq(from=start,by=interval*60, to=end) # date/time information
l<-length(date)

stations<-as.factor(rep(1:3,len=l)) # stations
df<-data.frame(date,stations) # data frame

What I would like is to reshape the station column from this data frame into several columns (in this example it will be 3 columns) and calculate the number of time each station was recorded in each date/time row. However, I would like to keep the original date/time column from the data base. If a station was not recorded in one specific date/time, then I want to assign a value of zero.

Ideally, I would like an output like this:

date              1   2   3
2012-01-15 0:00   1   0   0
2012-01-15 1:00   0   1   0
2012-01-15 2:00   0   0   1
2012-01-15 3:00   1   0   0
2012-01-15 4:00   0   1   0
2012-01-15 5:00   0   0   1
2012-01-15 6:00   1   0   0
2012-01-15 7:00   0   1   0
2012-01-15 8:00   0   0   1
2012-01-15 9:00   1   0   0
2012-01-15 10:00  0   1   0
  • 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-16T10:38:18+00:00Added an answer on June 16, 2026 at 10:38 am

    You can try to use function dcast() from library reshape2.

    library(reshape2)
    dcast(df,date~stations,length)
                       date 1 2 3
    1   2012-01-15 00:00:00 1 0 0
    2   2012-01-15 01:00:00 0 1 0
    3   2012-01-15 02:00:00 0 0 1
    4   2012-01-15 03:00:00 1 0 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql database that looks like this: |id | city | created
say i have a nvarchar field in my database that looks like this 1,
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a database that essentially looks like this: id uniqueidentifier NOT NULL data
I have table in an MS Access database that looks like this: ID Symbol
I have a database that look like this: [Invoice] -PK:ID -... [InvoiceDetail] -PK:ID -FK:Invoice_ID
I have many collections of documents in a mongo database that look like this:
I have a database full of text fields that look like this: (paragraph of
I have some code to update a database table that looks like try {
I have a self-referential table in my database that looks sort of like above.

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.