[英]Error in function to normalize data applied to a data frame
我已经从UCI机器学习存储库下载了bike-sharing-dataset,并试图在R中实现多元线性回归。这是数据的格式:
> head(data1)
season mnth hr holiday weekday workingday weathersit temp atemp hum windspeed cnt
1 1 1 0 0 6 0 1 0.24 0.2879 0.81 0.0000 16
2 1 1 1 0 6 0 1 0.22 0.2727 0.80 0.0000 40
3 1 1 2 0 6 0 1 0.22 0.2727 0.80 0.0000 32
4 1 1 3 0 6 0 1 0.24 0.2879 0.75 0.0000 13
5 1 1 4 0 6 0 1 0.24 0.2879 0.75 0.0000 1
6 1 1 5 0 6 0 2 0.24 0.2576 0.75 0.0896 1
我正在尝试使用以下功能对特定列(尚未进行标准化)进行标准化:
normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x)))
}
问题是当我运行时:
dfNorm <- as.data.frame(lapply(data1["season", "mnth", "hr", "weekday", "weathersit"], normalize))
我收到以下错误:
[.data.frame
(数据1,“季节”,“月份”,“小时”,“工作日”,“天气”)中的错误:未使用的参数(“工作日”,“天气”)
为什么会出现此错误,我该如何解决?
要就地修改,我将使用dplyr::mutate
。 这样的事情应该起作用:
library(dplyr)
dfNorm <- data1 %>%
mutate_at(.vars = vars(season, mnth, hr, weekday, weathersit),
.funs = funs(normalize))
只需将lapply
分配给新列:
df[c("season_norm", "mnth_norm", "hr_norm", "weekday_norm", "weathersit_norm")] <-
lapply(df[c("season", "mnth", "hr", "weekday", "weathersit")], normalize)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.