我有两列每小时,门槛,车。我想写一些代码,当阈值的第一个实例是1时,返回car列的MPH。
MPH Threshold Car
30 0 A
31 0 A
32 1 A
33 1 A
34 1 A
35 1 A
30 0 B
31 0 B
32 0 B
33 0 B
34 1 B
35 1 B
所需输出:
Value Car
32 A
34 B
发布于 2019-06-06 07:58:23
假设您总是至少有一个值,其中我们可以为每个Car
设置Threshold = 1
library(dplyr)
df %>%
group_by(Car) %>%
slice(which.max(Threshold == 1)) %>%
select(-Threshold)
# MPH Car
# <int> <fct>
#1 32 A
#2 34 B
使用base R ave
df[with(df, ave(Threshold == 1, Car, FUN = function(x)
seq_along(x) == which.max(x))), ]
发布于 2019-06-06 10:45:17
你也可以这样做
library(dplyr)
df %>%
filter(Threshold == 1) %>%
subset(!duplicated(Car))
library(data.table)
dt <- data.table(df)
dt[Threshold == 1, ][!duplicated(Car),]
发布于 2019-06-06 10:43:36
data.table
的一个选项
library(data.table)
i1 <- setDT(df)[, .I[which(Threshold == 1)[1]], Car]$V1
df[i1, .(Value = MPH, Car)]
# Value Car
#1: 32 A
#2: 34 B
数据
df <- structure(list(MPH = c(30L, 31L, 32L, 33L, 34L, 35L, 30L, 31L,
32L, 33L, 34L, 35L), Threshold = c(0L, 0L, 1L, 1L, 1L, 1L, 0L,
0L, 0L, 0L, 1L, 1L), Car = c("A", "A", "A", "A", "A", "A", "B",
"B", "B", "B", "B", "B")), class = "data.frame", row.names = c(NA,
-12L))
https://stackoverflow.com/questions/56469036
复制相似问题