# -- 10 --------------- 1+2 (3 * 4) ^ 2 a <- 3 * 4 A <- 5 a A sales <- c(1,2,3,4,5) sales sales <- 1:5 sales sales2 <- sales sales2 idx <- "sales" idx idx2 <- c("stock", "sales") idx2 # -- 11 --------------- sales[2] sales * 1.1 sqrt(2) sqrt(sales) mean(sales) # -- 14 --------------- dat <- read.csv("c:/R_work/grade1.csv") dat <- read.csv("grade1.csv") dat <- read.table("c:\R_work/\grade1.csv", header=T, sep=",") dat # すべてが表示される head(dat) # 最初の6行が表示される head(dat,6) # -- 15 --------------- apply(dat[,2:5], 2, mean) plot(dat[,2],dat[,4]) # -- 19 --------------- cst <- read.csv("customer.csv") cst head(cst) summary(cst) # -- 20 --------------- tapply(cst$Purchase, cst$Sex, mean) tapply(cst$Purchase, cst$Branch, mean) tapply(cst$Purchase, list(cst$Sex, cst$Branch), mean) # -- 21 --------------- hist(cst$Purchase) boxplot(cst$Purchase) boxplot(cst$Purchase~cst$Branch) stripchart(cst$Purchase~cst$Branch, vertical = TRUE, pch = 21, col = "maroon", bg ="orange",method = "jitter", add = TRUE) # -- 22 --------------- plot(cst$Visit, cst$Purchase) plot(cst$Time, cst$Purchase) plot(cst$Visit, cst$Time) cor(cst[,c(3,4,6)]) # -- 23 --------------- ttn <- read.csv("Titanic.csv") head(ttn) summary(ttn) # -- 24 --------------- table(ttn$Class, ttn$Survive) table(ttn$Age, ttn$Survive) table(ttn$Sex, ttn$Survive) mosaicplot(table(ttn$Class, ttn$Survive)) # -- 26 --------------- mcn <- read.csv("machine5.csv") a <- mcn[mcn$MachineID=="A",] b <- mcn[mcn$MachineID=="B",] d <- mcn[mcn$Period=="Day",] n <- mcn[mcn$Period=="Night",] hist(a$Weight, breaks=seq(98.5,100.5,0.1), border="#990000", col="#99343550", main="Histogram", xlab="weight", ylim=c(0,40)) hist(b$Weight, breaks=seq(98.5,100.5,0.1), border="#999900", col="#edae0050", add=T) hist(d$Weight, breaks=seq(98.5,100.5,0.1), border="#009900", col="#53995250", main="Histogram", xlab="weight", ylim=c(0,40)) hist(n$Weight, breaks=seq(98.5,100.5,0.1), border="#000099", col="#5399ff50", add=T) # -- 27 --------------- library(ggplot2) ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point() ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,colour=Species,size=Petal.Width))+geom_point() # -- 30 --------------- ice <- read.csv("IceCream.csv") cor(ice) pairs(ice) # -- 31 --------------- library(lattice) splom(~ice[3:7], groups = ice$Month, pch=16, col=c(1,2,3,4,5,6,7,8,9,10,11,12)) # -- 32 --------------- lm.ice <- lm(Paid~Temp, data=ice) summary(lm.ice) # -- 33 --------------- plot(ice$Temp, ice$Paid) abline(lm.ice$coef) text(ice$Temp, ice$Paid, ice$Month, pos=2, col=3) x <- c(1, 17) t(lm.ice$coef) %*% x # -- 33 --------------- sak <- read.csv("sake.csv", row.names="Pref") pca.sak<-prcomp(sak, scale=T) pca.sak # -- 34 --------------- plot(pca.sak$x[,1], pca.sak$x[,2]) text(pca.sak$x[,1], pca.sak$x[,2], rownames(sak), pos=2, col=3) plot(pca.sak$rotation[,1], pca.sak$rotation[,2]) text(pca.sak$rotation[,1], pca.sak$rotation[,2], colnames(sak), pos=2, col=3) biplot(pca.sak) # -- 35 --------------- nds <- read.csv("ndsoup.csv", row.names="Brand") library(MASS) ca.nds <- corresp(nds, nf=2) biplot(ca.nds, col=c(3, 4)) # -- 36 --------------- cty <- read.csv("cityecon2.csv", row.names="City") x=scale(cty) hc <- hclust(dist(x, method = "euclidean"), "complete") plot(hc, hang = -1) # -- 42 --------------- rec <- function(x, y){ x * y } rec(3, 4)