-1

我在 R 中工作。我有两个文本文件——一个是数据文件,另一个是注释文件。数据文件有一个标题行。注释文件具有对标头的描述,无论标头是“正常”还是“有缺陷”。我想通过将“Normal-”或“Defective-”附加到标题来更新数据文件中的标题。例如,如果标题是“abc123”并且注释文件中的相应条目是“Normal”,那么我必须将标题重命名为“Normal-abc123”。我必须为所有标题执行此操作。

谢谢

数据文件:

           GSM146796        GSM146798  GSM146779  GSM146781  GSM146783
1007_s_at     2107.7 9898.3406‌​9121213     1940.2     2608.8     1837.2

注释文件:

GSM146798 = Value for GSM146798: Stage 2, PT1, Normal (HG-U133A); src: Human Renal Epithelium
GSM146796 = Value for GSM146796: Stage 2, PT12, Normal (HG-U133A); src: Human Renal Epithelium
GSM146779 = Value for GSM146779: Stage 1, PT2, Defective (HG-U133A); src: Human Renal Epithelium
GSM146781 = Value for GSM146781: Stage 1, PT3, Defective (HG-U133A); src: Human Renal Epithelium
GSM146783 = Value for GSM146783: Stage 1, PT4, Defective (HG-U133A); src: Human Renal Epithelium
4

1 回答 1

0
# read in annotation
annotation <- read.table(text="GSM146798 = Value for GSM146798: Stage 2, PT1, Normal (HG-U133A); src: Human Renal Epithelium
                    GSM146796 = Value for GSM146796: Stage 2, PT12, Normal (HG-U133A); src: Human Renal Epithelium
                    GSM146779 = Value for GSM146779: Stage 1, PT2, Defective (HG-U133A); src: Human Renal Epithelium
                    GSM146781 = Value for GSM146781: Stage 1, PT3, Defective (HG-U133A); src: Human Renal Epithelium
                    GSM146783 = Value for GSM146783: Stage 1, PT4, Defective (HG-U133A); src: Human Renal Epithelium", stringsAsFactors=FALSE)

# read in table
data <- read.table(text="           GSM146796        GSM146798  GSM146779  GSM146781  GSM146783
                    1007_s_at     2107.7 9898.3406??9121213     1940.2     2608.8     1837.2", header=TRUE)

这很简单sapply。我想甚至可能有一种更直接的矢量化方式,但这应该可以满足您的需求。

newname <- sapply(names(data), function(x) 
                    paste(annotation$V9[annotation$V1==x],x,sep="-") )
names(data) <- newname
于 2013-07-05T06:17:50.483 回答