我的数据集t
包含1850列。其中大多数包含"fu1"
其名称。我想全部替换fu1
为“ v1”
样例t
如下:
> t
symp_pre_rh_fu1___1 symp_pre_rh_fu1___2 symp_pre_rh_fu1___3 symp_pre_rh_fu1___4
1 0 0 0 0
2 1 0 0 0
3 1 1 0 1
4 1 1 0 0
5 0 0 0 0
但我要打印:
> t
symp_pre_rh_v1___1 symp_pre_rh_v1___2 symp_pre_rh_v1___3 symp_pre_rh_v1___4
1 0 0 0 0
2 1 0 0 0
3 1 1 0 1
4 1 1 0 0
5 0 0 0 0
dplyr
如果可能的话,我希望有一个解决方案。
t <- structure(list(symp_pre_rh_fu1___1 = c(0L, 1L, 1L, 1L, 0L), symp_pre_rh_fu1___2 = c(0L,
0L, 1L, 1L, 0L), symp_pre_rh_fu1___3 = c(0L, 0L, 0L, 0L, 0L),
symp_pre_rh_fu1___4 = c(0L, 0L, 1L, 0L, 0L), symp_pre_rh_fu1___5 = c(0L,
0L, 1L, 0L, 0L)), row.names = c(NA, -5L), class = "data.frame")
@Onyambu提供了简单的方法。可能使用tidyverse方法dplyr::rename_all
。
第二个参数是.funs
将接受适用于名称的函数。在这种情况下,我们可以使用stringr::str_replace
替换字符串。
library(dplyr)
library(stringr)
t %>% rename_all(~str_replace(.,"fu1","v1"))
symp_pre_rh_v1___1 symp_pre_rh_v1___2 symp_pre_rh_v1___3 symp_pre_rh_v1___4 symp_pre_rh_v1___5
1 0 0 0 0 0
2 1 0 0 0 0
3 1 1 0 1 1
4 1 1 0 0 0
5 0 0 0 0 0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句