【问题标题】:gsub in columns value in dataframe数据框中列值中的 gsub
【发布时间】:2016-07-26 18:56:53
【问题描述】:

我有一个包含多列的文件。我正在展示两列我感兴趣的两列

 Probe.Set.ID         Entrez.Gene
A01157cds_s_at               50682
A03913cds_s_at               29366
A04674cds_s_at 24860 /// 100909612
A07543cds_s_at               24867
A09811cds_s_at               25662
----                          ----
A16585cds_s_at               25616

我需要用 "\t"(tab) 替换 /// 并且输出应该是这样的

A01157cds_s_at;50682
A03913cds_s_at;29366
A04674cds_s_at;24860      100909612

另外,我需要避免带有“---”的那些

【问题讨论】:

  • 如果将/// 替换为制表符,则某些行的列数会多于其他行,这将无法很好地导入。从外观上看,我会用read.fwf 导入它并在事后修复它。
  • 你正在苦苦挣扎的部分是什么?从外观上看,这可以读取为固定宽度数据help(read.fwf),您已经找到了gsub 用于替换分隔符,显然您可以使用write.csv2 写入该数据。您需要帮助哪一部分?顺便说一句:fortune(230)

标签: regex r dataframe bioinformatics


【解决方案1】:

这里是使用 dplyr 的稍微不同的方法:

data <- data.frame(Probe.Set.ID = c("A01157cds_s_at",
                                "A03913cds_s_at",
                                "A04674cds_s_at",
                                "A07543cds_s_at",
                                "A09811cds_s_at",
                                "----",
                                "A16585cds_s_at"),
               Entrez.Gene = c("50682",
                               "29366",
                               "24860 /// 100909612",
                               "24867",
                               "25662",
                               "----",
                               "25616")
)

if(!require(dplyr)) install.packages("dplyr")
library(dplyr)

data %>% 
  filter(Entrez.Gene != "----") %>%
  mutate(new_column = paste(Probe.Set.ID,
                        gsub("///", "\t", Entrez.Gene),
                        sep = ";"
                        )
     ) %>% select(new_column)

【讨论】:

    【解决方案2】:

    看起来您需要对数据进行子集化,然后将两列粘贴在一起,然后使用 gsub 替换“///”。这是我想出的,dat 是包含两列的数据框。

    dat = dat[dat$Probe.Set.ID != "----",] # removes the rows with "---"
    dat = paste0(dat$Probe.Set.ID, ";", dat$Entrez.Gene) # pastes the columns together and adds the ";"
    dat = gsub("///","\t",dat) # replaces the "///" with a tab
    

    另外,使用 cat() 来查看选项卡,而不是“\t”。我从这里得到的:How to replace specific characters of a string with tab in R。这将输出一个列表而不是 data.frame。你可以用data.frame()转换回来,但是你不能用cat()来查看。

    【讨论】:

      【解决方案3】:

      我们可以在这里使用dplyrtidyr

      library(dplyr)
      library(tidyr)
      
      > df <- data.frame(
          col1 = c('A01157cds_s_at', 'A03913cds_s_at', 'A04674cds_s_at', 'A07543cds_s_at', '----'), 
          col2 = c('50682', '29366', '24860 /// 100909612', '24867', '----'))
      
      
      > df %>% filter(col1 != '----') %>% 
          separate(col2, c('col2_first', 'col2_second'), '///', remove = T) %>%
          unite(col1_new, c(col1, col2_first), sep = ';', remove = T)
      
      > df
      
      ##                col1_new col2_second
      ## 1  A01157cds_s_at;50682        <NA>
      ## 2  A03913cds_s_at;29366        <NA>
      ## 3  A04674cds_s_at;24860    100909612
      ## 4  A07543cds_s_at;24867        <NA>
      
      • filter 删除带有col1 == '----' 的观察。
      • separatecol2 拆分为两列,即col2_firstcol2_second
      • unite 连接 col1col2_first; 作为分隔符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多