0

以下のような名前の.csvテーブルがありmailing.csvます。受信者、件名、送信者で構成されます。

       Receiver                                subject       sender
1   Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
2   Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
3   Adrian Cole    RE: [WHIRR-117] Composable services  Adrian Cole
4   Adrian Cole    RE: [WHIRR-117] Composable services  Adrian Cole
5   Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
6   Adrian Cole    RE: [WHIRR-117] Composable services  Adrian Cole
7   Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
8   Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
9   Adrian Cole    RE: [WHIRR-117] Composable services  Adrian Cole
10  Adrian Cole    RE: [WHIRR-117] Composable services  Adrian Cole
11  Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
12  Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
13  Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
14  Adrian Cole    RE: [WHIRR-117] Composable services    Tom White
15 Patrick Hunt RE: [WHIRR-123] Cassandra integration     Tom White
16 Patrick Hunt RE: [WHIRR-123] Cassandra integration   Andrei Savu
17 Patrick Hunt RE: [WHIRR-123] Cassandra integration   Andrei Savu
18 Patrick Hunt RE: [WHIRR-123] Cassandra integration     Tom White
19 Patrick Hunt RE: [WHIRR-123] Cassandra integration     Tom White
20 Patrick Hunt RE: [WHIRR-123] Cassandra integration   Adrian Cole
21 Patrick Hunt RE: [WHIRR-123] Cassandra integration     Tom White
22 Patrick Hunt RE: [WHIRR-123] Cassandra integration  Patrick Hunt

私がやりたいのは、上記の表の情報を.csvテンプレート(名前付きAC_template.csv)に更新/マッピングし、ファイル名としてバックレットの件名の詳細を使用して別のファイルに保存することです(たとえばAC_WHIRR-117)。上記の表の場合、asAC_WHIRR-117と。という名前の2つの新しいファイルを作成する必要がありますAC_WHIRR-123

サンプルの.csvテンプレート(AC_template.csv)は次のとおりです。

                Adrian.Cole Patrick.Hunt Andrei.Savu Bruno.Dumon Edward.J..Yoon Eugene.Koontz Jakob.Homan Kelvin.Kakugawa Tom.White
Adrian Cole               0            0           0           0              0             0           0               0         0
Patrick Hunt              0            0           0           0              0             0           0               0         0
Andrei Savu               0            0           0           0              0             0           0               0         0
Bruno Dumon               0            0           0           0              0             0           0               0         0
Edward J. Yoon            0            0           0           0              0             0           0               0         0
Eugene Koontz             0            0           0           0              0             0           0               0         0
Jakob Homan               0            0           0           0              0             0           0               0         0
Kelvin Kakugawa           0            0           0           0              0             0           0               0         0
Tom White                 0            0           0           0              0             0           0               0         0
Lars George               0            0           0           0              0             0           0               0         0
Soren Macbeth             0            0           0           0              0             0           0               0         0
                Lars.George Soren.Macbeth
Adrian Cole               0             0
Patrick Hunt              0             0
Andrei Savu               0             0
Bruno Dumon               0             0
Edward J. Yoon            0             0
Eugene Koontz             0             0
Jakob Homan               0             0
Kelvin Kakugawa           0             0
Tom White                 0             0
Lars George               0             0
Soren Macbeth             0             0

この質問の出力例は次のとおりです。

AC_WHIRR-117のサンプル出力:

                Adrian.Cole Patrick.Hunt Andrei.Savu Bruno.Dumon Edward.J..Yoon Eugene.Koontz Jakob.Homan Kelvin.Kakugawa Tom.White
Adrian Cole               0            0           0           0              0             0           0               0         9
Patrick Hunt              0            0           0           0              0             0           0               0         0
Andrei Savu               0            0           0           0              0             0           0               0         0
Bruno Dumon               0            0           0           0              0             0           0               0         0
Edward J. Yoon            0            0           0           0              0             0           0               0         0
Eugene Koontz             0            0           0           0              0             0           0               0         0
Jakob Homan               0            0           0           0              0             0           0               0         0
Kelvin Kakugawa           0            0           0           0              0             0           0               0         0
Tom White                 9            0           0           0              0             0           0               0         0
Lars George               0            0           0           0              0             0           0               0         0
Soren Macbeth             0            0           0           0              0             0           0               0         0
                Lars.George Soren.Macbeth
Adrian Cole               0             0
Patrick Hunt              0             0
Andrei Savu               0             0
Bruno Dumon               0             0
Edward J. Yoon            0             0
Eugene Koontz             0             0
Jakob Homan               0             0
Kelvin Kakugawa           0             0
Tom White                 0             0
Lars George               0             0
Soren Macbeth             0             0

AC_WHIRR-123のサンプル出力

               Adrian.Cole Patrick.Hunt Andrei.Savu Bruno.Dumon Edward.J..Yoon Eugene.Koontz Jakob.Homan Kelvin.Kakugawa Tom.White
