DrKp
DrKp

Reputation: 11

argument is not numeric or logical: returning NA in R - How to fix

I am get an error when I try to add a second row with NA's in the Age column of the DF and run the code to clean it up. I am wondering how to fix it. I am new to R and this example comes from a Udemy Course. This is not a quiz question - it is part of the presentation.

I added the additional row because I wanted to see if the code would work.

I did search this forum, but could not find an answer I understand.

#create the df cols
Country <- c("France", "Spain", "Germany", "Spain", "Germany", "France", "Spain", "France","Germany", "France")
Age <- c(44, 27, 30, 38, 40, 35, NA, 48, 50, 37)
Salary <- c(72000, 48000, 54000, 61000, NA, 58000, 52000, 79000, 830000, 67000)
Purchased <- c("No","Yes","No","No","Yes","Yes","No","Yes","No","Yes")

#create the df
empdata <- data.frame(Country, Age, Salary, Purchased)

#The logic to set the 'NA' values to the mean  will not work when you add this record, WHY?
empdata[nrow(empdata) + 1, ] = c('Spain', NA , 67000.00, "Yes")


#Run this logic to fix the 'NA' Values
empdata$Age = ifelse(is.na(empdata$Age), 
                     ave(empdata$Age, FUN = function(x) mean(x, na.rm= TRUE)),
                     empdata$Age)

empdata$Salary = ifelse(is.na(empdata$Salary), 
                        ave(empdata$Salary, FUN = function(x) mean(x, na.rm = TRUE)),
                        empdata$Salary)

#view the data
empdata

Upvotes: 1

Views: 40

Answers (1)

akrun
akrun

Reputation: 887048

This line

empdata[nrow(empdata) + 1, ] = c('Spain', NA , 67000.00, "Yes")

converts everything to character because a vector can hold only a single class. We can wrap it in a list as list can hold multiple type

empdata[nrow(empdata) + 1, ] = list('Spain', NA , 67000.00, "Yes")

Now we run the ifelse and should work

empdata$Age = ifelse(is.na(empdata$Age), 
                      ave(empdata$Age, FUN = function(x) mean(x, na.rm= TRUE)),
                      empdata$Age)
empdata
#   Country      Age Salary Purchased
#1   France 44.00000  72000        No
#2    Spain 27.00000  48000       Yes
#3  Germany 30.00000  54000        No
#4    Spain 38.00000  61000        No
#5  Germany 40.00000     NA       Yes
#6   France 35.00000  58000       Yes
#7    Spain 38.77778  52000        No
#8   France 48.00000  79000       Yes
#9  Germany 50.00000 830000        No
#10  France 37.00000  67000       Yes
#11   Spain 38.77778  67000       Yes

Upvotes: 1

Related Questions