我在R中使用mtcars数据集。我有一个数据帧列表(mtcars数据集分为多个柱面)。我需要:For循环数据帧列表
- 确定每个汽缸类型(即4,6,8)内汽车的最小值,以英里/加仑(mpg)为单位。
- 创建一个存储步骤1中找到的每辆汽车的马力(hp)值的矢量(矢量的长度为3)。
步骤I迄今进行的,如下所示:
# load the data
data(mtcars)
# split cars data.frame into a list of data frames by cylinder
cars <- split(mtcars, mtcars$cyl)
# find the position within each data frame for the min values of mpg (i.e. first
# column)
positions <- sapply(cars,function(x) which.min(x[,1]))
正如我看到的那样,下一个步骤将是使一个循环在每个数据帧以找到每个位置上的马力值。我试图为此做一个For循环,但是我一直无法使它工作。也许这个问题甚至有更好的解决方案。
请注意@bstockton在答案评论中指出了一些重要的内容。在8cyl类别中有两辆车共享最小值。 – 2014-08-29 06:39:44