+ Reply to Thread
Results 1 to 9 of 9

Thread: R cannot find an object within a loop

  1. #1
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Downloads
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

    R cannot find an object within a loop




    My R programming skill is quite basic I know, so be gentle!
    I am having difficulties in understanding what I am doing wrong here. Upon running the function, R reports an error saying that it cannot find the object 'd'.
    actually, the error is the following: Error in d[i, ] <<- rbind(res$eig[, 1]) : oggetto "d" non trovato. The last fit is in Italian, saying that the object "d" is not found.

    The aim of the function is to calculate the Correspondence Analysis' eigenvalues for the three table's dimensions. This is done once on the input contingency table, and B times for randomized tables. 'd' is the object (actually, a dataframe) in which the eigenvalues are stored.

    Below is the code for a small dataset, and for the function I have been working with. The function requires the 'FactoMineR' package.

    Code: 
    mydata <- structure(list(none = c(4, 4, 25, 18, 10), light = c(2, 3, 10, 
    24, 6), medium = c(3, 7, 12, 33, 7), heavy = c(2, 4, 4, 13, 2
    )), .Names = c("none", "light", "medium", "heavy"), row.names = c("SM", 
    "JM", "SE", "JE", "SC"), class = "data.frame")
    Code: 
    test <- function(data,B=1000){
      options(scipen = 999)
      nIter <- B+1
    
      numb.dim.cols <- ncol(data) - 1
    
      numb.dim.rows <- nrow(data) - 1
    
      table_dim <- min(numb.dim.cols, numb.dim.rows) # dimensionality of the table
    
      d <- as.data.frame(matrix(nrow=nIter, ncol=table_dim)) # object where eigenvalues are stored
    
      res <- CA(data, graph=FALSE) # perform CA on the original table
    
      d[1,]<- rbind(res$eig[,1]) # save the 3 eigenvalues in the first row of 'd'
    
      for (i in 2:nIter){
        rand_table <- as.data.frame(r2dtable(1, apply(data, 1,sum), apply(data, 2, sum)))  # create a randomized table
    
        res <- CA(rand_table, graph=FALSE) # perform CA on the random table
    
        d[i,] <<- rbind(res$eig[,1]) # this SHOULD store the new eigenvalues into subsequent rows of the 'd' object
    
      }
    }
    http://cainarchaeology.weebly.com/

  2. #2
    Devorador de queso
    Points: 97,518, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,987
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: R cannot find an object within a loop

    Where is CA coming from?
    I don't have emotions and sometimes that makes me very sad.

  3. #3
    Devorador de queso
    Points: 97,518, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,987
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: R cannot find an object within a loop

    But you might just need to change
    Code: 
     d[i,] <<- rbind(res$eig[,1])
    to
    Code: 
     d[i,] <- rbind(res$eig[,1])
    I don't have emotions and sometimes that makes me very sad.

  4. #4
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Downloads
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

    Re: R cannot find an object within a loop

    Thank you Dason. CA is a command out from the FactoMineR Package.
    Now, your suggestion worked. What if I want to make 'd' available outside the function?

    Cheers
    http://cainarchaeology.weebly.com/

  5. #5
    Devorador de queso
    Points: 97,518, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,987
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: R cannot find an object within a loop

    return d from the function then. You shouldn't be using global assigns to return values from a function.
    I don't have emotions and sometimes that makes me very sad.

  6. The Following User Says Thank You to Dason For This Useful Post:

    trinker (08-22-2017)

  7. #6
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Downloads
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

    Re: R cannot find an object within a loop

    Dason, what do you mean by "return d from the function"?
    Thanks
    http://cainarchaeology.weebly.com/

  8. #7
    Devorador de queso
    Points: 97,518, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,987
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: R cannot find an object within a loop

    Code: 
    test <- function(data,B=1000){
      options(scipen = 999)
      nIter <- B+1
    
      numb.dim.cols <- ncol(data) - 1
    
      numb.dim.rows <- nrow(data) - 1
    
      table_dim <- min(numb.dim.cols, numb.dim.rows) # dimensionality of the table
    
      d <- as.data.frame(matrix(nrow=nIter, ncol=table_dim)) # object where eigenvalues are stored
    
      res <- CA(data, graph=FALSE) # perform CA on the original table
    
      d[1,]<- rbind(res$eig[,1]) # save the 3 eigenvalues in the first row of 'd'
    
      for (i in 2:nIter){
        rand_table <- as.data.frame(r2dtable(1, apply(data, 1,sum), apply(data, 2, sum)))  # create a randomized table
    
        res <- CA(rand_table, graph=FALSE) # perform CA on the random table
    
        d[i,] <- rbind(res$eig[,1]) # this SHOULD store the new eigenvalues into subsequent rows of the 'd' object
    
      }
    
      return(d)
    }
    Then to use it you would use...
    Code: 
    d <- test(mydata)
    So test returns 'd' when it's done and then when you call the function you just need to store the result wherever you want.
    I don't have emotions and sometimes that makes me very sad.

  9. The Following User Says Thank You to Dason For This Useful Post:

    gianmarco (08-23-2017)

  10. #8
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Downloads
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

    Re: R cannot find an object within a loop

    How stupid of me!!!!!!
    Thank you
    http://cainarchaeology.weebly.com/

  11. #9
    Cookie Scientist
    Points: 13,806, Level: 76
    Level completed: 39%, Points required for next Level: 244
    Jake's Avatar
    Location
    Austin, TX
    Posts
    1,297
    Thanks
    66
    Thanked 584 Times in 438 Posts

    Re: R cannot find an object within a loop


    Stay functional, TS.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats