首页 > 解决方案 > Fill the missing data by the average or the most frequent string

问题描述

I am new to R. My dataframe has some missing data. For example:

Temperature   Location
  10.2        New York
  13.2        New York
              Toronto
  10           

I want fill the numeric columns by the average of the column and fill the non-numeric columns by the most frequent string. I this case, it will be "New York ". Can I get some help?

Thanks!

标签: r

解决方案


假设您的数据框被称为df

df$Temperature[is.na(df$Temperature)] <-
  mean(df$Temperature, na.rm = T)

df$Location[is.na(df$Location)] <-
  names(sort(table(df$Location), decreasing = T)[1])

推荐阅读