Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to replace missing values with median in an R data frame column?
To replace missing values with median, we can use the same trick that is used to replace missing values with mean. For example, if we have a data frame df that contain columns x and y where both of the columns contains some missing values then the missing values can be replaced with median as df$x[is.na(df$x)]<-median(df$x,na.rm=TRUE) for x and for y we can do the same as df$y[is.na(df$y)]<-median(df$y,na.rm=TRUE).
Example
df2$Ratings[is.na(df2$Ratings)]<-median(df2$Ratings,na.rm=TRUE) df2
Output
ID Ratings 1 1 3.0 2 2 1.0 3 3 1.0 4 4 4.0 5 5 1.0 6 6 4.0 7 7 2.0 8 8 3.0 9 9 2.0 10 10 2.0 11 11 3.0 12 12 5.0 13 13 5.0 14 14 1.0 15 15 4.0 16 16 1.0 17 17 4.0 18 18 2.5 19 19 1.0 20 20 2.5
Advertisements
