2

我有以下data.frame:

authors <- data.frame(
    surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
    nationality = c("US", "Australia", "US", "UK", "Australia"),
    deceased = c("yes", rep("no", 3),"noinfo"))

产生这个输出:

      surname nationality deceased
1    Tukey          US      yes
2 Venables   Australia       no
3  Tierney          US       no
4   Ripley          UK       no
5   McNeil   Australia   noinfo

我想要做的是获得deceasedby的频率nationality。产生这个输出:

US yes 1
US no  1
US noinfo 0
Australia yes 0
Australia no 1
Australia noinfo 1
UK yes 0
UK no 1
UK noinfo 0

目前我只能通过tables.

stat <- table(authors)

我不知道如何通过访问表格的元素来进行。建议将不胜感激。

4

1 回答 1

3

你需要table在你想要发生的事情上......

table( authors[ c("nationality" , "deceased" ) ] )
#           deceased
#nationality no noinfo yes
#  Australia  1      1   0
#  UK         1      0   0
#  US         1      0   1

并获得你想要的确切输出......把它变成data.frame......

data.frame( table( authors[ c("nationality" , "deceased" ) ] ) )
#  nationality deceased Freq
#1   Australia       no    1
#2          UK       no    1
#3          US       no    1
#4   Australia   noinfo    1
#5          UK   noinfo    0
#6          US   noinfo    0
#7   Australia      yes    0
#8          UK      yes    0
#9          US      yes    1
于 2013-08-22T09:46:20.180 回答