Fix randomForest.default(m, y, …) : Na/NaN/Inf in foreign function call

This error occurs when the randomForest.default function is unable to evaluate the input parameters due to missing or invalid data. The function is unable to properly process the input parameters and returns an error message. This can be fixed by ensuring that all parameters have valid and complete data before attempting to use the function. If the input parameters are not valid, the function will return an error.


One error you may encounter in R is:

Error in randomForest.default(m, y, ...) : 
  NA/NaN/Inf in foreign function call (arg 1)

There are two reasons for why this error might occur:

  • There are NA, NaN, or Inf values in the dataset
  • One of the variables in the dataset is a character

The easiest way to fix this error is to remove rows with missing data and convert character variables to factor variables:

#remove rows with missing values 
df <- na.omit(df)

#convert all character variables to factor variables
library(dplyr)
df %>% mutate_if(is.character, as.factor)

This tutorial shares an example of how to fix this error in practice.

Related: 

How to Reproduce the Error

Suppose we attempt to fit a random forest to the following data frame in R:

library(randomForest)

#create data frame
df <- data.frame(y <- c(30, 29, 30, 45, 23, 19, 9, 8, 11, 14),
                 x1 <- c('A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),
                 x2 <- c(4, 4, 5, 7, 8, 7, 9, 6, 13, 15))

#attempt to fit random forest model
model <- randomForest(formula = y ~ ., data = df)

Error in randomForest.default(m, y, ...) :
  NA/NaN/Inf in foreign function call (arg 1)

We receive an error because x1 is a character variable in the data frame.

We can confirm this by using the str() function to view the structure of the data frame:

str(df)

'data.frame':	10 obs. of  3 variables:
 $ y....c.30..29..30..45         : num  30 29 30 45 23 19 9 8 11 14
 $ x1....c..A....A....B....B.... : chr  "A" "A" "B" "B"
 $ x2....c.4..4..5..7..          : num  4 4 5 7 8 7 9 6 13 15

How to Fix the Error

To fix this error, we can use the mutate_if() function from to convert each character column to a factor column:

library(dplyr)

#convert each character column to factor
df = df %>% mutate_if(is.character, as.factor)

#fit random forest model
model <- randomForest(formula = y ~ ., data = df)

#view summary of model
model

Call:
 randomForest(formula = y ~ ., data = df) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 1

          Mean of squared residuals: 65.0047
                    % Var explained: 48.64

We don’t receive any error this time because there are no longer any character variables in the data frame.

The following tutorials explain how to address other common errors in R:

x