-3

I have a dataset named malt, where one of the columns is named ka. I want to replace NA values in that ka column by mean values in malt$ka and other value remain as it is, so do this by if else

 malt$ka <- ifelse(malt$ka=="NA", mean(malt$ka), "malt$AcqCostPercust") 

This does not seem to work, and I am confused how to replace values the NA values.

Axeman
  • 32,068
  • 8
  • 81
  • 94

2 Answers2

1

Or

malt$ka[is.na(malt$ka)] <- mean(malt$ka, na.rm = TRUE)
Axeman
  • 32,068
  • 8
  • 81
  • 94
0
x <- mean(malt$ka, na.rm=T) # assign mean value to a variable x
malt$ka<-ifelse(is.na(malt$ka),x,malt$ka)
Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22