英文:
Change column name in pivot_wider for Boolean column
问题
以下是您要翻译的内容:
"I have some data that I want to pivot_wider
, using one or more names_from
columns. At least one of the potential names_from
columns is Boolean.
This results in uninformative default column names like TRUE
, FALSE
or TRUE_somevalue
, FALSE_somevalue
(where somevalue
is an otherwise meaningful level from another column).
I would like to instead use some more informative column name when some names_from
column or columns are Boolean, for instance, varnameTRUE
, varnameFALSE
or varnameTRUE_somevalue
, varnameFALSE_somevalue
(where varname
is the name of the Boolean column in the first place).
Obviously, I could convert the Boolean column to character or factor with meaningful names in the first place, but I'm wondering if there's some generalizable way to do this using the arguments available to pivot_wider
. I can't figure out a way to do it with e.g. names_glue
or names_sep
. Ideally I'd like to be able to do this without even manually specifying the relevant columns, but have it happen automatically if a names_from
column is a Boolean one.
Is there some way to do this?
Some example data:
test_dat <- tibble(
idx = c(rep("a", 6), rep("b", 6)),
boo = c(rep(c(TRUE, FALSE), 6)),
word = c(rep(c("foo", "bar", "baz"), 4)),
val = runif(12)
)
> test_dat
# A tibble: 12 × 4
idx boo word val
<chr> <lgl> <chr> <dbl>
1 a TRUE foo 0.325
2 a FALSE bar 0.770
3 a TRUE baz 0.824
4 a FALSE foo 0.351
5 a TRUE bar 0.555
6 a FALSE baz 0.698
7 b TRUE foo 0.0200
8 b FALSE bar 0.427
9 b TRUE baz 0.325
10 b FALSE foo 0.463
11 b TRUE bar 0.987
12 b FALSE baz 0.345
Default output:
> test_dat %>% pivot_wider(names_from=c("boo", "word"), values_from=val)
# A tibble: 2 × 7
idx TRUE_foo FALSE_bar TRUE_baz FALSE_foo TRUE_bar FALSE_baz
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a 0.325 0.770 0.824 0.351 0.555 0.698
2 b 0.0200 0.427 0.325 0.463 0.987 0.345
Desired output would be identical except with column names more like booTRUE_foo
, booFALSE_foo
(or some variant thereof, e.g. boo_foo
, notboo_foo
)"
希望这能帮助您。
英文:
I have some data that I want to pivot_wider
, using one or more names_from
columns. At least one of the potential names_from
columns is Boolean.
This results in uninformative default column names like TRUE
, FALSE
or TRUE_somevalue
, FALSE_somevalue
(where somevalue
is an otherwise meaningful level from another column).
I would like to instead use some more informative column name when some names_from
column or columns are Boolean, for instance, varnameTRUE
, varnameFALSE
or varnameTRUE_somevalue
, varnameFALSE_somevalue
(where varname
is the name of the Boolean column in the first place).
Obviously, I could convert the Boolean column to character or factor with meaningful names in the first place, but I'm wondering if there's some generalizable way to do this using the arguments available to pivot_wider
. I can't figure out a way to do it with e.g. names_glue
or names_sep
. Ideally I'd like to be able to do this without even manually specifying the relevant columns, but have it happen automatically if a names_from
column is a Boolean one.
Is there some way to do this?
Some example data:
test_dat <- tibble(
idx = c(rep("a", 6), rep("b", 6)),
boo = c(rep(c(TRUE, FALSE), 6)),
word = c(rep(c("foo", "bar", "baz"), 4)),
val = runif(12)
)
> test_dat
# A tibble: 12 × 4
idx boo word val
<chr> <lgl> <chr> <dbl>
1 a TRUE foo 0.325
2 a FALSE bar 0.770
3 a TRUE baz 0.824
4 a FALSE foo 0.351
5 a TRUE bar 0.555
6 a FALSE baz 0.698
7 b TRUE foo 0.0200
8 b FALSE bar 0.427
9 b TRUE baz 0.325
10 b FALSE foo 0.463
11 b TRUE bar 0.987
12 b FALSE baz 0.345
Default output:
> test_dat %>% pivot_wider(names_from=c("boo", "word"), values_from=val)
# A tibble: 2 × 7
idx TRUE_foo FALSE_bar TRUE_baz FALSE_foo TRUE_bar FALSE_baz
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a 0.325 0.770 0.824 0.351 0.555 0.698
2 b 0.0200 0.427 0.325 0.463 0.987 0.345
Desired output would be identical except with column names more like booTRUE_foo
, booFALSE_foo
(or some variant thereof, e.g. boo_foo
, notboo_foo
)
答案1
得分: 2
你可以在最开始使用 `dplyr` 函数将列名附加到那些布尔(逻辑)列上:
```R
mutate(across(where(is.logical), ...))
然后再进行宽表格的变换。通过这种方式,你不需要手动指定布尔列的名称。
library(tidyverse)
test_dat %>%
mutate(across(where(is.logical), ~ paste0(cur_column(), .x))) %>%
pivot_wider(names_from = c(boo, word), values_from = val)
# # A tibble: 2 × 7
# idx booTRUE_foo booFALSE_bar booTRUE_baz booFALSE_foo booTRUE_bar booFALSE_baz
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 a 0.0556 0.679 0.409 0.666 0.675 0.312
# 2 b 0.281 0.477 0.0817 0.977 0.745 0.340
<details>
<summary>英文:</summary>
You can append column names to those boolean(logical) columns at first with `dplyr` functions:
mutate(across(where(is.logical), ...))
before pivoting to wide. By this way you don't need to require manual specification of the boolean column names.
```r
library(tidyverse)
test_dat %>%
mutate(across(where(is.logical), ~ paste0(cur_column(), .x))) %>%
pivot_wider(names_from = c(boo, word), values_from = val)
# # A tibble: 2 × 7
# idx booTRUE_foo booFALSE_bar booTRUE_baz booFALSE_foo booTRUE_bar booFALSE_baz
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 a 0.0556 0.679 0.409 0.666 0.675 0.312
# 2 b 0.281 0.477 0.0817 0.977 0.745 0.340
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论