我想获得一个通用公式来排列具有不同列数的数据框。
\n例如,在本例中,数据帧包含“categ_1,categ_2,points_1,points_2”:
\n library(tidyverse)\n set.seed(1)\n nrows <- 20\n df <- tibble(\n other_text = sample(letters,\n nrows, replace = TRUE),\n categ_1 = sample(c("A", "B"), nrows, replace = TRUE),\n categ_2 = sample(c("A", "B"), nrows, replace = TRUE),\n points_1 = sample(20:25, nrows, replace = TRUE),\n points_2 = sample(20:25, nrows, replace = TRUE),\n ) %>%\n rowwise() %>%\n mutate(total = sum(c_across(starts_with("points_")))) %>%\n ungroup()\nRun Code Online (Sandbox Code Playgroud)\n以及排列的公式:
\ndf %>%\n arrange(\n desc(total),\n categ_1, categ_2,\n desc(points_1), desc(points_2)\n )\nRun Code Online (Sandbox Code Playgroud)\n但df可以有更多列:“categ_1、categ_2、categ_3、points_1、points_2、points_3”。\n因此,在这种情况下,公式应为:
\ndf %>%\n mutate(\n categ_3 = …Run Code Online (Sandbox Code Playgroud) a、b 和 c 是句子
Column
a,b,c
b,c
a,c
c
Run Code Online (Sandbox Code Playgroud)
我想分开并计算每个值,以获得:
column a column b column c
yes yes yes
no yes yes
yes no yes
no no yes
Run Code Online (Sandbox Code Playgroud)