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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:52:59+00:00 2026-06-15T03:52:59+00:00

I am working with a dataset that comes with lme4, and am trying to

  • 0

I am working with a dataset that comes with lme4, and am trying to learn how to apply reshape2 to convert it from long to wide [full code at the end of the post].

library(lme4)
data("VerbAgg")  # load the dataset

The dataset has 9 variables; ‘Anger’, ‘Gender’, and ‘id’ don’t vary with ‘item’, while ‘resp’,
‘btype’, ‘situ’, ‘mode’, and ‘r2’ do.

I have successfully been able to convert the dataset from long to wide format using reshape():

wide <- reshape(VerbAgg, timevar=c("item"), 
             idvar=c("id", 'Gender', 'Anger'), dir="wide")

Which yields 316 observations on 123 variables, and appears to be correctly transformed. However, I have had no success using reshape/reshape2 to reproduce the wide dataframe.

wide2 <- recast(VerbAgg, id + Gender + Anger ~ item + variable)
Using Gender, item, resp, id, btype, situ, mode, r2 as id variables
Error: Casting formula contains variables not found in molten data: Anger

I may not be 100% clear on how recast defines id variables, but I am very confused why it does not see “Anger”. Similarly,

wide3 <- recast(VerbAgg, id + Gender + Anger ~ item + variable, 
               id.var = c("id", "Gender", "Anger"))
Error: Casting formula contains variables not found in molten data: item

Can anyone see what I am doing wrong? I would love to obtain a better understanding of melt/cast!

Full code:

## load the lme4 package 
library(lme4) 
data("VerbAgg")
head(VerbAgg)
names(VerbAgg) 

# Using base reshape()
wide <- reshape(VerbAgg, timevar=c("item"), 
                 idvar=c("id", 'Gender', 'Anger'), dir="wide")

# Using recast
library(reshape2)
wide2 <- recast(VerbAgg, id + Gender + Anger ~ item + variable)
wide3 <- recast(VerbAgg, id + Gender + Anger ~ item + variable, 
                id.var = c("id", "Gender", "Anger"))

# Using melt/cast
m <- melt(VerbAgg, id=c("id", "Gender", "Anger"))
wide <- o cast(m,id+Gender+Anger~...)
Aggregation requires fun.aggregate: length used as default
# Yields a list object with a length of 8? 

m <- melt(VerbAgg, id=c("id", "Gender", "Anger"), measure.vars = c(4,6,7,8,9))
wide <- dcast(m, id ~ variable)
# Yields a data frame object with 6 variables.
  • 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-15T03:53:00+00:00Added an answer on June 15, 2026 at 3:53 am

    I think the following code does what you want.

    library(lme4) 
    data("VerbAgg")
    
    # Using base reshape()
    wide <- reshape(VerbAgg, timevar=c("item"), 
                     idvar=c("id", 'Gender', 'Anger'), dir="wide")
    dim(wide) # 316 123
    
    # Using melt/cast
    require(reshape2)
    m1 <- melt(VerbAgg, id=c("id", "Gender", "Anger","item"), measure=c('resp','btype','situ','mode','r2'))
    wide4 <- dcast(m1,id+Gender+Anger~item+variable)
    dim(wide4) # 316 123
    
    R> wide[1:5,1:6]
      Anger Gender id resp.S1WantCurse btype.S1WantCurse situ.S1WantCurse
    1    20      M  1               no             curse            other
    2    11      M  2               no             curse            other
    3    17      F  3          perhaps             curse            other
    4    21      F  4          perhaps             curse            other
    5    17      F  5          perhaps             curse            other
    
    R> wide4[1:5,1:6]
      id Gender Anger S1WantCurse_resp S1WantCurse_btype S1WantCurse_situ
    1  1      M    20               no             curse            other
    2  2      M    11               no             curse            other
    3  3      F    17          perhaps             curse            other
    4  4      F    21          perhaps             curse            other
    5  5      F    17          perhaps             curse            other
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a dataset in R that comes with a codebook, which basically
I'm working on a dataset that consists of ~10^6 values which clustered into a
I am working on a web application that is consuming a Dataset returned by
I am currently working with an SSRS 2008 report that returns a dataset similar
I am working with a hourly dataset for 1 year and trying to calculate
I'm working with a set of legacy DAO code that returns an IList, where
I'm working on importing a dataset that looks like: Quantity 100-500 Processes litho, plain
I'm working with a large dataset of products(~1 million). These products come from many
I am working on an application that works like this: It fetches data from
I'm working with a dataset that's connected to an sql server and what I'm

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.