简体   繁体   English

在 R 中使用 for 循环计算平均值

[英]Calculate the mean with a for loop in R

This is what I have to do: Using a for loop calculate and store the mean lap time for each bike.这就是我必须做的:使用 for 循环计算并存储每辆自行车的平均单圈时间。 The code below is how I tried to solve it.下面的代码是我试图解决它的方法。 It gives me an error about some unexpected curly brackets and I don't know why.它给了我关于一些意外大括号的错误,我不知道为什么。 I must use a for loop (no tidyverse or any other package).我必须使用 for 循环(没有 tidyverse 或任何其他包)。

racing <- data.frame(Bike=rep(c("A1", "A2", "A3"), times=4),
                     Lap=rep(c(1,2,3,4), each=3),
                     Time=c(71,70,67,73,72,66,68,74,69,68,68,70))
for(bike in racing){
  if([racing$Bike=="A1",]){
    time_mean <- mean(racing$Time)
  }
  else if([racing$Bike=="A2",]){
    time_mean1 <- mean(racing$Time)
  }
  else{
    time_mean2 <- mean(racing$Time)
  }
}
lap_mean <- c(time_mean, time_mean1, time_mean2)
    

Error: unexpected '}' in "    }"

This can be made much simpler in one line using the tapply function:这可以使用tapply函数在一行中变得更简单:

> (lapmean<-tapply(racing$Time, racing$Bike, mean))
A1 A2 A3 
70 71 68 

Update:更新:

I'm note sure why my post was voted down given that it requires way less typing and is more efficient than any other procedure posted here (22 times more efficient in comparison with the worst performing code).我很清楚为什么我的帖子被否决了,因为它需要更少的输入并且比这里发布的任何其他程序更有效(与性能最差的代码相比效率高 22 倍)。 I ran execution times of all the other code submitted and here are the results in order of best to worst performers in terms of elapsed time in seconds:我运行了提交的所有其他代码的执行时间,以下是按经过时间(以秒为单位)按最佳到最差执行顺序排列的结果:

  1. My Code StatsStudent - .01 elapsed sec time using tictoc package My Code StatsStudent - 使用 tictoc 包的时间为 .01 秒
  2. akrun (aggregate) - 0.03阿克伦(聚合) - 0.03
  3. Dan - 0.07丹 - 0.07
  4. akrun (for loop) - 0.11 akrun (for 循环) - 0.11
  5. Duck - 0.22鸭子 - 0.22

We can use aggregate from base R我们可以使用来自base R aggregate

aggregate(Time ~ Bike, data = racing, FUN = mean)

-output -输出

#   Bike Time
#1   A1   70
#2   A2   71
#3   A3   68

Or using for loop或者使用for循环

unb <- unique(racing$Bike)
out <- c()
for(bike in unb) {
     out <- c(out, mean(subset(racing, Bike == bike, select = Time)$Time))
 }

setNames(out, unb)
#   A1 A2 A3  
# 70 71 68 

Try this.尝试这个。 You can create a vector to store the bikes and then loop across it using subset() to filter the data and compute the mean time.您可以创建一个向量来存储自行车,然后使用subset()遍历它来过滤数据并计算平均时间。 Here the code:这里的代码:

#Data
racing <- data.frame(Bike=rep(c("A1", "A2", "A3"), times=4),
                     Lap=rep(c(1,2,3,4), each=3),
                     Time=c(71,70,67,73,72,66,68,74,69,68,68,70),stringsAsFactors = F)
#Bikes
vb <- unique(racing$Bike)
#Loop
vmeans <- numeric(length(vb))
for(i in 1:length(vb))
{
  #Data
  x <- subset(racing,Bike==vb[i])
  #Mean
  vmeans[i] <- mean(x$Time)
}
names(vmeans) <- vb

Output:输出:

vmeans
A1 A2 A3 
70 71 68 

Using base-R, I think the below is very instructional:使用 base-R,我认为以下内容很有指导意义:

results <- data.frame("Bike"=NA, "MeanTime"=NA)

for(i in unique(racing$Bike)) {
  tmp <- filter(racing, Bike == i)
  print(tmp) ;  print(mean(tmp$Time))
  
  results[match(i, unique(racing$Bike)),] <- c(i, mean(tmp$Time))
}
  print(results)

which gives the following:这给出了以下内容:

>   print(results)
  Bike MeanTime
1   A1       70
2   A2       71
3   A3       68

on top of printing what the loop is doing在打印循环正在做什么的基础上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM