我有以下小標題
structure(list(blockLabel = structure(c(1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("auditory_only",
"bimodal_focus_auditory", "bimodal_focus_visual", "divided",
"visual_only"), class = "factor"), trial_resp.corr = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("0",
"1"), class = "factor"), participant = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("pilot01",
"pilot02", "pilot03"), class = "factor"), Freq = c(0L, 1L, 3L,
74L, 0L, 12L, 71L, 69L, 70L, 12L, 0L, 1L, 2L, 77L, 11L, 12L,
71L, 70L, 67L, 1L, 1L, 1L, 3L, 75L, 0L, 11L, 71L, 69L, 69L, 12L
), tc = c(12, 72, 72, 144, 12, 12, 72, 72, 144, 12, 12, 72, 72,
144, 12, 12, 72, 72, 144, 12, 12, 72, 72, 144, 12, 12, 72, 72,
144, 12), freq = c(0, 1.38888888888889, 4.16666666666667, 51.3888888888889,
0, 100, 98.6111111111111, 95.8333333333333, 48.6111111111111,
100, 0, 1.38888888888889, 2.77777777777778, 53.4722222222222,
91.6666666666667, 100, 98.6111111111111, 97.2222222222222, 46.5277777777778,
8.33333333333333, 8.33333333333333, 1.38888888888889, 4.16666666666667,
52.0833333333333, 0, 91.6666666666667, 98.6111111111111, 95.8333333333333,
47.9166666666667, 100)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -30L))
我想根據參與者變數的級別創建三個不同的表。最終結果或多或少應該如下所示:

我已經開始撰寫以下代碼的腳本(因為我正在尋找通過 dplyr、應用系列、回圈或映射函式來執行此操作)
list %>% as_data_frame() %>%
select(blockLabel, trial_resp.corr, participant, Freq, freq) %>%
map(~ flextable(.x))
但不幸的是,我收到以下錯誤代碼:
Error in flextable(.x) : is.data.frame(data) is not TRUE
我不是這種方法的專家,因此如果您對解決問題和分享知識以實作最終結果有什么建議,請告訴我(順便說一下,我指定 Trial_resp 的正確性對應于 1 和不正確性對應于 0 .corr 變數)
謝謝
uj5u.com熱心網友回復:
這是一種方法,我們將 'trial_resp.corr' 值修改為 'Incorrectness'、'Correctness' 基于 0、1 值,然后select是感興趣的列,split由 'participant' 列創建一個list資料集,回圈listwith map, 將資料從 long 調整為 Wide withpivot_wider然后轉換為flextable
library(dplyr)
library(tidyr)
library(purrr)
library(flextable)
library(stringr)
library(janitor)
lst1 <- df1 %>%
mutate( trial_resp.corr = recode(trial_resp.corr,
'0'= 'Incorrectness', '1' = "Correctness")) %>%
select(blockLabel, trial_resp.corr, participant, Freq, `Freq%` = freq) %>%
split(.$participant) %>%
map(~ .x %>%
select(-participant) %>%
pivot_wider(names_from = trial_resp.corr,
values_from = c(Freq, `Freq%`), values_fill = 0,
names_glue ="{trial_resp.corr}{str_remove(.value, 'Freq' )}") %>%
adorn_totals() %>%
mutate(across(ends_with("%"),
~ replace(.x, n(), 100 *get(str_remove(cur_column(),
fixed("%")))[n()]/sum(Incorrectness[n()], Correctness[n()])))) %>%
flextable)
如果我們想在全域環境中創建物件
list2env(lst1, .GlobalEnv)
-輸出
lst1[[1]]

uj5u.com熱心網友回復:
我會提出以下建議(對于 dplyr):
library(dplyr)
dfs <- list()
for (prt in levels(df$participant)){
dfs[[prt]] <- df %>% filter(participant == prt) %>% select(-participant)
}
您將獲得三個表以進行任何所需的進一步操作:
> dfs
$pilot01
# A tibble: 10 × 5
blockLabel trial_resp.corr Freq tc freq
<fct> <fct> <int> <dbl> <dbl>
1 auditory_only 0 0 12 0
2 bimodal_focus_auditory 0 1 72 1.39
3 bimodal_focus_visual 0 3 72 4.17
4 divided 0 74 144 51.4
5 visual_only 0 0 12 0
6 auditory_only 1 12 12 100
7 bimodal_focus_auditory 1 71 72 98.6
8 bimodal_focus_visual 1 69 72 95.8
9 divided 1 70 144 48.6
10 visual_only 1 12 12 100
$pilot02
# A tibble: 10 × 5
blockLabel trial_resp.corr Freq tc freq
<fct> <fct> <int> <dbl> <dbl>
1 auditory_only 0 0 12 0
2 bimodal_focus_auditory 0 1 72 1.39
3 bimodal_focus_visual 0 2 72 2.78
4 divided 0 77 144 53.5
5 visual_only 0 11 12 91.7
6 auditory_only 1 12 12 100
7 bimodal_focus_auditory 1 71 72 98.6
8 bimodal_focus_visual 1 70 72 97.2
9 divided 1 67 144 46.5
10 visual_only 1 1 12 8.33
$pilot03
# A tibble: 10 × 5
blockLabel trial_resp.corr Freq tc freq
<fct> <fct> <int> <dbl> <dbl>
1 auditory_only 0 1 12 8.33
2 bimodal_focus_auditory 0 1 72 1.39
3 bimodal_focus_visual 0 3 72 4.17
4 divided 0 75 144 52.1
5 visual_only 0 0 12 0
6 auditory_only 1 11 12 91.7
7 bimodal_focus_auditory 1 71 72 98.6
8 bimodal_focus_visual 1 69 72 95.8
9 divided 1 69 144 47.9
10 visual_only 1 12 12 100
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/529897.html
