r - 我怎样才能转动这个小标题?
问题描述
考虑到这个小标题
library(tibble)
mytab <- tribble(
~siren_ent, ~nbeta_dep01, ~nbeta_dep02, ~effeta_dep01, ~effeta_dep02, ~categ,
"A", 3, 0, 50, 0, "X",
"B", 0, 2, 0, 30, "X",
"C", 1, 1, 10, 15, "Y"
)
我想转动它以获得这个结果......
> result
# A tibble: 4 x 4
DEP categ nbeta effeta
<chr> <chr> <dbl> <dbl>
1 01 X 3 50
2 01 X 1 10
3 02 Y 2 30
4 02 Y 1 15
如果您有 tidyverse 的解决方案,我很感兴趣。
提前谢谢了 !
解决方案
我假设您的第二个effeta_dep01
专栏实际上应该被称为effeta_dep02
. 如果是这样,那么这给出了接近所需输出的东西:
library(tidyr)
library(dplyr)
mytab %>%
pivot_longer(nbeta_dep01:effeta_dep02,
names_sep = "_dep",
names_to = c(".value", "dep")) %>%
filter(nbeta > 0) %>%
select(-siren_ent)
这使
# A tibble: 4 x 4
categ dep nbeta effeta
<chr> <chr> <dbl> <dbl>
1 X 01 3 50
2 X 02 2 30
3 Y 01 1 10
4 Y 02 1 15
我假设您的预期输出不正确,但如果不是,我很高兴知道您期望的转换。
推荐阅读
- mongodb - 在具有身份验证的 MongoDB Docker 映像上找不到用户
- excel - 在 DOORS 模块中使用高级过滤器进行过滤
- javascript - 在相应栏的上方显示值
- jupyter-notebook - 如何从列表中制作一个复选框并在 jupyter notebook 中获取选定的值
- kotlin - 在某些条件下从现有的 ArrayLists 创建一个新的 ArrayList
- wordpress - 在 Nav_Menu (WordPress) /w SASS 中突出显示当前页面
- php - 致命错误:即使在安装了 Guzzle 之后,在 Laravel 中也找不到类“Guzzle\Http\Client”
- wordpress - 如何在不将图像上传到媒体库的情况下修复主题缩略图?
- php - HTML 表单的正确 PHP 输入
- javascript - 如何从节点 api 服务器返回 imap 结果?