问题描述
我有一个包含行名称的 .txt
文件。
read.table()方法,你可以使用 )
(或其中一个,例如 read.csv()
),那么很容易修复是更改调用: read.table(file =foo.txt,row.names = 1,....)
其中 ....
是您需要/使用的其他参数。 row.names
参数接受从中获取行名称的数据文件的列号。它不需要是第一列。有关详细信息,请参阅?read.table
。
如果已经有R中的数据,或者来自另一个路由,只要设置 rownames
属性并从对象中删除第一个变量(假设 obj
是您的对象)
rownames(obj)< - obj [,1] ## set rownames
obj< - obj [,-1] ##删除第一个变量
I have a .txt
file that contains row names. However, R set the row names as the first column.
解决方案 If you used read.table()
(or one of it's ilk, e.g. read.csv()
) then the easy fix is to change the call to:
read.table(file = "foo.txt", row.names = 1, ....)
where ....
is other arguments you needed/used. The row.names
argument take the column number of the data file from which to take the row names. It need not be the first column. See ?read.table
for details/info.
If you already have the data in R and can;t be bothered to re-read it, or is came from another route, just set the rownames
attribute and remove the first variable from the object (assuming obj
is your object)
rownames(obj) <- obj[, 1] ## set rownames
obj <- obj[, -1] ## remove the first variable
这篇关于在读取文件时指定行名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!