2

I'm looking to rename colnames to include the characters _16S_sed if they're not present. For example I have the dataset

> dput(dataframe1)
structure(list(GL11_DN_1_16S_sed = structure(1:3, .Label = c("val1", 
"val2", "val3"), class = "factor"), GL12_UP_3_16S_sed = structure(1:3, .Label = c("val1", 
"val2", "val3"), class = "factor"), GL13_1_DN = structure(1:3, .Label = c("val1", 
"val2", "val3"), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

where the third column needs the characters _16S_sed. TIA

0

2 Answers 2

3

In base R, one approach would be to first identify the columns missing the desired string using grep or grepl, then replace:

missing_colnames <- grep("_16S_sed", colnames(df), invert = TRUE) # thanks @rps1227
colnames(df)[missing_colnames] <- paste0(colnames(df[missing_colnames]), "_16S_sed")

# or
missing_colnames2 <- !grepl("_16S_sed", colnames(df))
colnames(df)[missing_colnames2] <- paste0(colnames(df)[missing_colnames2], "_16S_sed")

Output:

#   GL11_DN_1_16S_sed GL12_UP_3_16S_sed GL13_1_DN_16S_sed
# 1              val1              val1              val1
# 2              val2              val2              val2
# 3              val3              val3              val3
Sign up to request clarification or add additional context in comments.

2 Comments

You can use invert = TRUE in grep() to get the indices that do not containt 16S_sed instead of setdiff() too.
@rps1227 thanks! Excellent point - I have edited accordingly
2
library(tidyverse)

df %>%  
  rename_with(~ if_else(str_detect(.x, "_16S_sed"), .x, str_c(.x, "_16S_sed")))

# A tibble: 3 × 3
  GL11_DN_1_16S_sed GL12_UP_3_16S_sed GL13_1_DN_16S_sed
  <fct>             <fct>             <fct>            
1 val1              val1              val1             
2 val2              val2              val2             
3 val3              val3              val3    

  

Glimpsed:

Rows: 3
Columns: 3
$ GL11_DN_1_16S_sed <fct> val1, val2, val3
$ GL12_UP_3_16S_sed <fct> val1, val2, val3
$ GL13_1_DN_16S_sed <fct> val1, val2, val3

Comments

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.