如何根据R中的另一个文件重命名标题列

时间:2013-07-04 21:01:05

标签: r

我在R.工作。我有两个文本文件 - 一个是数据文件,另一个是注释文件。数据文件有一个标题行。注释文件包含标题的描述,标题是“正常”还是“缺陷”。我想通过在标题中附加“Normal-”或“Defective-”来更新数据文件中的标题。例如,如果标题是“abc123”并且注释文件中的相应条目是“正常”,那么我必须将标题重命名为“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

1 个答案:

答案 0 :(得分: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