Reassign a variable in R

  • Comments posted to this topic are about the item Reassign a variable in R

  • Nice, easy question today, thanks Steve

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • "Without worrying about types". That doesn't terrify me in the slightest...

    I'm just going to run some quick SQL.
    Update Ledger
    Set Debt = 'Pancakes';

    Ahhh, I feel better now. 🙂

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thank you for this question. If you run the code provided in the answer #3:  gc(x); x <- "42 is the best answer"
    then the result is also an re-assign the variable x to the value "42 is the best answer".

    Try run in R this code:
    x <- 42
    x
    cat("The class of variable x is ",class(x),"\n")

    x <- "42 is the best answer"
    x
    cat(" Now the class of variable x becomes ",class(x),"\n")

    cat(" Now call the function gc() for the memory management with print the memory usage statistics and re-assign the variable x","\n")
    gc(x); x <- "42 is the best answer"    # Incorrect argument(x) of the function gc() is ignored.
    x
    cat("  Now the class of variable x is ",class(x),"\n")

    cat("  Now delete the variable x by using the rm() function and re-assign the variable x","\n")
    rm(x); x <- 62
    x
    cat("  Now the class of variable x is chaged to ",class(x),"\n")

    Results:

    Executing the program....
    $Rscript main.r
    [1] 42
    The class of variable x is numeric
    [1] "42 is the best answer"
    Now the class of variable x becomes character
     Now call the function gc() for the memory management with print the memory usage statistics and re-assign the variable x
       used (Mb) gc trigger (Mb) max used (Mb)
    Ncells 84024 4.5  350000 18.7 284784 15.3
    Vcells 172806 1.4  786432 6.0 558411 4.3
    [1] "42 is the best answer"
      Now the class of variable x is character
      Now delete the variable x by using the rm() function and re-assign the variable x
    [1] 62
      Now the class of variable x is chaged to numeric
    Garbage collection 1 = 0+0+1 (level 2) ...
    4.5 Mbytes of cons cells used (24%)
    1.4 Mbytes of vectors used (22%)

  • To me it looked pretty straightforward. Thanks, Steve!

  • George Vobr - Wednesday, April 19, 2017 10:00 AM

    Thank you for this question. May be the irony that if you run the code gc(x); x <- "42 is the best answer" it calls the function gc() for the automatic memory management with print the memory usage statistics.
    The Argument (x) is ignored. The result is a re-assing the variable x to the value "42 is the best answer".

    I like this option the best. No Really!

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • George Vobr - Wednesday, April 19, 2017 10:00 AM

    Thank you for this question. If you run the code provided in the answer #3:  gc(x); x <- "42 is the best answer"
    then the result is also an re-assign the variable x to the value "42 is the best answer".

    Try run in R this code:
    x <- 42
    x
    cat("The class of variable x is ",class(x),"\n")

    x <- "42 is the best answer"
    x
    cat(" Now the class of variable x becomes ",class(x),"\n")

    cat(" Now call the function gc() for the memory management with print the memory usage statistics and re-assign the variable x","\n")
    gc(x); x <- "42 is the best answer"    # Incorrect argument(x) of the function gc() is ignored.
    x
    cat("  Now the class of variable x is ",class(x),"\n")

    cat("  Now delete the variable x by using the rm() function and re-assign the variable x","\n")
    rm(x); x <- 62
    x
    cat("  Now the class of variable x is chaged to ",class(x),"\n")

    Results:

    Executing the program....
    $Rscript main.r
    [1] 42
    The class of variable x is numeric
    [1] "42 is the best answer"
    Now the class of variable x becomes character
     Now call the function gc() for the memory management with print the memory usage statistics and re-assign the variable x
       used (Mb) gc trigger (Mb) max used (Mb)
    Ncells 84024 4.5  350000 18.7 284784 15.3
    Vcells 172806 1.4  786432 6.0 558411 4.3
    [1] "42 is the best answer"
      Now the class of variable x is character
      Now delete the variable x by using the rm() function and re-assign the variable x
    [1] 62
      Now the class of variable x is chaged to numeric
    Garbage collection 1 = 0+0+1 (level 2) ...
    4.5 Mbytes of cons cells used (24%)
    1.4 Mbytes of vectors used (22%)

    Please be careful about disclosing the correct answer. Your post is linked on the home page and people who did not dare to try to answer the QotD can read it. Not that it means too much, but I am just trying to make the field level.

  • Revenant - Wednesday, April 19, 2017 5:08 PM

    Please be careful about disclosing the correct answer. Your post is linked on the home page and people who did not dare to try to answer the QotD can read it. Not that it means too much, but I am just trying to make the field level.

    O. K., thanks for the alert. Somehow, I started it up too fast... 😉

  • George Vobr - Wednesday, April 19, 2017 5:45 PM

    Revenant - Wednesday, April 19, 2017 5:08 PM

    Please be careful about disclosing the correct answer. Your post is linked on the home page and people who did not dare to try to answer the QotD can read it. Not that it means too much, but I am just trying to make the field level.

    O. K., thanks for the alert. Somehow, I started it up too fast... 😉

    Live long and prosper. 😉

  • R becoming easy for me...thanks Steve.

    “When I hear somebody sigh, ‘Life is hard,’ I am always tempted to ask, ‘Compared to what?’” - Sydney Harris

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply