Skip to content

Instantly share code, notes, and snippets.

@tmosest
Created October 8, 2016 09:15
Show Gist options
  • Save tmosest/a853804ad350409ef6ce8804c081ad18 to your computer and use it in GitHub Desktop.
Save tmosest/a853804ad350409ef6ce8804c081ad18 to your computer and use it in GitHub Desktop.
R basics from STDIN
# Enter your code here. Read input from STDIN. Print output to STDOUT
f <- file("stdin")
open(f)
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
cases <- readLines(f,n=1)
line <- readLines(f,n=1)
data <- as.numeric(strsplit(line, " ")[[1]])
n <- length(data)
mean <- mean(data)
median <- median(data)
mode <- getmode(data)
sd <- sqrt(sum((data - mean(data))^2) / (n))
sd2 <- sd(data)
error <- qnorm(0.975)*sd/sqrt(n)
min <- mean - error
max <- mean + error
c <- c(min, max)
o <- paste(c, collapse=" ")
#prop.test(data, conf.level = 0.95)
#options(digits=1)
write(mean, stdout())
write(median, stdout())
write(mode, stdout())
write(sd, stdout())
write(o, stdout())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment