Gforces の動きを分析するコードがあり、時間の経過に伴う GForces のプロットが返されます。スクリプトはデータのサブセット (ID で分類) に対して機能しますが、データフレーム全体を一度に分析するために互換性を持たせる方法がわからず、ファセット グラフ (たとえば ggplot を使用) で結果を返します。誰かがこれを行う方法を知っていますか、それとも一度に 1 つの ID でデータを分析する運命にありますか?
コードは次のとおりです。
#Subset by ID
number1 <-subset(positions,subset=(ID==1))
head(number1)
A <- numeric()
Al <- numeric()
Radius <- numeric()
GForce <- numeric()
D12 <- numeric()
D13 <- numeric()
D23 <- numeric()
Proportion <- numeric()
Proportion_sel <- numeric()
nr<-length(number1$Timestamp)
for(i in 3:nr){
D12[i] <- sqrt((positions$X[i-2]-positions$X[i-1])^2 + (positions$Y[i-2]-positions$Y[i-1])^2)
D23[i] <- sqrt((positions$X[i-1]-positions$X[i])^2 + (positions$Y[i-1]-positions$Y[i])^2)
D13[i] <- sqrt((positions$X[i-2]-positions$X[i])^2 + (positions$Y[i-2]-positions$Y[i])^2)
if (D13[i]>0) {
if ((((D12[i]^2+D13[i]^2-D23[i]^2)^2)/(4*(D13[i]^2))) > D12[i]^2) {
A[i] <- 0
Radius[i] <- 0
GForce[i] <- 0
}
else {
A[i] <- sqrt(D12[i]^2-(((D12[i]^2+D13[i]^2-D23[i]^2)^2)/(4*(D13[i]^2))))
Radius[i] <- ((0.5*D13[i])^2+(A[i])^2)/(2*A[i])
GForce[i] <- ((D12[i]+D23[i])/2)^2/Radius[i]
}
}
else {
A[i] <- 0
Radius[i] <- 0
GForce[i] <- 0
}
}
# GForce plot over Time
plot(number1$Timestamp,GForce)
これは私に戻ります: - 経時的な Gforces のプロット
ID ごとに GForce を返し、それらを元のデータフレームに追加の列として返す方法を見つけると、問題が発生するようです。これが可能になると、ggplot を使用して ID ごとにプロットすることも簡単になりますが、これを達成する方法がわかりません..
データ (簡略化):
dput(positions)
structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), Timestamp = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L,
33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L
), X = c(-34.126, -34.087, -34.047, -34.01, -33.983, -33.973,
-33.985, -34.019, -34.07, -34.13, -34.188, -34.237, -34.273,
-34.295, -34.305, -34.307, -34.305, -34.301, -34.299, -34.297,
-34.296, -34.29, -34.272, -34.235, -34.178, -34.107, -34.034,
-33.967, -33.91, -33.865, -33.832, -33.81, -33.799, -33.8, -33.814,
-33.841, -33.878, -33.923, -33.975, -34.033, -34.098, -34.17,
-34.243, -34.311, -34.366, -33.691, -33.646, -33.598, -33.547,
-33.497, -33.452, -33.414, -33.383, -33.357, -33.331, -33.302,
-33.268, -33.234, -33.203, -33.179, -33.163, -33.154, -33.145,
-33.132, -33.111, -33.081, -33.041, -32.993, -32.937, -32.873,
-32.807, -32.74, -32.676, -32.611, -32.543, -32.468, -32.384,
-32.293, -32.199, -32.109, -32.029, -31.959, -31.899, -31.846,
-31.796, -31.749, -31.704, -31.659, -31.612, -31.561), Y = c(3.393,
3.396, 3.398, 3.402, 3.408, 3.419, 3.434, 3.452, 3.471, 3.489,
3.506, 3.518, 3.525, 3.526, 3.523, 3.518, 3.513, 3.511, 3.511,
3.513, 3.516, 3.517, 3.51, 3.494, 3.467, 3.434, 3.402, 3.376,
3.358, 3.348, 3.343, 3.343, 3.346, 3.351, 3.36, 3.373, 3.39,
3.408, 3.43, 3.453, 3.479, 3.506, 3.532, 3.555, 3.57, 6.684,
6.757, 6.823, 6.887, 6.953, 7.024, 7.099, 7.174, 7.245, 7.307,
7.363, 7.414, 7.466, 7.52, 7.579, 7.643, 7.71, 7.781, 7.853,
7.923, 7.988, 8.047, 8.098, 8.139, 8.173, 8.202, 8.228, 8.251,
8.27, 8.285, 8.298, 8.314, 8.337, 8.372, 8.423, 8.487, 8.558,
8.628, 8.688, 8.735, 8.768, 8.792, 8.814, 8.843, 8.881)), .Names = c("ID",
"Timestamp", "X", "Y"), row.names = c(NA, 90L), class = "data.frame")