r 在分组数据框中用“/”分隔数字

r Separate numbers with "/" in grouped data frame

我有一个类似于以下示例的大型数据框:

假设有一个球类游戏,您可以将球扔到目标中并获得分数。有 A 队和 B 队,每个人都可以决定要扔多少次球(所以这是不平等的)。它看起来像这样:

df<-data.frame("ID" = as.factor(c(1,2,3,4,5,6,7,8,9,10)),
               "Group" = c("A","A","A","A","A","B","B","B","B","B"),
               "Number_of_hits" = c(1,2,5,3,3,1,2,1,1,3),
               "Score_per_hit"=c("60","23.4/18.1","30/34.1/40/10.2/15.5","12/15.0/40","40/23.1/22","20.5","64.8/12.2","50","30","20/43.3/65"))

   ID Group Number_of_hits        Score_per_hit
   1     A              1                   60
   2     A              2            23.4/18.1
   3     A              5 30/34.1/40/10.2/15.5
   4     A              3           12/15.0/40
   5     A              3           40/23.1/22
   6     B              1                 20.5
   7     B              2            64.8/12.2
   8     B              1                   50
   9     B              1                   30
  10     B              3           20/43.3/65

现在,我想以一种可以对评分做一些事情的方式进行更改,所以我希望它是这样的:

   ID Group Score_per_hit
   1     A          60.0
   2     A          23.4
   2     A          18.1
   3     A          30.0
   3     A          34.1
   3     A          40.0
   3     A          10.2
   3     A          15.5
   4     A          12.0
   4     A          15.0
   4     A          40.0
   5     A          40.0
   5     A          23.1
   5     A          22.0
etc..

我有 seen/tried 一些关于 str_extract 的东西,但我只得到错误.. 有人可以帮助我吗?非常感谢!

可能有更优雅的方法,但这里有一个使用 for 循环的选项:

df2 <- vector("list", nrow(df))
for(i in seq(df2)){
  df2[[i]] <- expand.grid(
    ID = df$ID[i], 
    Group=df$Group[i], 
    Score_per_hit = as.numeric(
      strsplit(as.character(df$Score_per_hit[i]), "/", fixed = TRUE)[[1]])
  )
}
df2 <- do.call("rbind", df2)
df2