0

データフレームから文を生成しようとしています 以下はデータフレームです

# Code
mycode <- c("AAABBB", "AAABBB", "AAACCC", "AAABBD")
mycode <- sample(mycode, 20, replace = TRUE)

# Date
mydate <-c("2016-10-17","2016-10-18","2016-10-19","2016-10-20")
mydate <-sample(mydate, 20, replace = TRUE)

# resort
myresort <-c("GB","IE","GR","DK")
myresort <-sample(myresort, 20, replace = TRUE)

# Number of holidaymakers
HolidayMakers <- sample(1000, 20, replace = TRUE)

mydf <- data.frame(mycode,
                  mydate,
                  myresort,
                  HolidayMakers)

例として、「コードの場合、最大の目的地は、合計で最も多くの訪問日があった場所です」mycodeのような文を作成したいと思います。mycodemyresortsmydateHolidayMakers

コードごとに複数の行があると仮定すると。mydate私が欲しいのは、たとえばandごとに 1 つの文を含める代わりに、次のmyresortようなことを言いたい1 つの文です。

「コード AAABBB の場合、最大の目的地は GB、GR、DK、IE であり、訪問のトップ日は 2016-10-17,2016-10-18,2016-10-19 で、合計 650 でした」

650 は、基本的に、mycode ごとに、その日のすべての国のすべてのホリデー メーカーの合計になります。

誰か助けて?

お時間をいただきありがとうございます

4

1 に答える 1

2

あなたは試すことができます:

library(dplyr)
res <- mydf %>%
  group_by(mycode) %>%
  summarise(d = toString(unique(mydate)), 
            r = toString(unique(myresort)), 
            h = sum(HolidayMakers)) %>%
  mutate(s = paste("For the code", mycode, 
                   "the biggest destinations are", r, 
                   "where the top days of visiting were", d, 
                   "with a total of", h))

これにより、次のことが得られます。

> res$s

#[1] "For the code AAABBB the biggest destinations are GB, GR, IE, DK 
#     where the top days of visiting were 2016-10-17, 2016-10-18, 
#     2016-10-20, 2016-10-19 with a total of 6577"
#[2] "For the code AAABBD the biggest destinations are IE 
#     where the top days of visiting were 2016-10-17, 2016-10-18 
#     with a total of 1925"                                    
#[3] "For the code AAACCC the biggest destinations are IE, GR, DK 
#     where the top days of visiting were 2016-10-20, 2016-10-17, 
#     2016-10-19, 2016-10-18 with a total of 2878"    

: 「上位訪問日」の計算方法に関するガイダンスが提供されていないため、単純にすべての日を含めました。実際の状況に合わせて上記を簡単に編集できます。

于 2016-10-26T11:06:11.647 に答える