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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:04:58+00:00 2026-06-14T13:04:58+00:00

I have the following dataset (see for loading dataset below) ID Date qty 1

  • 0

I have the following dataset (see for loading dataset below)

     ID       Date qty
1  ID25 2007-12-01  45
2  ID25 2008-01-01  26
3  ID25 2008-02-01  46
4  ID25 2008-03-01   0
5  ID25 2008-04-01  78
6  ID25 2008-05-01  65
7  ID25 2008-06-01  32
8  ID99 2008-02-01  99
9  ID99 2008-03-01   0
10 ID99 2008-04-01  99

And I would like to create a pivot table of that. I do that with the following command and that seems to be working fine:

pivottable <- xtabs(qty ~ ID + Date, table)

The output is the following:

ID     2007-12-01 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01
ID25         45         26         46          0         78         65         32
ID99          0          0         99          0         99          0          0

However, for ID99 there are only values for 3 periods the rest is marked as ‘0’. I would like to display NA in the fields that have no values in the first table. I would like to get a table that looks as following:

ID     2007-12-01 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01
ID25         45         26         46          0         78         65         32
ID99         NA         NA         99          0         99         NA         NA

Any suggestion on how to accomplish this?

Loading dataset:

table <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("ID25", "ID99"), class = "factor"), Date = structure(c(7L, 
1L, 2L, 3L, 4L, 5L, 6L, 2L, 3L, 4L), .Label = c("01/01/2008", 
"01/02/2008", "01/03/2008", "01/04/2008", "01/05/2008", "01/06/2008", 
"01/12/2007"), class = "factor"), qty = c(45L, 26L, 46L, 0L, 
78L, 65L, 32L, 99L, 0L, 99L)), .Names = c("ID", "Date", "qty"
), class = "data.frame", row.names = c(NA, -10L))

table$Date <- as.POSIXct(table$Date, format='%d/%m/%Y')
  • 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-14T13:05:01+00:00Added an answer on June 14, 2026 at 1:05 pm

    You could use xtabs twice to obtain the output you are looking for:

    1. Create the table:

      pivottable <- xtabs(qty ~ ID + Date, table)
      
    2. Replace all zeros of non-existing combinations with NA:

      pivottable[!xtabs( ~ ID + Date, table)] <- NA
      

    The output:

          Date
    ID     2007-12-01 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01
      ID25         45         26         46          0         78         65         32
      ID99                               99          0         99                      
    

    Note that NAs are not displayed. This is due to the print function for this class. But you could use unclass(pivottable) to achieve regular behavior of print.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
I have the following dataset with daily collected data. R> toydata date group coef
I have following dataset: name1 <- c(P1, P2, IndA, IndB, IndC, IndD, IndE, IndF,
I have following dataset Id Title Group 1 title1 A 2 title2 A 3
I have big dataset (but the following is small one for example). I can
I have a dataset with ~20M rows and I'm observing the following behavior. The
I have the following data set structure: date time_in_hours price Sep 03 08 9.76
Would someone kindly assist me with the following? I have two DataGridView objects that
I have the following script which creates 2 threads as you can see.The thing
i have the following sample data with duplicate information: ID Date Emp_ID Name Keep

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.