Заменить значения менее 18 в R на среднее значение столбца - PullRequest
0 голосов
/ 23 октября 2019

Я пытаюсь очистить и обучить данные Dodger Loop, чтобы использовать среднее количество автомобилей за 5 минут из 19 для любого записанного значения, меньшего 18. Я уже удалил все недостающие данные, но, похоже, могу понять, как это сделать. значения от -1 до 17 и переключите их на 19.

#Set working directory
setwd("xxx")
#import Traffic Data dodger.csv rename it to traffic
Traffic <- read.csv(xxx header=FALSE)
View(Traffic)
#import doger game data dodger.events rename it to games
Games <- read.csv("xxx header=FALSE)
View(Games)
#open class and ggplot pacakages
library(class)
library(tm)
library(data.table)
library(ggplot2)
library(caret)
#View dat structure
str(Traffic)
#Find the median of traffic to use for missing data
summary(Traffic)
#
str(Games)
summary(Games)
#Remove uneeded columns and Name ID Columns
Games <- Games[-6]
Games <- Games[-5]
View(Games)
names(Games)[1]<-"Date"
names(Games)[2]<-"Start Time"
names(Games)[3]<-"End"
names(Games)[4]<-"Attendance"
names(Games)[2]<-"Start"
View(Games)
#Name ID Colums in Traffic
View(Traffic)
names(Traffic)[1]<-"Date and Time"
names(Traffic)[2]<-"Amount"
View(Traffic)

#Find the mean of Traffic
mean(Traffic$Amount)
[1] NA
#Mean is undefined missing data, replace with mean
mean(Traffic$Amount, na.rm = T)
                   [1] 18.95073
#Averacars per 5 min is 18.9, round to 19
#Replace missing (-1) data with average car per 5 min
Avg_Traffic <- ave(Traffic$Amount, FUN = function(x) 
mean(x, na.rm = T))


Traffic$Amount <- ifelse(is.na(Traffic$Amount), 
Avg_Traffic, Traffic$Amount)

##Traffic$Amount[x <18]?????

1 Ответ

0 голосов
/ 23 октября 2019

Изменить все -1 или значения до 19

Трафик $ Сумма <- ifelse (Трафик $ Сумма <18,18, Трафик $ Сумма) </p>

Проверить данные для исправления = SAT

печать (Трафик)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...