分析整个数据帧而不是r中的单独子集

时间:2013-03-19 13:10:17

标签: r subset

我有代码在运动中分析Gforces,随着时间的推移返回给我一个GForces的情节。该脚本适用于数据的子集(按ID分类),我不知道如何使其兼容一次分析整个数据帧,并将结果返回到分面图(例如ggplot)。有谁知道如何做到这一点,或者我注定要一次用一个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")

1 个答案:

答案 0 :(得分:4)

我加入@Arun,你需要解释一下你在做什么。代码并没有什么帮助,特别是如果写得不好并且有一些错误。

据说,如果我查看你的代码,你会按ID进行处理。一个想法是将您的代码放在一个函数中,并使用lapply为每个ID调用它。

您可以按ID创建3个绘图,因此您可以创建矩阵布局,在其中绘制每行中的3个绘图。

ids <- unique(positions$ID)
layout(matrix(1:(length(ids)*3),ncol=3,byrow=TRUE)) 

然后,您为每个ID调用您的流程,例如:

lapply(ids,function(myID){
  number1 <-subset(positions,subset=(ID==myID))
  .....

  # GForce calculation
  plot(number1$Timestamp,GForce)    ## I change one line 

   ...
  sum(GForce[3:max])
})

enter image description here