开始使用免费开始使用

使用 .SD(I)

.SD 搭配 .SDcols 是非常强大的功能,可大幅简化对多列的计算。

  • .SD 是一个特殊符号,表示 Subset of Data(数据子集)。
  • .SDcols 指定应包含在 .SD 中的列。

本练习是课程的一部分

在 R 中使用 data.table 进行数据处理

查看课程

练习说明

  • 针对每个月,找到对应最短行程的行(在 duration 上使用 which.min())。
  • 结果应包含 monthstart_stationend_stationstart_dateend_dateduration 列。

交互式实操练习

通过完成这段示例代码来试试这个练习。

relevant_cols <- c("start_station", "end_station", 
                   "start_date", "end_date", "duration")

# Find the row corresponding to the shortest trip per month
shortest <- batrips[, ___, 
                    by = month(start_date), 
                    .SDcols = ___]
shortest
编辑并运行代码