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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:53:37+00:00 2026-06-16T20:53:37+00:00

Can someone explain what’s happening in this example code? I have a function which

  • 0

Can someone explain what’s happening in this example code? I have a function which does a calculation loop and as always, wanted to initialize my output vector instead of incrementing it each time thru the loop.

Rgames> library(Rmpfr)
Rgames> foo<-rep(NA,5)
Rgames> foo
[1] NA NA NA NA NA
Rgames> rfoo<-mpfr(rep(NA,5),20)
Rgames> rfoo
5 'mpfr' numbers of precision  20   bits 
[1] NaN NaN NaN NaN NaN
Rgames> for(jj in 1:5) {
+ foo[jj]<- mpfr(jj,10)
+ rfoo[jj]<-mpfr(jj,10)
+ }
Rgames> rfoo
5 'mpfr' numbers of precision  10   bits 
[1] 1 2 3 4 5
Rgames> foo
[[1]]
'mpfr1' 1

[[2]]
'mpfr1' 2

[[3]]
'mpfr1' 3

[[4]]
'mpfr1' 4

[[5]]
'mpfr1' 5

I don’t understand why, apparently, the existing non-mpfr vector foo is not only coerced to a list, but then each time through the loop, the new value is inserted into foo[jj] as a list, giving me an unpleasant “list of lists” . The mpfr vector rfoo does what I expected I’d get in both cases. (I checked, and if I do not initialize, and put something inside the loop like foo<-c(foo,mpfr(jj,10)) I do get a result equivalent to rfoo)

  • 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-16T20:53:38+00:00Added an answer on June 16, 2026 at 8:53 pm

    What’s happening here is the same thing that would happen if you were working with lists instead of mpfr objects. For example, as follows. I believe this makes sense because S4 objects are stored in a similar way to lists, but I’m not an S4 expert.

    > foo <- rep(NA,2)
    > foo
    [1] NA NA
    > foo[1] <- list(1)
    > foo
    [[1]]
    [1] 1
    
    [[2]]
    [1] NA
    

    I believe that what happens is that the original atomic vector gets coerced to a list to be able to include the object that you’ve asked to put there. I can’t find any documentation about that right here; I think it’s discussed in Chambers’s book but don’t have that at hand.

    One can easily recreate this behavior using S3 methods as well; first the S3 methods to create a new class:

    mynum <- function(x) {structure(as.list(x), class="mynum")}
    print.mynum <- function(x) { cat("My numbers are:\n")
                                 print(do.call(paste, x), quote=FALSE) }
    

    Here’s what happens if you start with an atomic vector:

    > (foo <- rep(NA, 2))
    [1] NA NA
    > foo[1] <- mynum(1)
    > foo
    [[1]]
    [1] 1
    
    [[2]]
    [1] NA
    

    and here’s what happens if you start with the mynum vector:

    > (rfoo <- mynum(rep(NA, 2)))
    My numbers are:
    [1] NA NA
    > rfoo[1] <- mynum(1)
    > rfoo
    My numbers are:
    [1] 1 NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain why, in this example , the function is not firing? I've
Can someone explain why the HashMap acts like it does in this example: Simple
Can someone explain why this is occurring? The code below was executed in the
Can someone explain to me why this code fails once in a while with
Can someone explain to me how to use this function? Tempo closes registration after
Can someone explain this strange result for me? Got the following code in linqPad
Can someone explain me why this code compiles and runs fine, despite the fact
Can someone explain me when Windows shows this message? What do i have to
Can someone explain to me why this is always always getting only one color
Can someone explain to me what this does? #define ROUNDUP(n,width) (((n) + (width) -

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.