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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:58:29+00:00 2026-05-16T22:58:29+00:00

I tried to use acast from reshape2 within a self written function, but had

  • 0

I tried to use acast from reshape2 within a self written function, but had the problem that acast did not find the data I send to it.

Here is my data:

library("reshape2")
x <- data.frame(1:3, rnorm(3), rnorm(3), rnorm(3))    
colnames(x) <- c("id", "var1", "var2", "var3")
y <-melt(x, id = "id", measure = c("var1", "var2", "var3"))

y then looks like this:

  id variable      value
1  1     var1  0.1560812
2  2     var1  1.0343844
3  3     var1 -1.4157728
4  1     var2  0.8808935
5  2     var2  0.1719239
6  3     var2  0.6723758
7  1     var3 -0.7589631
8  2     var3  1.1325995
9  3     var3 -1.5744876

now I can cast it back via acast:

> acast(y,y[,1] ~ y[,2])
        var1      var2       var3
1  0.1560812 0.8808935 -0.7589631
2  1.0343844 0.1719239  1.1325995
3 -1.4157728 0.6723758 -1.5744876

However, when writing a small wrapper for acast that should do the same, i get a stupid error messages:

wrap.acast <- function(dat, v1 = 1, v2 = 2) {
    out <- acast(dat, dat[,v1] ~ dat[,v2])
    return(out)
}

wrap.acast(y)

Error in eval(expr, envir, enclos) : object 'dat' not found

The problem is obviously related to something like environments and global/local variables. As it gives other error messages after declaring dat in the global environment (i.e., v1 and v2 not found as long as they aren’t global).

I would like to use resahpe (especially acast) within a function without the necessity of declaring the variables outside the function. What is the trick?

Thanks.

  • 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-05-16T22:58:30+00:00Added an answer on May 16, 2026 at 10:58 pm

    Instead of using the formula specification, use the character specification:

    acast(y, list(names(y)[1], names(y)[2]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to use an image file in my HTML template. But when I
I tried to use object expression to extend the IDelegateEvent, but in fsi there
I tried to use $.cssRule() plugin and found that it makes it inconvenient to
I tried to use this code but it doesn't work. http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html Ld /Users/waitonza/Library/Developer/Xcode/DerivedData/Dr_Ngoo-aanknxmuodcgjicaigxevljxokeq/Build/Products/Debug-iphonesimulator/Dr Ngoo.app/Dr
I tried to use __declspec(dllexport) to export functions in my DLL, but it doesn't
I have tried: use information_schema update referential_constraints set delete_rule='cascade'; but got the following error:
I tried use file_put_contents output new page. but I meet some trouble in breaking
I tried to use DriveInfo.IsReady, but it returns false if an unformatted floppy is
I tried use javascript spilt to get part of the word : new from
I tried use js to change my url from http://localhost/text/index.php#mainpage%20menu1 to http://localhost/text/index.php#homepage%20menu1 My code

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.