Adrian Cole               0            1           0           0              0             0           0               0         0
Patrick Hunt              1            0           2           0              0             0           0               0         4
Andrei Savu               0            2           0           0              0             0           0               0         0
Bruno Dumon               0            0           0           0              0             0           0               0         0
Edward J. Yoon            0            0           0           0              0             0           0               0         0
Eugene Koontz             0            0           0           0              0             0           0               0         0
Jakob Homan               0            0           0           0              0             0           0               0         0
Kelvin Kakugawa           0            0           0           0              0             0           0               0         0
Tom White                 0            4           0           0              0             0           0               0         0
Lars George               0            0           0           0              0             0           0               0         0
Soren Macbeth             0            0           0           0              0             0           0               0         0
                Lars.George Soren.Macbeth
Adrian Cole               0             0
Patrick Hunt              0             0
Andrei Savu               0             0
Bruno Dumon               0             0
Edward J. Yoon            0             0
Eugene Koontz             0             0
Jakob Homan               0             0
Kelvin Kakugawa           0             0
Tom White                 0             0
Lars George               0             0
Soren Macbeth             0             0

以下のようにdput(head)mailing.csv

structure(list(Receiver = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Adrian Cole", 
"Patrick Hunt"), class = "factor"), subject = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("RE: [WHIRR-117] Composable services", 
"RE: [WHIRR-123] Cassandra integration "), class = "factor"), 
    sender = structure(c(4L, 4L, 1L, 1L, 4L, 1L), .Label = c("Adrian Cole", 
    "Andrei Savu", "Patrick Hunt", "Tom White"), class = "factor")), .Names = c("Receiver", 
"subject", "sender"), row.names = c(NA, 6L), class = "data.frame")

dput(head)for AC_template.csv

