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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:53:46+00:00 2026-06-13T07:53:46+00:00

Why does reshape2 melt return value = NA for me? It works for me

  • 0

Why does reshape2 melt return value = NA for me?

It works for me with reshape, but not with reshape2:

Here is an example datafile:

"","station_id","year","month","day","h1","h2","h3","h4","h5","h6","h7","h8","h9","h10","h11","h12","h13","h14","h15","h16","h17","h18","h19","h20","h21","h22","h23","h24"
"1",1,2004,1,1,46,46,45,41,39,35,33,33,36,47,53,54,55,55,55,55,52,46,40,40,39,38,40,41
"2",1,2004,1,2,43,44,46,46,47,47,47,47,47,47,47,49,52,56,54,56,57,53,50,47,46,45,45,45
"3",1,2004,1,3,45,46,46,44,43,46,46,47,51,55,56,59,65,68,69,68,68,65,64,63,62,63,63,62
"4",1,2004,1,4,63,62,62,62,60,60,60,62,60,64,64,66,71,70,71,72,71,68,67,67,65,64,65,64
"5",1,2004,1,5,64,63,65,64,64,64,64,64,65,66,66,67,68,68,66,66,66,66,63,54,52,49,47,47
"6",1,2004,1,6,47,46,45,43,41,41,39,39,40,43,45,44,45,46,46,46,45,39,39,39,38,36,32,32

Let’s say it is saved as /tmp/foo.csv, then:

Using reshape:

$ R
...
Type 'q()' to quit R.

> library("reshape")
Loading required package: plyr

Attaching package: ‘reshape’

The following object(s) are masked from ‘package:plyr’:

    rename, round_any

> hlist <- NULL; for(z in 1:24) { hlist <- cbind(hlist, sprintf("h%d",z)) }
> 
> thh <- read.csv('/tmp/foo.csv')
> thm <- melt(thh,measure.vars=hlist,variable="hour")
> head(thm)
  station_id year month day hour value
1          1 2004     1   1   h1    46
2          1 2004     1   2   h1    43
3          1 2004     1   3   h1    45
4          1 2004     1   4   h1    63
5          1 2004     1   5   h1    64
6          1 2004     1   6   h1    47
> q()

Using reshape2:

$ R
...
Type 'q()' to quit R.

> library("reshape2")
> hlist <- NULL; for(z in 1:24) { hlist <- cbind(hlist, sprintf("h%d",z)) }
> 
> thh <- read.csv('/tmp/foo.csv')
> thm <- melt(thh,measure.vars=hlist,variable="hour")
> head(thm)
  station_id year month day hour value
1          1 2004     1   1   h1    NA
2          1 2004     1   2   h1    NA
3          1 2004     1   3   h1    NA
4          1 2004     1   4   h1    NA
5          1 2004     1   5   h1    NA
6          1 2004     1   6   h1    NA
> q()

You can see that using library("reshape"), the value column has numbers, but for libary("reshape2"), it has NA, for the same data.

  • 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-13T07:53:47+00:00Added an answer on June 13, 2026 at 7:53 am

    There are better ways to do what you are trying to do.

    All of the following would work with melt() from reshape2:

    # Not using hlist
    melt(th, measure.vars=5:ncol(th), variable="hour")
    melt(th, id.vars=1:4, variable="hour")
    
    # Using your hlist
    hlist <- NULL; for(z in 1:24) { hlist <- cbind(hlist, sprintf("h%d",z)) }
    melt(th, measure.vars=as.vector(hlist), variable="hour")
    
    # Using an alternative hlist
    hlist <- paste0("h", 1:24)
    melt(th, measure.vars=hlist, variable="hour")
    

    It seems like melt() from “reshape” accepts a matrix as an input for measure.vars, but melt() from “reshape2” does not (which I find more reasonable).

    Update: Example reproducible problem

    FYI, below is a start-to-finish way that you can share this problem in a way that is convenient for other Stack Overflow users to copy and paste:

    # Use set.seed when you want to use random numbers 
    #   but want others to have the same data as you.
    set.seed(1) 
    
    # Make up some data that mimics your actual dataset
    # Does not have to be your exact dataset
    th <- cbind(
      data.frame(station = rep(LETTERS[1:3], each = 3),
                 year = 2004, month = rep(1:3, times = 3)), 
      setNames(data.frame(
        matrix(sample(100, 45, replace = TRUE), nrow = 9)),
               paste0("h", 1:5)))
    
    hlist <- NULL; for(z in 1:5) { hlist <- cbind(hlist, sprintf("h%d",z)) }
    # Cleanup any unnecessary stuff that your code leaves behind in the workspace
    rm(z) 
    

    Now, demonstrate your problem. You can use detach(package:package_name) instead of having to quit and restart R.

    library(reshape)
    head(melt(th, measure.vars = hlist, variable = "hour"))
    #   station year month hour value
    # 1       A 2004     1   h1    27
    # 2       A 2004     2   h1    38
    # 3       A 2004     3   h1    58
    # 4       B 2004     1   h1    91
    # 5       B 2004     2   h1    21
    # 6       B 2004     3   h1    90
    detach(package:reshape)
    
    library(reshape2)
    head(melt(th, measure.vars = hlist, variable = "hour"))
    #   station year month hour value
    # 1       A 2004     1   h1  <NA>
    # 2       A 2004     2   h1  <NA>
    # 3       A 2004     3   h1  <NA>
    # 4       B 2004     1   h1  <NA>
    # 5       B 2004     2   h1  <NA>
    # 6       B 2004     3   h1  <NA>
    detach(package:reshape2)
    

    Hope this helps!

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

Sidebar

Related Questions

Does anyone know why UsernameExists wont return True. I must have my syntax messed
I am manipulating a data frame using the reshape package. When using the melt
OK, I think this will be fairly simple, but my numpy-fu is not quite
I am attempting to understand why development had shifted from reshape to reshape2 package.
reshape function should change the shape of a matrix. But if I try using
Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator
I'm trying to run an example GLUT program, but it only creates a white
Does anyone know if there is a way to generate different code in the
Does COUNT(*) have any significant impact for MySQL performance if query already has GROUP
Does anyone have a translate function for x/y positions after rotation in javascript? for

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.