• R/O
  • SSH

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Commit MetaInfo

修订版efcf65d567002165748a9ca068eeb397c2ef5d00 (tree)
时间2022-09-16 22:36:18
作者Lorenzo Isella <lorenzo.isella@gmai...>
CommiterLorenzo Isella

Log Message

How to recode factors based on their associated value.

更改概述

差异

diff -r 4f2e96e0121a -r efcf65d56700 R-codes/test_factors.R
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/R-codes/test_factors.R Fri Sep 16 15:36:18 2022 +0200
@@ -0,0 +1,33 @@
1+library(tidyverse)
2+library(forcats)
3+
4+
5+
6+df <- tibble(x=rep(letters[1:10], 10), w=seq(100))
7+
8+df
9+
10+###detect the first 4 factors based on the w column
11+
12+ff <- df |>
13+ group_by(x) |>
14+ summarise(w_tot=sum(w)) |>
15+ ungroup() |>
16+ arrange(desc(w_tot)) |>
17+ slice(1:4) |>
18+ pull(x)
19+
20+ff
21+
22+## recode the data
23+
24+df_new <- df |>
25+ mutate(w=if_else(x %in% ff, x, "other"))
26+
27+df_new
28+
29+df_new2 <- df |>
30+ mutate(x2=fct_lump_n(x,4, w))
31+
32+
33+df_new2