以下のようなデータフレームを想定します。
各ステーションごとのspeciesAとBの体長と、それに対応するカウントデータです。
> set.seed(10) > df <- data.frame( + species = c(rep("A",4), rep("B",4)), + station = rep(1:2, 4), + length = round(rnorm(8, 15, 2)), + count = round(rnorm(8, 5, 2)) + ) > df species station length count 1 A 1 15 2 2 A 2 15 4 3 A 1 12 7 4 A 2 14 7 5 B 1 16 5 6 B 2 16 7 7 B 1 13 6 8 B 2 14 5
これを以下のような2cm刻みのテーブルに変形したいのですが、どうすればよいでしょうか?
> cnt_table <- data.frame( + species = c("A","A","B","B"), + station = c(1,2,1,2), + L11_12 = c(0,0,0,0), + L13_14 = c(7,7,6,5), + L15_16 = c(2,4,5,7), + L17_18 = c(0,0,0,0), + L19_20 = c(0,0,0,0) + ) > cnt_table species station L11_12 L13_14 L15_16 L17_18 L19_20 1 A 1 0 7 2 0 0 2 A 2 0 7 4 0 0 3 B 1 0 6 5 0 0 4 B 2 0 5 7 0 0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。