2016-12-24 104 views
-2

我有一個文件,其中包含下面的數據,我正在嘗試將包含「GBM1,GBM10 ...」的列「條件」更改爲通用名稱「GBM」。 我需要R命令幫助將它們轉換爲GBM。 我想知道如何使用dplyr來點。如何替換R中列的內容?

下面是數據..

dat <- data.frame(
sample = c("GSM564972", "GSM564973", "GSM564974"), 
condition= c("GBM1", "GBM10", "GBM11")) 

希望的輸出:

sample condition 
GSM564972  GBM 
GSM564973  GBM 
GSM564974  GBM 
+1

[R中更換一整列]的可能的複製一個選項(http://stackoverflow.com/questions/19295277/replacing-a-whole-column-in- R) – Ale

回答

0

U可以使用sub函數從dplyrmutate以取代 「」 在組合數字(空字符串) 。

library(dplyr) 
# use sub within mutate to change column 
mutate(dat, condition = sub("\\d+", "", condition)) 

sub使用的例子見here

1

這裏是str_extract

library(stringr) 
library(dplyr) 
dat %>% 
    mutate(condition = str_extract(condition, "\\D+"))