how to calculate colMedian using colMedian function. I get an error: Argument 'x' must be a matrix or a vector.
col_medians <- round(colMedians(impute_marks[,-1], na.rm=TRUE),0)
k <- which(is.na(impute_marks), arr.ind=TRUE)
impute_marks[k] <- col_medians[k[,-1]]
I need to do the below operation for all the columns other than first column in the data frame. Below code works fine. but I in for loop gives an error unknown courses when looped.
impute_marks$c1[is.na(impute_marks$c1)] <- round(mean(impute_marks$c1[!is.na(impute_marks$c1)]),0)
here, impute_marks is the name of the dataset and c1 is the column name.
using the above operation I am able to find the mean and replace all NA values in c1 (column). But I have 30+ columns. How can I write the above operation in a for loop to loop through each course and replace NA value with the mean?
my function for the operation:
impute_marks$F27SA[is.na(impute_marks$F27SA)] <- round(mean(impute_marks$F27SA[!is.na(impute_marks$F27SA)]),0)
imputing_using_mean <- function()
{
courses <- names(impute_marks)[2:26]
for(i in seq_along(courses))
{
impute_marks$courses[[i]][is.na(impute_marks$courses[[i]])] <- round(mean(impute_marks$courses[[i]][!is.na(impute_marks$courses[[i]])]),0)
}
}
imputing_using_mean()