I'm trying to calculate the minimum values of a numeric column for each level of a factor, while keeping values of another factor in the resulting data frame.
# dummy data
dat <- data.frame(
code = c("HH11", "HH45", "JL03", "JL03", "JL03", "HH11"),
index = c("023434", "3377477", "3388595", "3377477", "1177777", "023434"),
value = c(24.1, 37.2, 78.9, 45.9, 20.0, 34.6)
)
The result I want is the minimum of value
for each level of code
, keeping index
in the resulting data frame.
# result I want:
# code value index
# 1 HH11 24.1 023434
# 2 HH45 37.2 3377477
# 3 JL03 20.0 1177777
# ddply attempt
library(plyr)
ddply(dat, ~ code, summarise, val = min(value))
# code val
# 1 HH11 24.1
# 2 HH45 37.2
# 3 JL03 20.0
# base R attempt
aggregate(value ~ code, dat, min)
# code value
# 1 HH11 24.1
# 2 HH45 37.2
# 3 JL03 20.0
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…