r - 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!
解决方案
假设您的数据框被称为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])
推荐阅读
- sql - 使用 t-sql 从一组特定数字中获取 6 个数字的所有组合
- javascript - 双向范围滑块,具有两个方向的颜色
- python - 3D python scipy数据插值的问题-在结果数据上交换轴
- typescript - 未捕获的类型错误:无法读取未定义的属性“振动”
- c - 凯撒密码没有被check50清除,不知道为什么
- javascript - Discord.JS 删除包含某个单词的消息
- javascript - deno - 如何提供图像
- html - 将元素放置在水平线上,其中一个放置在中心
- python - 如何在 ODS 文件中获取工作表名称?
- javascript - 在打字稿中使用 Map.get() 方法,返回未定义,而我正在处理未定义