Shinshu
University
信州大学 農学部 食料生産システム科学コース/
大学院 農学専攻 先端生命科学分野
土壌生物学研究室

Rメモ
データ取り込み
a <- read.delim("clipboard", header=T)
基本統計量
sum()
mean()
sd()
cor(x,y)
summary()
データ集計
---
#1要因
a <- read.delim("clipboard", header=F)#header無しで
library(psych)
b <- describeBy(a\\$V2, list(a\\$V1), mat=T)
b
par(mar=c(5, 4, 4, 2)) #x軸のラベルが切れる場合は1要素目を変更
c <- barplot(b\\$mean, names.arg=b\\$group1,
ylim=c(0,max(b\\$mean+b$se)), las=3)
arrows(c, b\\$mean-b\\$se, c, b\\$mean+b\\$se, angle=90, length=0.1)
arrows(c, b\\$mean+b\\$se, c, b\\$mean-b\\$se, angle=90, length=0.1)
library(agricolae)
anova <- aov(V2 ~ V1, data=a)
summary(anova)
(HSD.test(anova, "V1", group=T))
#2要因
a <- read.delim("clipboard", header=F)#header無しで
library(psych)
b <- describeBy(a\\$V3, list(a\\$V1, a\\$V2), mat=T)
b
par(mar=c(5, 4, 4, 2))
#x軸のラベルが切れる場合は1要素目を変更
c <- barplot(b\\$mean, names.arg=paste(b\\$group2, b\\$group1, sep=", "), ylim=c(0,max(b\\$mean+b\\$se)), las=3)
arrows(c, b\\$mean-b\\$se, c, b\\$mean+b\\$se, angle=90, length=0.1)
arrows(c, b\\$mean+b\\$se, c, b\\$mean-b\\$se, angle=90, length=0.1)
library(agricolae)
anova <- aov(V3 ~ V1*V2, data=a)
summary(anova)
a2 <- cbind(paste(a\\$V1, a\\$V2, sep="_"), a)
colnames(a2) <- c("treatment", "treatment1", "treatment2", "measurement")
anova2 <- aov(measurement ~ treatment, data=a2)
(HSD.test(anova2, "treatment", group=T))
#3要因
a <- read.delim("clipboard", header=F)#header無しで
library(psych)
b <- describeBy(a\\$V4, list(a\\$V1, a\\$V2, a\\$V3), mat=T)
b
par(mar=c(5, 4, 4, 2))
#x軸のラベルが切れる場合は1要素目を変更
c <- barplot(b\\$mean, names.arg=paste(b\\$group3, b\\$group2, b\\$group1, sep=", "), ylim=c(0,max(b\\$mean+b\\$se)), las=3)
arrows(c, b\\$mean-b\\$se, c, b\\$mean+b\\$se, angle=90, length=0.1)
arrows(c, b\\$mean+b\\$se, c, b\\$mean-b\\$se, angle=90, length=0.1)
-----
結果をコピーしエクセルのシートに張り付けたら、「テキストウィザードで編集」を選択し、スペース区切りを選ぶ
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)
分散分析
----
#1要因
measurement<-"measurement"
treatment<-"treatment"
a <- read.delim("clipboard", header=T)
library(agricolae)
anova <- aov(measurement ~ treatment, data=a)
summary(anova)
(HSD.test(anova, treatment, group=T))
#2要因
measurement<-"measurement"
treatment1<-"treatment1"
treatment2<-"treatment2"
a <- read.delim("clipboard", header=T)
library(agricolae)
anova <- aov(measurement ~ treatment1*treatment2, data=a)
summary(anova)
a2 <- cbind(paste(a\\$treatment1, a\\$treatment2, sep="_"), a)
colnames(a2[,1]) <- "treatment"
anova2 <- aov(measurement ~ treatment, data=a2)
(HSD.test(anova2, "treatment", group=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()#符号
log(p/(1-p))#ロジット変換 P:比率
データの型
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[a\\$V1=="F", )#条件に合うもののみ処理する
b<-a[ ,colnames(a)=="treatment"]#条件に合う列名のデータを抽出する
d <- [order(d\\$V1, decreasing=T),]#降順、昇順はdecreasing=Tを書かない