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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:39:55+00:00 2026-06-01T06:39:55+00:00

I am working with a hourly dataset for 1 year and trying to calculate

  • 0

I am working with a hourly dataset for 1 year and trying to calculate sum of weekday (Mon – Fri) and weekend (sat, sun) values and add to a new row at the bottom of the data set. my data frame looks like this:

enter image description here

I have created the sum for all week with the following command:

## calculate column sum
df[366,(3:27)] <- colSums(df[,3:27], na.rm = T)

Then calculated the number of missing values in each column with this:

## calculate number of missing values in a column

NA.find <- function(x) length(which(is.na(x))) #function for finding missing values
myNumCols <- which(unlist(lapply(df, is.numeric))) 
df[(nrow(df) + 1), myNumCols] <- sapply(df[, myNumCols],NA.find )

Now I am trying to calculate the weekday and weekend sum between (Column v1:total) and also number of missing values and add the the bottom rows like I did for the all days.

I know that I need to somehow define my date so that R can read it and then use some how to subset weekday and weekend, I figure out how to subset individual like this:

e = colsplit(df1$date,split=" ",names=c("day1","day2","month"))

df2 = cbind(df1[,c("type","date")],day=e[1],cdate=e[2],month=e[3],df1[,3:ncol(df1)])
df3 = subset(df2, day1 == "Mon")

But I am not sure how can subset it say Mon:Friday and again Saturday:Sunday?

What I am trying to achieve is to create 4 new rows from (368:371) and add weekday total, weekday number of missing values, weekend total, weekend number of missing values.

Many thanks,
Ayan

  • 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-01T06:39:56+00:00Added an answer on June 1, 2026 at 6:39 am

    A few comments.

    1. Don’t add column sums to your data frame. For example, in your code, you have:

      df[366,(3:27)] <- colSums(df[,3:27], na.rm = T)
      

      Suppose we subset your data frame, to every weekend. You column sums are wrong. Store them as a separate vector:

      year_sums =colSums(df[,3:27], na.rm = TRUE)
      
    2. Since you no longer have column sums in your data frame, it’s easier to query. So to determine the number of missing values, do something like:

      colSums(is.na(dd[,3:27])
      

      R treats TRUE as 1 and FALSE as 0.

    3. Now to answer your question. The clever way is to use R’s date object or use the lubridate package. However, I would suggest you start with something a bit easier. Just create a new column and subset. In your example, the first day of the week is Friday. So

      ##M for mid week
      ##W for week end 
      df$type = c("M", "W", "W", "M", "M", "M", "M")
      

      creates a column identifying if the data is weekend or mid-week. So to subset, just:

      df[df$type == "M",]
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in PHP/MySQL. I have a table in my database called hourly
I have a report running on hourly basis checking for new orders. It's working
I am working on a project where I return back the values of a
Working with hibernate and spring social, I am trying to query the database by
Working on a small game using an HTML5 canvas, and javascript. And it's working
Working with an undisclosed API, I found a function that can set the number
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working with H2 I get this error when I try to write a row
Working on the problems on Project Euler to try to learn Clojure. I'm on
Working with MS Access for the first time and coming across a few problems

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.