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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:33:53+00:00 2026-06-06T04:33:53+00:00

I have a function (bobB) that seems to be getting stuck in a while

  • 0

I have a function (bobB) that seems to be getting stuck in a while loop. When I hit escape and look at warnings() I get the following error:

Warning message:
In min(xf, yf) : no non-missing arguments to min; returning Inf  

Some example code:

#I have the data: 

x<-"A03" 
y<-"A24"

sitex<-c("Sp1","Sp1","Sp3","Sp3")
sitey<-c("Sp2","Sp4","Sp2","Sp4")
gsim<-c(0.2,0.3,0.4,0.1)
gsim<-data.frame(sitex,sitey,gsim)

site<-c("A03","A03","A03","A03","A24","A24","A24","A24")
species<-c("Sp1","Sp1","Sp3","Sp4","Sp1","Sp1","Sp3","Sp4")
freq<-c(0.2,0.3,0.4,0.1,0.3,0.3,0,0.4)
ssf<-data.frame(site,species,freq,stringsAsFactors=FALSE)

#My function:  

bobB <- function (x, y, ssf, gsim) {

#*Step 1.* Create an empty matrix 'specfreq' to fill 

#Selects the species frequency data greater than 0 for the two sites being compared  

ssfx <- ssf[ssf$site == x & ssf$freq >0,]
ssfy <- ssf[ssf$site == y & ssf$freq >0,]

#pull out the species that are present at site x and/or site y using a merge,  
#this is    needed to create the initial empty matrix  

m<-(merge(ssfx,ssfy,all=TRUE))
species<-unique(m$species)

#Creates an empty matrix of the frequency of each species at site x and y  

 specfreq <- matrix(0, length(species), 2, dimnames=list(species,c(x,y)))

#*Step 2.* Fill empty matrix 'specfreq' with data from data.frame 'ssf'  

for(i in 1:nrow(ssf{specfreq[rownames(specfreq)==ssf[i,"species"],colnames(specfreq)==ssf[i,"site"]] <- ssf[i,"freq"]}

#*Step 3.* For species present at site x and y remove the minimum of the two from both  
#find minimum frequency for each species for site x and y  

a <- pmin(specfreq[,x], specfreq[,y])

#subtract 'a' from current 'specfreq'  

specfreq <- specfreq - a

#*Step 4.* Calulate variable B  

#Set answer to 0

answer <- 0

#while 'specfreq' contains data (i.e. is >0) keep doing the following  

while(sum(specfreq) > 1e-10) {

#Find species remaining at site x  

sx <- species[specfreq[,1] > 0]

#Find species remaining at site y  

sy <- species[specfreq[,2] > 0]     

#Pull out the gsim value for sx and sy

gsimre <-gsim[gsim$sitex %in% sx & gsim$sitey %in% sy,]

#determines the row containing the maximum value for remaining gsim and assigns it to i     

i <- which.max(gsimre$gsim)

#once the max gsim value has been found (i) we go back to the specfreq matrix and filter  
#out the frequency of the site x species associated with i   

xf <- specfreq[(gsimre$sitex[i]),x]

#and the frequency of the site y species associated with i  

yf <- specfreq[(gsimre$sitey[i]),y]

#The frequency of the species at site x associated with the greatest i is multiplied by i           

answer <- answer + xf * gsimre$gsim[i]

#Then take the minimum common frequency for the two species associated with i  

f <- min(xf,yf)

#Subtract theminimum  common frequency from both the site x and site y column  

specfreq[gsimre$sitex[i],x] <- specfreq[gsimre$sitex[i],x]-f
specfreq[gsimre$sitey[i],y] <- specfreq[gsimre$sitey[i],y]-f   
}
answer
}
bobB(x, y, ssf, gsim)
  • 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-06T04:33:54+00:00Added an answer on June 6, 2026 at 4:33 am

    Ok so here is your problem (I think):

    xf <- specfreq[(gsimre$sitex[i]),x]
    yf <- specfreq[(gsimre$sitey[i]),y]
    

    (gsimre$sitex[i])and (gsimre$sitey[i])are factors which means that when you use them to index something there are interpreted as numeric and not as characters.
    And indeed:

    gsimre$sitey
    [1] Sp4
    Levels: Sp2 Sp4  
    

    Sp4, being the second factor, has a numerical value of 2, hence specfreq[(gsimre$sitey[i]),y] is specfreq[2,2] which is 0.

    Hence, this should do the trick:

    xf <- specfreq[as.character(gsimre$sitex[i]),x]
    yf <- specfreq[as.character(gsimre$sitey[i]),y]
    

    Or you declare them as being strings and not factors at the beginning, as you did for ssf but not for gsim:

    gsim<-data.frame(sitex,sitey,gsim, stringsAsFactors=FALSE)
    

    Because of this mistake yf was equal to 0 and therefore f=min(xf,yf)was always equal to 0, hence your endless loop.

    P. S. : answer should be out of the while loop if you want the function to return it, i. e.

        }
    answer
    }
    

    instead of

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

Sidebar

Related Questions

I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick
So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have function that have been colled in stored procedure. The function returns a
i have function that changes background color of element and src of image. in
I have function that scrolls to a anchor position in a list function snapToAnchor(anchor)
I have function getMemory() which returns VARIANT (mfc). It is said that in ulVal
I have function that has : ob_start(); //Include of some files $content = ob_get_contents();
I have function in my database class that returns the id of the people
I have function that returns the index of a character GetCharFromPos(Pt: TPoint): Integer; now

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.