-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMean.R
More file actions
51 lines (39 loc) · 1.14 KB
/
myMean.R
File metadata and controls
51 lines (39 loc) · 1.14 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
46
47
48
49
50
51
# Create our own user-defined mean function while trying to match functionality.
myMean <- function(x, trim = 0, na.rm = false, ...)
{
# If x is not logical (coerced to numeric), numeric (including integer) or complex, NA_real_ is returned, with a warning.
if ( class(x) != 'logical' && class(x) != 'numeric' && class(x) != 'integer' && class(x) != 'complex' )
{
# EX: [1] NA
# Warning message:
# In mean.default("hi") : argument is not numeric or logical: returning NA
# foo <- deparse(substitute(myMean))
cat('Warning message:\nIn "', deparse(match.call()), '" : argument is not numeric or logical: returning NA\n', sep = '')
return (NA_real_)
}
# na.rm means to strip values that aren't the correct class types in x.
# perform mean
results <- 0
count = length(x)
for (i in x)
{
#summation
results = results + i
}
# mean
results = results / count
return (as.numeric(results))
}
x <- myMean
print (x)
# NOT RUN {
x <- c(0:10, 50)
cat ('\n', x, '\n')
xm <- myMean(x)
cat ('\n', xm, '\n')
xJUNK <- c("This is junk!!!!")
xFAIL <- myMean(xJUNK)
cat ('\n', xFAIL, '\n')
# xc <- c(xm, mean(x, trim = 0.10))
# print (xc)
# }