diff options
Diffstat (limited to 'lab2')
| -rw-r--r-- | lab2/Rplots.pdf | bin | 0 -> 5314 bytes | |||
| -rw-r--r-- | lab2/abc.Rdata | bin | 0 -> 109 bytes | |||
| -rwxr-xr-x | lab2/intro.r | 48 | ||||
| -rwxr-xr-x | lab2/main.r | 74 |
4 files changed, 122 insertions, 0 deletions
diff --git a/lab2/Rplots.pdf b/lab2/Rplots.pdf Binary files differnew file mode 100644 index 0000000..7074237 --- /dev/null +++ b/lab2/Rplots.pdf diff --git a/lab2/abc.Rdata b/lab2/abc.Rdata Binary files differnew file mode 100644 index 0000000..c40c2da --- /dev/null +++ b/lab2/abc.Rdata diff --git a/lab2/intro.r b/lab2/intro.r new file mode 100755 index 0000000..037210e --- /dev/null +++ b/lab2/intro.r @@ -0,0 +1,48 @@ +#!/usr/bin/env Rscript + +v <- c(1:10, seq(11, 20, by = 2)) + +typeof(1) +typeof(1L) +typeof(v) +typeof(as.integer(v)) +head(v) +str(v) +plot(v) + + +save.image(file = "abc.Rdata") + +v <- seq(1, 10, by = 0.5) +v + +load("abc.Rdata") + +v + + +v <- c("a", "b", "c", "b", "a") + +f <- factor(v) + +func <- function(x, y = 0) { + return((x * x) + y) +} + +func(y = 3, 5) + + +m <- matrix(c(1:6, 11:13), nrow = 3, ncol = 3, byrow = TRUE) +m + +m[2:3, 1:2] + +df <- data.frame(a = c(1:5), b = c("a", "b", "c", "d", "e")) + +df$a +df$b + +df[df$a > 2, ] + + +q("no")
\ No newline at end of file diff --git a/lab2/main.r b/lab2/main.r new file mode 100755 index 0000000..f468363 --- /dev/null +++ b/lab2/main.r @@ -0,0 +1,74 @@ +#!/usr/bin/env Rscript + +zip_income <- read.table("zipIncome.txt", header = TRUE, sep = "|") + +names(zip_income) <- c("zipCode", "income") + +head(zip_income) +tail(zip_income) + +overall_mean <- mean(zip_income$income) +overall_median <- median(zip_income$income) + +summary(zip_income) + +cat("Mean income:", overall_mean, "\n") +cat("Median income:", overall_median, "\n") + + +dim(zip_income) +names(zip_income) + +unique(zip_income$zipCode) + +colSums(is.na(zip_income)) + +zip_income_clean <- na.omit(zip_income) + +colSums(is.na(zip_income_clean)) + +zip_income$income[is.na(zip_income$income)] <- -100 +zip_income$zipCode[is.na(zip_income$zipCode)] <- 99 +write.table(zip_income, + file = "zip_income_clean.txt", # output filename + sep = "|", + row.names = FALSE +) + +s1 <- c(seq(1, 10, by=1), seq(16, 25, by=1)) + +df <- data.frame(x = seq(1, 20), y = s1) + +print(df) +plot(df) + +model <- lm(y ~ x, data = df) +summary(model) + +plot(df$x, df$y) +abline(model, col = "red") + +plot(density(zip_income$income)) + +hist(zip_income$income) + +library(lattice) +densityplot(zip_income$income) +densityplot(log(zip_income$income)) + +x <- rnorm(10) +y <- rnorm(10,2) + +t.test(x, y) + +# ---------------------------------- + +zip_income <- read.table("zipIncome.txt", header = TRUE, sep = "|") + +names(zip_income) <- c("zipCode", "income") + +model <- lm(income ~ factor(zipCode), data = zip_income) +summary(model) + +plot(zip_income$zipCode, zip_income$income) +abline(model, col = "red") |
