您可以使用“which”作为以下代码:
df <- data.frame(cbind(Date = '1/2/2010', Time = '5pm', Item = 'Car', Value = 50000))
new <- data.frame(cbind(Date = '1/3/2010', Time = '6am', Item = 'keys', Value = 100))
df <- rbind(df, new)
searchIndex1 <- function(dd, tt, itm, val){
which(df$Date==dd & df$Time== tt & df$Item ==itm & df$Value == val)
}
searchIndex1(dd='1/3/2010', tt='6am', itm='keys', val=100)
它将返回索引号 2。
或者你可以使用“过滤器”:
searchIndex2 <- function(dd, tt, itm, val){
df.with.index <- mutate(df, IDX = 1:n())
result <- filter(df.with.index,(Date==dd & Time== tt & Item ==itm & Value == val))$IDX
}
searchIndex2(dd='1/2/2010', tt='5pm', itm='Car', val=50000)
它将返回索引 1。