Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

# Contingency Matrix から Listへの変換

Contingency Matrix(Table)を使っていて、一時的にListへ変換したいことがあります。

このContingecy Matrixから

cmat =

id A B C
1 F T F
2 F T F
3 F F T
3 F F F

または

id A B C
1 0.1 0.6 0.3
2 0.1 0.7 0.1
3 0.1 0.1 0.4
4 0 0 0

こういうリストへ

clist =

key value
1 B
2 B
3 C
4 A

# 一解法

reshapeやtidyrを使ってやるのが基本のようですが、論理値だけでなく、値のうち最大のものという条件などを考えると、以下のような感じにしてみました。

``````clist <- cmat %>% tidyr::gather(key, value, - id) %>%
group_by(id) %>%
top_n(n = 1) %>%
slice(1) %>%
arrange(id)
``````

うーん、違うな。

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?