gpt4 book ai didi

r - 快速问题 : alternatives to a short but a bit involved code

转载 作者:行者123 更新时间:2023-12-03 22:59:04 29 4
gpt4 key购买 nike

我正在尝试生成一个具有规范的字符串(每列的列名和最大字符串长度):

library(tidyverse)
df <- tibble::tribble(
~nif, ~dni_check,
"33333457V", "33333457",
"44817563Q", "44817563",
"33256164G", "33256164",
"36050083K", "36050083"
)
df %>%
summarise(across(everything(), ~ str_c(" C(", max(str_length(.)), ")"))) %>%
map2_chr(colnames(.), ., str_c) %>%
str_c(collapse = "; ")
#> [1] "nif C(9); dni_check C(8)"
出于好奇,您是否会提出替代方案,也许以更短或更简洁的方式?

最佳答案

基本 R 选项

paste0(
do.call(
sprintf,
c(
fmt = "%s C(%d)",
rev(stack(sapply(df, function(v) max(nchar(v)))))
)
),
collapse = "; "
)
"nif C(9); dni_check C(8)"

关于r - 快速问题 : alternatives to a short but a bit involved code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67562366/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com