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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:03:25+00:00 2026-06-16T03:03:25+00:00

I have the following function: s_c <- function(n, t){ r_num <- runif(1,min=0,max=1) use <-

  • 0

I have the following function:

s_c <- function(n, t){
    r_num <- runif(1,min=0,max=1)
    use <- sample(s[,1],1)
    use2 <- subset(s,s[,1]==use,2)
    use2 <- as.numeric(use2)
    ne_s <- sample(subset(s,s[,2]!=use2,2),1)

    Return(use) 
    if (t>50 & r_num<0.5){
        ne_s
    }
        else 
           0  
}

I would actually like to use the variable created in the function in a command outside the function, so I would like to “return” in the sense of being able to refer to the variable outside the function

Question 2:

What if I would like to do an assignment within the if statement, for example

 if (t>50 & r_num<0.5){
        s[,4]=use
    }

Can this be done?

  • 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-16T03:03:26+00:00Added an answer on June 16, 2026 at 3:03 am

    Although you should probably return the two values as a list, you can affect the parent’s environment quite easily.

    Replace this:

    Return(use)
    

    with this:

    use <<- use
    

    For your edit, you could use the syntax:

    s[,4] <<- use
    

    Note: The “parent’s environment” is NOT the environment in which the function is called. It is the environment in which the function is defined. It is for this reason that a function in R is known as a closure. Explicitly:

    s_c <- function() {
      use <<- 1
    }
    
    f <- function() {
      use <- 0
      s_c()
      return(use)
    }
    

    f() will return 0. s_c() is modifying a different use variable (in a different environment).

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

Sidebar

Related Questions

i have to use the following function, to change the input's type <input class=input
I have the following function to return a clean path for a script. function
I have the following Objective-c Function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger
I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the
I have a C function with the following signature: int my_function(int n, struct player
I have written the following function but it's isn't returning anything when I run
Let's say we have the following function: foo <- function(x) { line1 <- x
I have the following JQuery function about a range slider . It's just working
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
I want to use the following function (in PostgreSQL): create type holder as (userName

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.