データ取り込み
a <- read.delim("clipboard", header=T)
 
基本統計量
sum()
mean()
sd()
cor(x,y)
summary()
 
集計
split(a, a$V1)#カテゴリごとに分割
a[a$V1=="F" & a$V2>160]#Fで160以上の行を表示
subset(a, V1=="F")#Fの行を表示
by(a$V2, a$V1, mean)#カテゴリごとに集計
aggregate(a$V2, list(a$V1), mean)#カテゴリごとに集計
aggregate(a[,c(1:4)], list(a[,c(1:2)]), mean)#カテゴリごとに集計
 
等分散検定
var.test(x1, x2)
 
t検定
t.test(x, y, paired=F, var.equal=T)
 
分散分析 
x<-factor(x)
anova <- aov(y ~ x1*x2, data=a)
summary(anova)
TukeyHSD(anova)
library(agricolae)
(HSD.test(anova, "x", group=T))
 
相関
cor(x, y)
cor.test(x, y, method="pearson or spearman")
 
回帰
plot(x, y)
result <- lm(y~x)
summary(result)
abline(result)
 
データ書き出し
write.table(a, "filename", sep="\t", quote=F, append=F, row.names=F)
 
グラフ
plot(x, y, xlim=c(0,1), xlab="letter", log="x")#散布図
hist(x)#ヒストグラム
boxplot(x1, x2, names=c("x1", "x2")#箱ひげ図
 
par(new=T) #重ね合わせ
png(filename="xxx.png", width=480, height=480, pointsize=12, bg="white")#保存法
plot(1:10)
dev.off()
 
プログラム
for(i in 1:5){}
if(x<0){}else{}
 
演算子
== #equal
!= #not equal
>=
<=
! #not
&& #and (numeric)
|| #or (numeric)
& #and (vector)
| #or (vector)
xor(x,y)
 
関数
round(x, digits=1)
ceiling()#切り上げ
floor()#切り捨て
signif(x, digits=6) #有効桁数
asin()
log()#e
log10()
log2()
log1p()#log(1+x)
exp()#e
sqrt()
sign()#符号
 
データの型
NULL 何もない
NA 欠損値
NaN 非数
Inf 無限大
 
集合
union(x, y)#和
intersect(x, y)#積
setdiff(x, y)#差
 
文字列
chrtr()#置換
paste()#結合
 
ベクトル
x[10 < x & x < 40]#条件に合うものを取り出し

マージ
merge(x, y, by.x="x", by.y="y", all.x=T)
 
b <- ifelse(a$V1=="F", 1, a$V2)#条件に合うもののみ処理する

b<-a[ ,colnames(a)=="treatment"]#条件に合う列名のデータを抽出する