structure(list(Adrian.Cole = c(0L, 0L, 0L, 0L, 0L, 0L), Patrick.Hunt = c(0L, 
0L, 0L, 0L, 0L, 0L), Andrei.Savu = c(0L, 0L, 0L, 0L, 0L, 0L), 
    Bruno.Dumon = c(0L, 0L, 0L, 0L, 0L, 0L), Edward.J..Yoon = c(0L, 
    0L, 0L, 0L, 0L, 0L), Eugene.Koontz = c(0L, 0L, 0L, 0L, 0L, 
    0L), Jakob.Homan = c(0L, 0L, 0L, 0L, 0L, 0L), Kelvin.Kakugawa = c(0L, 
    0L, 0L, 0L, 0L, 0L), Tom.White = c(0L, 0L, 0L, 0L, 0L, 0L
    ), Lars.George = c(0L, 0L, 0L, 0L, 0L, 0L), Soren.Macbeth = c(0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("Adrian.Cole", "Patrick.Hunt", 
"Andrei.Savu", "Bruno.Dumon", "Edward.J..Yoon", "Eugene.Koontz", 
"Jakob.Homan", "Kelvin.Kakugawa", "Tom.White", "Lars.George", 
"Soren.Macbeth"), row.names = c("Adrian Cole", "Patrick Hunt", 
"Andrei Savu", "Bruno Dumon", "Edward J. Yoon", "Eugene Koontz"
), class = "data.frame")

WHIRR-117の出力例は次のとおりです。

structure(list(Adrian.Cole = c(0L, 0L, 0L, 0L, 0L, 0L), Patrick.Hunt = c(0L, 
0L, 0L, 0L, 0L, 0L), Andrei.Savu = c(0L, 0L, 0L, 0L, 0L, 0L), 
    Bruno.Dumon = c(0L, 0L, 0L, 0L, 0L, 0L), Edward.J..Yoon = c(0L, 
    0L, 0L, 0L, 0L, 0L), Eugene.Koontz = c(0L, 0L, 0L, 0L, 0L, 
    0L), Jakob.Homan = c(0L, 0L, 0L, 0L, 0L, 0L), Kelvin.Kakugawa = c(0L, 
    0L, 0L, 0L, 0L, 0L), Tom.White = c(9L, 0L, 0L, 0L, 0L, 0L
    ), Lars.George = c(0L, 0L, 0L, 0L, 0L, 0L), Soren.Macbeth = c(0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("Adrian.Cole", "Patrick.Hunt", 
"Andrei.Savu", "Bruno.Dumon", "Edward.J..Yoon", "Eugene.Koontz", 
"Jakob.Homan", "Kelvin.Kakugawa", "Tom.White", "Lars.George", 
"Soren.Macbeth"), row.names = c("Adrian Cole", "Patrick Hunt", 
"Andrei Savu", "Bruno Dumon", "Edward J. Yoon", "Eugene Koontz"
), class = "data.frame")

WHIRR-123の出力例は次のとおりです。

structure(list(Adrian.Cole = c(0L, 1L, 0L, 0L, 0L, 0L), Patrick.Hunt = c(1L, 
0L, 2L, 0L, 0L, 0L), Andrei.Savu = c(0L, 2L, 0L, 0L, 0L, 0L), 
    Bruno.Dumon = c(0L, 0L, 0L, 0L, 0L, 0L), Edward.J..Yoon = c(0L, 
    0L, 0L, 0L, 0L, 0L), Eugene.Koontz = c(0L, 0L, 0L, 0L, 0L, 
    0L), Jakob.Homan = c(0L, 0L, 0L, 0L, 0L, 0L), Kelvin.Kakugawa = c(0L, 
    0L, 0L, 0L, 0L, 0L), Tom.White = c(0L, 4L, 0L, 0L, 0L, 0L
    ), Lars.George = c(0L, 0L, 0L, 0L, 0L, 0L), Soren.Macbeth = c(0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("Adrian.Cole", "Patrick.Hunt", 
"Andrei.Savu", "Bruno.Dumon", "Edward.J..Yoon", "Eugene.Koontz", 
"Jakob.Homan", "Kelvin.Kakugawa", "Tom.White", "Lars.George", 
"Soren.Macbeth"), row.names = c("Adrian Cole", "Patrick Hunt", 
"Andrei Savu", "Bruno Dumon", "Edward J. Yoon", "Eugene Koontz"
), class = "data.frame")

専門家からの助けに感謝します...

4

1 に答える 1

2

plyr基本関数でのパッケージの使用table。組み立てが必要な場合があります。これにより、ほとんどの方法でそこに到達するはずです。

#load template
template <- structure(list(Adrian.Cole = c(0L, 0L, 0L, 0L, 0L, 0L), Patrick.Hunt = c(0L, 
0L, 0L, 0L, 0L, 0L), Andrei.Savu = c(0L, 0L, 0L, 0L, 0L, 0L), 
    Bruno.Dumon = c(0L, 0L, 0L, 0L, 0L, 0L), Edward.J..Yoon = c(0L, 
    0L, 0L, 0L, 0L, 0L), Eugene.Koontz = c(0L, 0L, 0L, 0L, 0L, 
    0L), Jakob.Homan = c(0L, 0L, 0L, 0L, 0L, 0L), Kelvin.Kakugawa = c(0L, 
    0L, 0L, 0L, 0L, 0L), Tom.White = c(0L, 0L, 0L, 0L, 0L, 0L
    ), Lars.George = c(0L, 0L, 0L, 0L, 0L, 0L), Soren.Macbeth = c(0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("Adrian.Cole", "Patrick.Hunt", 
"Andrei.Savu", "Bruno.Dumon", "Edward.J..Yoon", "Eugene.Koontz", 
"Jakob.Homan", "Kelvin.Kakugawa", "Tom.White", "Lars.George", 
"Soren.Macbeth"), row.names = c("Adrian Cole", "Patrick Hunt", 
"Andrei Savu", "Bruno Dumon", "Edward J. Yoon", "Eugene Koontz"
), class = "data.frame")
#the rownames of this data frame hold the names of senders/receivers 
#that we are interested in
names.to.search <- rownames(template)

#load data frame
mailing <- structure(list(Receiver = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
    .Label = c("Adrian Cole", "Patrick Hunt"), class = "factor"), 
    subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
    .Label = c("RE: [WHIRR-117] Composable services", 
    "RE: [WHIRR-123] Cassandra integration "), class = "factor"), 
  sender = structure(c(4L, 4L, 1L, 1L, 4L, 1L), .Label = c("Adrian Cole", 
  "Andrei Savu", "Patrick Hunt", "Tom White"), class = "factor")), .Names = c("Receiver", 
    "subject", "sender"), row.names = c(NA, 6L), class = "data.frame")
names(mailing) <- tolower(names(mailing))
#get topic to sort by
mailing$topic <- gsub(".*\\[(.*)\\].*","\\1",mailing$subject)
#restrict to rows that have sender and receiver in names list
mailing <- mailing[mailing$receiver %in% names.to.search & 
    mailing$sender %in% names.to.search,]
library(plyr)
fn <- function(x) {
    with(x, {
        #add NA-name and name-NA to the sender and receiver lists 
        #so that the resulting table is of the right dimension
        receiver <- append(as.character(receiver), 
            c(names.to.search, rep(NA,times=length(names.to.search))))
        sender <- append(as.character(sender), 
            c(rep(NA,times=length(names.to.search)),names.to.search))
        #create the table
        y <- table(receiver,sender)
        #write table to csv file
        write.csv(y,file=paste0("AC_",topic[1],".csv"))
    })
}
#perform fn on each section of data frame by topic
d_ply(mailing,.(topic),fn)
于 2012-09-25T04:57:47.677 に答える