我有以下資料集:
我一直在嘗試使用以下代碼構建列聯表:
library(readr)
library(tidyverse)
library(magrittr)
data1 %>%
select(blockLabel, trial_resp.corr, participant) %>%
group_by(blockLabel, trial_resp.corr, participant) %$%
with(., table(blockLabel, trial_resp.corr, participant))
, , participant = pilot01
trial_resp.corr
blockLabel 0 1
auditory_only 0 12
bimodal_focus_auditory 1 71
bimodal_focus_visual 3 69
divided 74 70
visual_only 0 12
, , participant = pilot02
trial_resp.corr
blockLabel 0 1
auditory_only 0 12
bimodal_focus_auditory 1 71
bimodal_focus_visual 2 70
divided 77 67
visual_only 11 1
, , participant = pilot03
trial_resp.corr
blockLabel 0 1
auditory_only 1 11
bimodal_focus_auditory 1 71
bimodal_focus_visual 3 69
divided 75 69
visual_only 0 12
我想做的是添加另一列,其值(低于 1 和 0)以百分比轉換,最后是總計。
我不知道是否可行,但如果不是,請建議一些迭代方式(do.call()、apply()、map()、for 回圈),如果第一種方式不可行。
我使用了以下解決方案,但我不知道如何繼續
data1%>% select(blockLabel, trial_resp.corr, participant) %>%
group_by(blockLabel, trial_resp.corr, participant) %$%
as.data.frame(with(., table(blockLabel, trial_resp.corr, participant))) %>%
mutate(freq = Freq / sum(Freq)) %>%
group_split(participant, trial_resp.corr)
[[1]]
# A tibble: 5 x 5
blockLabel trial_resp.corr participant Freq freq
<fct> <fct> <fct> <int> <dbl>
1 auditory_only 0 pilot01 0 0
2 bimodal_focus_auditory 0 pilot01 1 0.00107
3 bimodal_focus_visual 0 pilot01 3 0.00321
4 divided 0 pilot01 74 0.0791
5 visual_only 0 pilot01 0 0
[[2]]
# A tibble: 5 x 5
blockLabel trial_resp.corr participant Freq freq
<fct> <fct> <fct> <int> <dbl>
1 auditory_only 1 pilot01 12 0.0128
2 bimodal_focus_auditory 1 pilot01 71 0.0759
3 bimodal_focus_visual 1 pilot01 69 0.0737
4 divided 1 pilot01 70 0.0748
5 visual_only 1 pilot01 12 0.0128
[[3]]
# A tibble: 5 x 5
blockLabel trial_resp.corr participant Freq freq
<fct> <fct> <fct> <int> <dbl>
1 auditory_only 0 pilot02 0 0
2 bimodal_focus_auditory 0 pilot02 1 0.00107
3 bimodal_focus_visual 0 pilot02 2 0.00214
4 divided 0 pilot02 77 0.0823
5 visual_only 0 pilot02 11 0.0118
[[4]]
# A tibble: 5 x 5
blockLabel trial_resp.corr participant Freq freq
<fct> <fct> <fct> <int> <dbl>
1 auditory_only 1 pilot02 12 0.0128
2 bimodal_focus_auditory 1 pilot02 71 0.0759
3 bimodal_focus_visual 1 pilot02 70 0.0748
4 divided 1 pilot02 67 0.0716
5 visual_only 1 pilot02 1 0.00107
[[5]]
# A tibble: 5 x 5
blockLabel trial_resp.corr participant Freq freq
<fct> <fct> <fct> <int> <dbl>
1 auditory_only 0 pilot03 1 0.00107
2 bimodal_focus_auditory 0 pilot03 1 0.00107
3 bimodal_focus_visual 0 pilot03 3 0.00321
4 divided 0 pilot03 75 0.0801
5 visual_only 0 pilot03 0 0
[[6]]
# A tibble: 5 x 5
blockLabel trial_resp.corr participant Freq freq
<fct> <fct> <fct> <int> <dbl>
1 auditory_only 1 pilot03 11 0.0118
2 bimodal_focus_auditory 1 pilot03 71 0.0759
3 bimodal_focus_visual 1 pilot03 69 0.0737
4 divided 1 pilot03 69 0.0737
5 visual_only 1 pilot03 12 0.0128
謝謝
uj5u.com熱心網友回復:
我們可以崩潰as.data.frame
data1 %>%
select(blockLabel, trial_resp.corr, participant) %>%
group_by(blockLabel, trial_resp.corr, participant) %$%
with(., table(blockLabel, trial_resp.corr, participant)) %>%
as.data.frame
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527000.html
標籤:rdplyr添加
上一篇:更快地填充矩陣
下一篇:提取最長匹配字串R
