-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_frames.R
More file actions
45 lines (30 loc) · 1.35 KB
/
Data_frames.R
File metadata and controls
45 lines (30 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
set.seed(100)
data<-as.data.frame(ggplot2::mpg)
#-------------------------------------------------------------------------------------------------------------------------------
rows_sample<-sample(nrow(data), 10)
rows_sample
data[rows_sample,"cty"]<--999
#-------------------------------------------------------------------------------------------------------------------------------
replace_negatives<-function(x)
{
ifelse(x<0,NA,x)
}
data<-data.frame(lapply(data,replace_negatives))
#-------------------------------------------------------------------------------------------------------------------------------
mean_by_class <- sapply(unique(as.character(data$class)),
function(x) { mean(data[data$class == x, 'cty'],na.rm = T) })
for (i in rows_sample)
{
if(is.na(data$cty[i]))
{
data$cty[i]<- mean_by_class[[data$class[i]]]
}
}
#Alternate method
#data$cty <- with(data, ave(cty, class,
# FUN = function(x) replace(x, is.na(x), mean(x, na.rm = TRUE))))
#-------------------------------------------------------------------------------------------------------------------------------
summary(data)
#-------------------------------------------------------------------------------------------------------------------------------
mean(mpg$cty)
mean(data$cty)