用空格替换要素名称中的所有下划线 [英] Replace all underscores in feature names with a space
本文介绍了用空格替换要素名称中的所有下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用空格替换数据框特征名称中的所有下划线:
I'd like to replace all underscores in a dataframes feature names with a space:
library(tidyverse)
names <- c("a_nice_day", "quick_brown_fox", "blah_ha_ha")
example_df <- data.frame(
x = 1:3,
y = LETTERS[1:3],
z = 4:6
)
names(example_df) <- names
尝试:
example_df %>% rename_all(replace = c("_" = " "))
Error: `.funs` must specify a renaming function
也尝试过:
example_df %>% rename_all(funs(replace = c("_" = " ")))
Error: `nm` must be `NULL` or a character vector the same length as `x`
如何用空格替换要素名称中的所有下划线?
How can I replace all the underscores in the feature names with a space?
推荐答案
有关:
example_df %>% select_all(funs(gsub("_", " ", .)))
输出:
a nice day quick brown fox blah ha ha
1 1 A 4
2 2 B 5
3 3 C 6
您也可以使用 rename
,但是在这种情况下,您需要以其他方式调用它:
You could also use rename
, however in this case you'd need to call it in a different way:
example_df %>% rename_all(function(x) gsub("_", " ", x))
或者简单地:
example_df %>% rename_all(~ gsub("_", " ", .))
这篇关于用空格替换要素名称中的所有下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文