一:合并文件
目的是:合并两个文件
解决方法:用merge
合并两个文件,通过相同的列名Gene_Symbol
file1 <- read.csv("你的路径/.csv")
file2 <- read.csv("你的路径/.csv")
newfile <- merge(file1, file2, by = c("Gene_Symbol"))
生成的文件会在原本的列名后加上.x
、.y
区分原来属于哪个文件
.x
表示file1
、.y
表示file2
二:file1
只有一列Gene_Symbol
,file2
中有Gene_Symbol
和Gene_name
两列
目的是:想从file2
中检索到相应的Gene_name
,并填在file1
对应的Gene_Symbol
后面
解决方法:hash()
library(hash)
h <- hash()
#构建键值对
.set(h, keys = file2$Gene_Symbol, values = file2$Gene_Name)
genename <- file2[,2]
file1[,2] <- values(h, genename)
三:计算两个文件中相同列名的行
解决方法:intersect()
both <- intersect(file1$相同列名, file2$相同列名)
length(both)
四:提取两个文件中具有相同值的一整行,或一整行中的某几列
目的是:现有一个文件file
,内有几行内容,file1
含有其中某些相同值,想要提取出file
中含有file1
内容的对应行


解决方法:
%in%
output <- file[file$V1 %in% file1, ]

这里用到了
TRUE
、FALSE
来判断要不要输出这一行

💥需要注意的是:把被查找的文件写在
%in%
之前,才会是这样的结果,否则只会显示小文件中的很少的行数,如下面这个例子:
file1 %in% file$V1

网友评论