R 用平均值或最频繁的字符串填充缺少的数据

R 用平均值或最频繁的字符串填充缺少的数据,r,R,我是R的新手。我的数据框缺少一些数据。例如: Temperature Location 10.2 New York 13.2 New York Toronto 10 我想用列的平均值填充数字列,用最频繁的字符串填充非数字列。我想这件事是在纽约 . 我能得到一些帮助吗 谢谢 假设您的数据帧称为df: 到目前为止你试过什么?有用的:is.na,dplyr::coalesce df$Temperature[

我是R的新手。我的数据框缺少一些数据。例如:

Temperature   Location
  10.2        New York
  13.2        New York
              Toronto
  10           
我想用列的平均值填充数字列,用最频繁的字符串填充非数字列。我想这件事是在纽约 . 我能得到一些帮助吗


谢谢

假设您的数据帧称为df:


到目前为止你试过什么?有用的:is.na,dplyr::coalesce
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])