前田 和寛(@kazutan)
2014/11/29
R勉強会@広島(Hijiyama.R)
比治山大学短期大学部
@kazutan
#ビールうめぇ
“data.csv"というファイルを読み込むなら…
x <- read.csv("data.csv")
一行目に変数名がある場合は自動で読み込みますが、うまく行かない時は…
x <- read.csv("data.csv", header = TRUE)
XLConnectパッケージが必要です
# パッケージインストール
install.packages("XLConnect")
# パッケージ読み込み
library(XLConnect)
# ekusu.xlsxの一つ目のシートを読み込む
x <- readWorksheetFromFile("ekusu.xlsx", sheet = 1)
でも正直なところ、csvファイルに落としこんで読み込んだほうがいいですよ
あらかじめExcelなどで読み込ませたいデータをコピーして…
x <- read.table("clipboard", header = TRUE)
header = FALSE
x <- read.table(pipe('pbpaste'), header = TRUE)
(1)で指定したファイル名を以下のように変更
x <- read.csv(file.choose())
file.choose()
の場所に放り込む」ことをしてます行番号を指定したアクセス
iris[3,] #3行目(全部)にアクセス
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
3 4.7 3.2 1.3 0.2 setosa
列番号を指定したアクセス(最初の5行だけ)
head(iris[,2],5) #irisの2列目にアクセス
[1] 3.5 3.0 3.2 3.1 3.6
iris[21:24,] #21行目から24行目へのアクセス
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
21 5.4 3.4 1.7 0.2 setosa
22 5.1 3.7 1.5 0.4 setosa
23 4.6 3.6 1.0 0.2 setosa
24 5.1 3.3 1.7 0.5 setosa
iris[21:24,1:3] #21-24行目の1-3列目へのアクセス
Sepal.Length Sepal.Width Petal.Length
21 5.4 3.4 1.7
22 5.1 3.7 1.5
23 4.6 3.6 1.0
24 5.1 3.3 1.7
iris[21:24,-5] #21-24行目から、5列目を除外
Sepal.Length Sepal.Width Petal.Length Petal.Width
21 5.4 3.4 1.7 0.2
22 5.1 3.7 1.5 0.4
23 4.6 3.6 1.0 0.2
24 5.1 3.3 1.7 0.5
iris[21:24,-4:-5] #21-24行目から4,5列目を除外
Sepal.Length Sepal.Width Petal.Length
21 5.4 3.4 1.7
22 5.1 3.7 1.5
23 4.6 3.6 1.0
24 5.1 3.3 1.7
head(iris$Sepal.Width,3)
[1] 3.5 3.0 3.2
ls(iris) #iris内の変数(オブジェクト)リストを出力
[1] "Petal.Length" "Petal.Width" "Sepal.Length" "Sepal.Width"
[5] "Species"
head(subset(iris, select = 2:3),3) #irisの2-3列
Sepal.Width Petal.Length
1 3.5 1.4
2 3.0 1.4
3 3.2 1.3
head(subset(iris,select = Sepal.Width),3)
Sepal.Width
1 3.5
2 3.0
3 3.2
head(subset(iris, select = c(Sepal.Length,Sepal.Width)),3)
Sepal.Length Sepal.Width
1 5.1 3.5
2 4.9 3.0
3 4.7 3.2
head(subset(iris, select = Sepal.Length:Petal.Length),3)
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
head(subset(iris,select = -c(Sepal.Length,Species)),2)
Sepal.Width Petal.Length Petal.Width
1 3.5 1.4 0.2
2 3.0 1.4 0.2
subset(iris,select=2:3, subset=Sepal.Width>4.0)
Sepal.Width Petal.Length
16 4.4 1.5
33 4.1 1.5
34 4.2 1.4
iris2 <- transform(iris, kazu=Sepal.Width+5)
head(iris2,3)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species kazu
1 5.1 3.5 1.4 0.2 setosa 8.5
2 4.9 3.0 1.4 0.2 setosa 8.0
3 4.7 3.2 1.3 0.2 setosa 8.2
先ほどのiris2データフレームを使います
iris2 <- transform(iris2, kazu=kazu^2)
head(iris2,3)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species kazu
1 5.1 3.5 1.4 0.2 setosa 72.25
2 4.9 3.0 1.4 0.2 setosa 64.00
3 4.7 3.2 1.3 0.2 setosa 67.24
x y
1 1 11
2 2 12
3 3 NA
4 NA 14
5 5 15
x[complete.cases(x),]
x y
1 1 11
2 2 12
5 5 15
na.rm
オプションで指定していく方法もhead(iris[,1:3],3)
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
head(rowSums(iris[,1:3]),3)
[1] 10.0 9.3 9.2
head(iris[,1:3],3)
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
head(rowMeans(iris[,1:3]),3)
[1] 3.333333 3.100000 3.066667
a b c
1 1 A 11
2 2 B 12
3 3 C 13
a b d
1 2 B 12
2 3 C 13
3 4 D 14
merge(x,y)
a b c d
1 2 B 12 12
2 3 C 13 13
両方を網羅して結合
merge(x,y,all=TRUE)
a b c d
1 1 A 11 NA
2 2 B 12 12
3 3 C 13 13
4 4 D NA 14
install.packages("ggplot2")
library(ggplot2)
qplot(Sepal.Length, Sepal.Width, data=iris)
qplot(Sepal.Length, Sepal.Width, data=iris, main="kazutan")
qplot(Sepal.Length, Sepal.Width, data=iris, xlab="yoko", ylab="tate")
qplot(Sepal.Length, Sepal.Width, data=iris, xlim=c(0,7), ylim=c(1,8))
qplot(Sepal.Length, Sepal.Width, data=iris, color=Species)
qplot(Sepal.Length, Sepal.Width, data=iris, shape=Species)
qplot(Sepal.Length, Sepal.Width, data=iris, geom=c("point","smooth"))
c()
で束ねて重ね書きしてある
qplot(Sepal.Length, Sepal.Width, data=iris, geom=c("point", "smooth"), method="lm")
qplot(Species, Sepal.Width, data=iris, geom="boxplot")
qplot(Species, data=iris, geom="bar")
xy <- data.frame(x = c("A","B","C"), y = c(3.5,4.5,4.2))
qplot(x, data=xy, geom="bar", weight=y)
xy <- data.frame(x = 2010:2014, y = c(3.5, 4.5, 4.2, 4.9, 5.2))
qplot(x, y, data=xy, geom="line")