Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Support] Efficient reshaping with melt - Error: measure variables not found in data #2270

Closed
BarkleyBG opened this issue Jul 14, 2017 · 3 comments

Comments

@BarkleyBG
Copy link

BarkleyBG commented Jul 14, 2017

I want to "efficiently reshape" my data using the new list functionality. But I can't even get a simple example to work with the list notation.

I'm starting with a clean environment in an RMarkdown script in a package I'm working on. Note that I'm running R 3.2.3.

library(data.table)
# data.table 1.10.4
 # The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
 # Documentation: ?data.table, example(data.table) and browseVignettes("data.table")
 # Release notes, videos and slides: http://r-datatable.com
#Warning message:
#package ‘data.table’ was built under R version 3.2.5 

toy dataset, and it works fine in the usual case

toy <- data.frame(aa = 3:4, ab= 5:6)
t1 <- melt(data = toy, measure.vars=1:2)
t1
#  variable value
#1       aa     3
#2       aa     4
#3       ab     5
#4       ab     6

But this fails with the list notation

 t2 <- melt(data = toy,measure.vars = list(1:2))
#Error: measure variables not found in data: 1:2
 t2
#Error: object 't2' not found

I'm also having troubles with the patterns. Should I load reshape2 first? I tried that and it didn't seem to work.

 t3 <- melt(data = toy,measure.vars = patterns("^a_"))
#Error in melt_check(data, id.vars, measure.vars, variable.name, value.name) : 
  #could not find function "patterns"
 t3
#Error: object 't3' not found

fwiw

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.10.4

loaded via a namespace (and not attached):
[1] magrittr_1.5   plyr_1.8.4     tools_3.2.3    reshape2_1.4.1 Rcpp_0.12.4    stringi_1.1.2 
[7] stringr_1.2.0 

Thanks for your help!

@MichaelChirico
Copy link
Member

MichaelChirico commented Jul 14, 2017 via email

@BarkleyBG
Copy link
Author

And indeed, what a silly mistake it is

toy <- data.frame(aa = 3:4, ab= 5:6) %>% 
    as.data.table() ### fixes everything, obviously

t2 <- melt(data = toy,measure.vars = list(1:2)) ###now works
t2  
#   variable value
#1:       aa     3
#2:       aa     4
#3:       ab     5
#4:       ab     6

@MichaelChirico
Copy link
Member

MichaelChirico commented Jul 14, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants