【发布时间】:2021-06-02 07:48:44
【问题描述】:
就像我有一个数据集 enter image description here
那么我怎样才能找到第一局的最高得分
【问题讨论】:
-
max(df[df$innings=="1",]$score)。假设df是您的数据集。
标签: r
就像我有一个数据集 enter image description here
那么我怎样才能找到第一局的最高得分
【问题讨论】:
max(df[df$innings=="1",]$score)。假设 df 是您的数据集。
标签: r
你可以的
df <- data.frame(innings=c(1,1,2,2,1), score = c(170,189,230,190,210))
inningsScoreSplit <- split(df$score,df$innings)
maxScores <- data.frame(
inning = names(inningsScoreSplit),
maxScore = sapply(inningsScoreSplit,max)
)
得到
> maxScores
inning maxScore
1 1 210
2 2 230
【讨论】:
这是学习 tidyverse 的好机会。 基本上你可以用一行代码做到这一点:
# This make your dataset
df <- data.frame(innings=c(1,1,2,2,1), score = c(170,189,230,190,210))
# This loads up the library tidyverse.
# If this gives you an error you might need to install tidyverse by using "install.packages("tidyverse")"
library(tidyverse)
# This calculates the max. There are 2 commands here: filter and summarise.
Result <- df %>% filter(innings == 1) %>% summarise(max(score))
【讨论】: