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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:41:13+00:00 2026-06-15T14:41:13+00:00

I have the following data frame id datestamp hrofday val1 val2 val3 a 20120401

  • 0

I have the following data frame

id datestamp hrofday val1 val2 val3
a  20120401 0 3.2 0 1
a  20120401 1 3.3 4 0
a  20120401 2 3.4 6 0
...
a  20120401 23 7.3 0 2

It represents a user-id followed by hour of day, val1 val2 & val3.
I want to use cast to put the data frame in the following shape using either reshape or base R. For every id I want the values in val1 in each of the 24 fields and simultaneously find the total for val2 & val3. e.g. shown below

id datestamp val1.0 val1.1 val1.2 ... val1.23 total.val2 total.val3
a  20120401 3.2 3.3 3.4 ... 7.3 10 3
...

Any help much appreciated. Thanks in advance.

  • 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-15T14:41:14+00:00Added an answer on June 15, 2026 at 2:41 pm

    Not precisely what you wanted (slightly different column names):

    vecs <-  by(dfrm, dfrm$id, FUN= function(dfr) 
    return(data.frame(id = dfr$id[1], val1=matrix(dfr$val1, ncol=24), 
                       total.val2=sum(dfr$val2), total.val3=sum(dfr$val3) ) )
             )  
    
    dfvecs <- do.call(rbind, vecs) # Not really needed for single group 
                           # .. but it is the standard way to recombine by() results
    
    > dfvecs
      id val1.1 val1.2 val1.3 val1.4 val1.5 val1.6 val1.7 val1.8 val1.9 val1.10 val1.11
    a  a   6.94   5.45   2.83   9.23   2.92   8.37   2.86   2.67   1.87    2.32    3.17
      val1.12 val1.13 val1.14 val1.15 val1.16 val1.17 val1.18 val1.19 val1.20 val1.21
    a    3.03    1.59     0.4    2.19    8.11    5.26    9.15    8.31    0.46    4.56
      val1.22 val1.23 val1.24 total.val2 total.val3
    a    2.65    3.05    5.07     118.44      99.02
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data frame x <- read.table(text = id1 id2 val1 val2
Suppose I have the following data frame: User.Id Tags 34234 imageUploaded,people.jpg,more,comma,separated,stuff 34234 imageUploaded 12345
I have the following data.frame: sample <- data.frame(day=c(1,2,5,10,12,12,14)) sample.table <- as.data.frame(table(sample$day)) Now what I'd
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
I have the following data test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10)) I would like a barplot with
I have the following data.frames: a <- data.frame(id = 1:3, v1 = c('a', NA,
I have the following data: a=c(1:10) b=c(16:25) c=c(24:33) wa=c(3,7,3,3,3,3,3,3,3,1) wb=c(3,2,3,3,3,3,3,3,3,8) wc=c(4,1,4,4,4,4,4,4,4,1) z=data.frame(a,b,c,wa,wb,wc) I want
I have the following dataframe: DF <- data.frame(x = c(1, 2, 3,NA), y =
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the
I have the following dataframe: sp <- combn(c(sp1,sp2,sp3,sp4),2) d <- data.frame(t(sp),freq=sample(0:100,6)) and two factors

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.