3

不規則なデータの列を含むデータフレームがあります: 各トピックが文字列である「トピック」で、隣接するトピックは区切り文字 (この場合は「|」) で区切られています。

library(lubridate)
events <- data.frame(
  date  =dmy(c(     "12/6/2012",           "13/7/2012",    "4/8/2012")),
  days  =    c(               1,                     6,           0.5),
  name  =    c("Intro to stats", "Stats Winter school", "TidyR tools"),
  topics=    c( "probability|R", "R|regression|ggplot", "tidyR|dplyr"),
  stringsAsFactors=FALSE
  )

データフレームは次のeventsようになります。

        date days                name              topics
1 2012-06-12  1.0      Intro to stats       probability|R
2 2012-07-13  6.0 Stats Winter school R|regression|ggplot
3 2012-08-04  0.5         TidyR tools         tidyR|dplyr

このデータフレームを変換して、各行に 1 つのトピックと、そのトピックに費やした日数の指標が含まれるようにします。N トピックが D 日間にわたって提示された場合、各トピックに D/N 日が費やされたと仮定します。

私は急いでこれをしなければならなかったので、次のようにしました:

library(dplyr)

events %>%
  # Figure out how many topics were delivered at each event
  mutate(
    ntopics=sapply(
      gregexpr("|", topics, fixed=TRUE),
      function(x)(1 + sum(attr(x, "match.length") > 0 ))
      )
    ) %>%
  # Create a data frame with one topic per row
  do(data.frame(
    date    =rep(   .$date, .$ntopics),
    days    =rep(   .$days, .$ntopics),
    name    =rep(   .$name, .$ntopics),
    ntopics =rep(.$ntopics, .$ntopics),
    topic   =unlist(strsplit(.$topics, "|", fixed=TRUE)),
    stringsAsFactors=FALSE
    )) %>%
  # Estimate roughly how many days were spent on each topic
  mutate(daysPerTopic=days/ntopics)

それは私たちに与えます

        date days                name ntopics       topic daysPerTopic
1 2012-06-12  1.0      Intro to stats       2 probability         0.50
2 2012-06-12  1.0      Intro to stats       2           R         0.50
3 2012-07-13  6.0 Stats Winter school       3           R         2.00
4 2012-07-13  6.0 Stats Winter school       3  regression         2.00
5 2012-07-13  6.0 Stats Winter school       3      ggplot         2.00
6 2012-08-04  0.5         TidyR tools       2       tidyR         0.25
7 2012-08-04  0.5         TidyR tools       2       dplyr         0.25

これをよりエレガントに実現する方法を知りたいです。

4

2 に答える 2