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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error in do_one(nmeth) : na/nan/inf in foreign function call (arg 1) - Simple way to solve it
Next
Answered
Agathe Couturier
  • 14
Agathe Couturier
Asked: May 12, 20222022-05-12T07:36:19+00:00 2022-05-12T07:36:19+00:00In: r

Error in do_one(nmeth) : na/nan/inf in foreign function call (arg 1) – Simple way to solve it

  • 14

. Advertisement .

..3..

. Advertisement .

..4..

I have the following r code, but I do not know how to find the correct result. Why has this problem occurred, and how can it be solved? This is the code I am running:

#create data frame df 

<- data.frame(var1=c(2, 4, 4, 6, 7, 8, 8, 9, 9, 12), 
              
              var2=c(12, 14, 14, 8, 8, 15, 16, 9, 9, 11), 
              
              var3=c(22, NA, 23, 24, 28, 23, 19, 16, 12, 15)) 


row.names(df) <- LETTERS[1:10] 


#view data frame 

df 


 
 var1 var2 var3 

A   2   12   22 

B   4   14   NA 

C   4   14   23 

D   6    8   24 

E   7    8   28 

F   8   15   23 

G   8   16   19 

H   9    9   16 

I   9    9   12 

J  12   11   15

This is the error text I receive:

#attempt to perform k-means clustering with k = 3 clusters
km <- kmeans(df, centers = 3)

Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

machine-learning
  • 2 2 Answers
  • 218 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-05-29T14:42:14+00:00Added an answer on May 29, 2022 at 2:42 pm

    The cause: The reason is that when you attempt to perform k-means clustering in R but the data frame you’re using misses one or more values.

    Solution: The simpliest way to fix this error is to use the na.omit() function to remove rows with missing values from the data frame:

    #remove rows with NA values
    df <- na.omit(df)
    
    #perform k-means clustering with k = 3 clusters
    km <- kmeans(df, centers = 3)
    
    #view results
    km
    
    K-means clustering with 3 clusters of sizes 4, 3, 2
    
    Cluster means:
      var1      var2     var3
    1  5.5 14.250000 21.75000
    2 10.0  9.666667 14.33333
    3  6.5  8.000000 26.00000
    
    Clustering vector:
    A C D E F G H I J 
    1 1 3 3 1 1 2 2 2 
    
    Within cluster sum of squares by cluster:
    [1] 46.50000 17.33333  8.50000
     (between_SS / total_SS =  79.5 %)
    
    Available components:
    
    [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
    [6] "betweenss"    "size"         "iter"         "ifault"  
    • 18
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Inès Raymond
    2022-05-25T19:31:29+00:00Added an answer on May 25, 2022 at 7:31 pm

    Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

    This error can also be caused by non-numerical values in the table.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.