如果特定字符串不存在,则重命名列。

huangapple go评论62阅读模式
英文:

Renaming columns if a specific character string doesn't exist

问题

我想将列名更改为包括字符 _16S_sed,如果它们尚未出现。例如,我有以下数据集:

> 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))

其中第三列需要添加字符 _16S_sed。谢谢。

英文:

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

答案1

得分: 3

以下是您要翻译的代码部分:

在基本的R中,一种方法是首先使用grepgrepl来识别缺少所需字符串的列,然后进行替换:

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

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

输出:

#   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

希望这对您有所帮助。如果您需要进一步的帮助,请随时告诉我。

英文:

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

missing_colnames &lt;- grep(&quot;_16S_sed&quot;, colnames(df), invert = TRUE) # thanks @rps1227
colnames(df)[missing_colnames] &lt;- paste0(colnames(df[missing_colnames]), &quot;_16S_sed&quot;)

# or
missing_colnames2 &lt;- !grepl(&quot;_16S_sed&quot;, colnames(df))
colnames(df)[missing_colnames2] &lt;- paste0(colnames(df)[missing_colnames2], &quot;_16S_sed&quot;)

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

答案2

得分: 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 val1, val2, val3
$ GL12_UP_3_16S_sed val1, val2, val3
$ GL13_1_DN_16S_sed val1, val2, val3


<details>
<summary>英文:</summary>

    library(tidyverse)
    
    df %&gt;%  
      rename_with(~ if_else(str_detect(.x, &quot;_16S_sed&quot;), .x, str_c(.x, &quot;_16S_sed&quot;)))
    
    # A tibble: 3 &#215; 3
      GL11_DN_1_16S_sed GL12_UP_3_16S_sed GL13_1_DN_16S_sed
      &lt;fct&gt;             &lt;fct&gt;             &lt;fct&gt;            
    1 val1              val1              val1             
    2 val2              val2              val2             
    3 val3              val3              val3    

      

Glimpsed: 

    Rows: 3
    Columns: 3
    $ GL11_DN_1_16S_sed &lt;fct&gt; val1, val2, val3
    $ GL12_UP_3_16S_sed &lt;fct&gt; val1, val2, val3
    $ GL13_1_DN_16S_sed &lt;fct&gt; val1, val2, val3

</details>



huangapple
  • 本文由 发表于 2023年6月29日 21:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定