在 R 中读取文本文件并将其转换为字符对象 [英] Read text file in R and convert it to a character object

查看:55
本文介绍了在 R 中读取文本文件并将其转换为字符对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 R 2.10.0 中读取这样的文本文件.

I'm reading a text file like this in R 2.10.0.

248585_at   250887_at   245638_s_at AFFX-BioC-5_at
248585_at   250887_at   264488_s_at 245638_s_at AFFX-BioC-5_at  AFFX-BioC-3_at  AFFX-BioDn-5_at
248585_at   250887_at

使用命令clusters<-read.delim("test",sep="\t",fill=TRUE,header=FALSE)

现在,我必须将这个文件中的每一行传递给一个 BioConductor 函数,该函数只接受字符向量作为输入.

Now, I must pass every row in this file to a BioConductor function that takes only character vectors as input.

我的问题是在这个簇"对象上使用 as.character 会将所有内容都变成数字字符串.

My problem is that using as.character on this "clusters" object turns everything into numeric strings.

> clusters[1,]
         V1        V2          V3             V4 V5 V6 V7
1 248585_at 250887_at 245638_s_at AFFX-BioC-5_at         

但是

> as.character(clusters[1,])
[1] "1" "1" "2" "3" "1" "1" "1"

有没有办法保留原来的名字,放到一个字符向量中?

Is there any way to keep the original names and put them into a character vector?

也许有帮助:我的read.delim"文件给出的clusters"对象属于list"类型.

Maybe it helps: my "clusters" object given by the "read.delim" file belongs to the "list" type.

非常感谢:-)

费德里科

推荐答案

默认情况下,字符列被转换为因子.您可以通过设置 as.is=TRUE 参数来避免这种情况:

By default character columns are converted to factors. You can avoid this by setting as.is=TRUE argument:

clusters <- read.delim("test", sep="\t", fill=TRUE, header=FALSE, as.is=TRUE)

如果您只将参数从文本文件传递到字符向量,您可以执行以下操作:

If you only pass arguments from text file to character vector you could do something like:

x <- readLines("test")
xx <- strsplit(x,split="\t")
xx[[1]] # xx is a list
# [1] "248585_at"      "250887_at"      "245638_s_at"    "AFFX-BioC-5_at"

这篇关于在 R 中读取文本文件并将其转换为字符对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