【发布时间】:2020-09-19 03:26:32
【问题描述】:
问题说明
我有一个表,其中一些列以我想删除的字符串开头。我试图通过使用 dplyr 中的新“交叉”功能和 stringr 中的函数来操作字符串来做到这一点,但我失败了(这就是我在这里的原因!)。
示例代码
library(tidyverse)
# Generate a short mock-up table
set.seed(1)
mockup <- tibble(n_col1 = sample(1:10, 5, FALSE),
v_col2 = sample(letters, 5, FALSE),
col3 = sample(10:20, 5, TRUE),
col4 = sample(LETTERS, 5, FALSE)) # More columns beginning with "n_", "v_" or nothing
# Remove the "n_" or "v_" strings from the column names
# This is as far as my skills go...
mockup <- mockup %>% mutate(across(starts_with(c("n_", "v_")), str_remove))
问题
任何想法如何以我的方式或以不同的方式以简短有效的方式做到这一点?
【问题讨论】: