0

私にはかなり長くて(私にとって)複雑な質問があります。私は欧州連合理事会からの投票データを持っています。ここでは、各国の投票行動が名義尺度に従ってコード化されています。

0: yes
1: no
2: Abstention
3: no with a negative statement
4: Abstention with a negative statement
5: yes with a negative statement

データは次の形式です(データセットからの20の観測値のダンプについては、投稿の最後を参照してください)。

Country1 Country2 Country3 ... Date
1        0        0        ... 2004-12-12
1        2        0        ... 2003-02-14
2        0        1        ... 2004-05-22
...      ...      ...      ... ...

まず、データを月ごとに集計したいと思います。ここで、月ごとに、国ごとに0、1、2などがいくつあったかを合計します。理想的には、データは次のようになります。

Month    Country   sum of 0s    sum of 1s   Sum of 2s
January  Country1  2            0           1  
January  Country2  4            0           0
...      ...       ...          ...         ...

これが完了したら、データを次のようなパネル形式にしたいと思います。

Country   Month    sum of 0s   sum of 1s   sum of 2s
Country1  January  2           0           1 
Country1  February 0           1           3
...       ...      ...         ...         ...
Country2  January  4           0           0
Country2  February 2           2           0
...       ...      ...         ...         ...

これが非常に時間のかかる質問である場合は申し訳ありませんが、目的の結果を得ることができずに、さまざまな適用関数によって、さまざまな適用関数を使って、ずっと遊んでいます。どんな助けでも大歓迎です!

データセットからの20の観測(dput()関数からの出力):

    structure(list(Recitals = c(29L, 13L, 2L, 20L, 10L, 18L, 29L, 
6L, 4L, 16L, 7L, 6L, 12L, 23L, 6L, 10L, 2L, 6L, 9L, 8L, 7L), 
    Voting_Rule = structure(c(4L, 4L, 5L, 5L, 5L, 5L, 5L, 4L, 
    4L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 4L), .Label = c("0", 
    "Qualified Majority", "Simple Majority", "Unanimity", "Qualified majority", 
    "Simple majority"), class = "factor"), Belgium = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), Denmark = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Czech.Republic = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Germany = c(0L, 
    0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Estonia = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Greece = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Spain = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), France = c(0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Ireland = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Italy = c(0L, 0L, 0L, 0L, 0L, 0L, 5L, 
    0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Cyprus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Latvia = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), Lithuania = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Luxembourg = c(0L, 
    0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Hungary = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Malta = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Netherlands = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Austria = c(0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Poland = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Portugal = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Slovenia = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Slovakia = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Finland = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Sweden = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    UK = c(0L, 0L, 0L, 0L, 0L, 0L, 5L, 0L, 0L, 0L, 5L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Dates = structure(c(12716, 
    12716, 12716, 12674, 12674, 12698, 12705, 12724, 12738, 12738, 
    12716, 12741, 12744, 12754, 12754, 12758, 12758, 12758, 12759, 
    12759, 12759), class = "Date")), .Names = c("Recitals", "Voting_Rule", 
"Belgium", "Denmark", "Czech.Republic", "Germany", "Estonia", 
"Greece", "Spain", "France", "Ireland", "Italy", "Cyprus", "Latvia", 
"Lithuania", "Luxembourg", "Hungary", "Malta", "Netherlands", 
"Austria", "Poland", "Portugal", "Slovenia", "Slovakia", "Finland", 
"Sweden", "UK", "Dates"), row.names = c(752L, 753L, 762L, 774L, 
775L, 776L, 777L, 780L, 789L, 790L, 793L, 794L, 797L, 816L, 817L, 
818L, 819L, 820L, 824L, 825L, 826L), class = "data.frame")
4

1 に答える 1

3

Hadleyのreshape2およびplyrパッケージは、作業に大いに役立ちます。

library(reshape2)

# load your data into variable d1
# d1 <- struct( ... your data ...)

# first, melt the data.
# molten data is very very useful for further transformation.
d2 <- reshape2:::melt.data.frame(d1[,-(1:2)], # drop unused variables
                                 id.vars="Dates",
                                 variable.name="Country")

# create a variable "Month" from Date
d2$Month <- months(d2$Date)

# cast the data to the desired format using appropriate function (here, length())
d3 <- dcast(d2, Country+Month~value, length)

# finally change the names of newly created variables
names(d3)[-(1:2)] <- sprintf("sum of %ss", names(d3)[-(1:2)])
于 2010-12-30T23:17:10.380 に答える