小编D. *_*mpo的帖子

使用dplyr重新编码多个列

我有一个数据框,我在其中重新编码了几列,以便将999设置为NA

dfB <-dfA %>%
  mutate(adhere = if_else(adhere==999, as.numeric(NA), adhere)) %>%
  mutate(engage = if_else(engage==999, as.numeric(NA), engage)) %>%
  mutate(quality = if_else(quality==999, as.numeric(NA), quality)) %>%
  mutate(undrstnd = if_else(undrstnd==999, as.numeric(NA), undrstnd)) %>%
  mutate(sesspart = if_else(sesspart==999, as.numeric(NA), sesspart)) %>%
  mutate(attended = if_else(attended>=9, as.integer(NA), attended))
Run Code Online (Sandbox Code Playgroud)

我想使用mutate_at()和一系列列和recode()而不是if_else(),但我仍然坚持如何给它条件.我认为999基于一些mutate_all示例的类似= NA的东西 - 但是我还需要NA来匹配.x的类型,我不确定如何使它对类型敏感

我试过了:

y <- data.frame(y1=c(1,2,999,3,4), y2=c(1L, 2L, 999L, 3L, 4L), y3=c(T,T,F,F,T))
z <- y %>%
    mutate_at( vars(y1:y2), funs(recode(.,`999` = as.numeric(NA))))
Run Code Online (Sandbox Code Playgroud)

但我得到一个警告"未被替换的值被视为NA为.x不兼容.请详细指定替换或提供.default"我可以看到它的数字列,但不是整数列y2"

> z
  y1 y2    y3
1  1 NA  TRUE
2  2 NA  TRUE
3 NA …
Run Code Online (Sandbox Code Playgroud)

r dplyr recode mutate

4
推荐指数
4
解决办法
3870
查看次数

R ggplot 多面图中的多级 x 轴标签

我尝试使用两次调用 annotate() 的技巧来获取 2 级嵌套刻度标签(此处),但我想与 facet_wrap() 结合使用

df <- tribble(
  ~action, ~gend, ~status, ~cellMean,
  "P",     "M",   "A",     1,
  "P",     "M",   "B",     2,
  "P",     "F",   "A",     1.4,
  "P",     "F",   "B",     2.6,
  "V",     "M",   "A",     2,
  "V",     "M",   "B",     3,
  "V",     "F",   "A",     2.2,
  "V",     "F",   "B",     3.8
) %>%
  mutate(action=factor(action,levels=c("P","V")),
         gend  =factor(gend,  levels=c("F","M")),
         status=factor(status,levels=c("A","B")))

df

# action obscured/overlaid
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  theme_light()
Run Code Online (Sandbox Code Playgroud)

单面板叠加

# action used as facet
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 facet-wrap multi-level x-axis

1
推荐指数
1
解决办法
731
查看次数

标签 统计

r ×2

dplyr ×1

facet-wrap ×1

ggplot2 ×1

multi-level ×1

mutate ×1

recode ×1

x-axis ×1