第3回R統計解析環境ゼミ

今月いろいろ忙しくて,3週間も更新できていません。

昨日3回目のRゼミをして、平均など簡単な統計の計算を紹介しました。

そのひのコマンドをここでも紹介します。参加できな方々は参考にしてみてください。


# Growth rate of phytoplankton
# T is the temperature.
# S is the salinity
# We can declare our own functions.
growth.fn=function(T,S) {
growth=(-6.84e-4*T^2+0.0354*T-0.213)*(-1.03e-3*S^2+0.579*S-0.548)/0.31
return(growth)
}

# Range of salinity is 10 to 35, in steps of 5 (by = 5).
# Range of temperature is 15 to 35, in steps of 5.
# seq() is the sequence function
salinity.range=seq(10,35,by=5)
temperature.range=seq(15,35,by=5)

# Determine the number of salinity and temperature variables.
# length() is the length function.
salinity.range.length=length(salinity.range)
temperature.range.length=length(temperature.range)

# Choose the amount of replicates used for each salinity x temperature pair.
replicates=10

# Put the data together.
# rep() is the repeat function.
salinity=rep(salinity.range, replicates * temperature.range.length)
temperature=rep(temperature.range, each = replicates * salinity.range.length)

# Gather all the data into a data.frame.
# data.frame() is the function to create a table of data.
sampledata=data.frame(temperature,salinity,growth=growth.fn(temperature,salinity))

# Add some noise to the data.
# rnorm() is the function to return values from a normal distribution.
sampledata$growth=rnorm(length(sampledata$growth),mean=sampledata$growth,sd=1)

# Determine the mean of each T x S pair, which returns a data.frame
x=aggregate(sampledata$growth, list(sampledata$temperature,sampledata$salinity),mean)

# You can also use tapply, which returns a multi-dimensional array.
tapply(sampledata$growth, list(sampledata$temperature,sampledata$salinity),mean)

コメント

このブログの人気の投稿

RStudioとggplot():プロットができないとき

光合成関連の単語

大村湾調査!! ~海藻・生き物編~