0

I have my dataset t comprising 1850 columns. Most of these contain "fu1" in their name. I want to replace all fu1 to "v1"

A sample of t looks like:

> 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

But I want it to print:

> 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

I would prefer a solution in dplyr if possible.

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")
2
  • 4
    names(t) <- sub("fu1", "v1", names(t)) or even names(t) <- gsub("fu1", "v1", names(t)) but use sub in this case Commented Dec 16, 2020 at 19:38
  • Well that was easy. Thank you. Commented Dec 16, 2020 at 19:43

1 Answer 1

1

@Onyambu provided the easy way. A tidyverse approach might be to use dplyr::rename_all.

The second argument is .funs which will accept a function to apply to the names. In this case, we can use stringr::str_replace to replace the string.

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, exactly what I requested - a dplyr solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.