在 R 中读取具有重复行名称的 csv 文件

发布于 2024-09-30 09:48:14 字数 573 浏览 10 评论 0原文

我正在尝试读取具有重复行名称的 csv 文件,但无法读取。我收到的错误消息是 Error in read.table(file = file, header = header, sep = sep, quote = quote, : 不允许重复的 'row.names'。

代码我我使用的是:

S1N657 <- read.csv("S1N657.csv",header=T,fill=T,col.names=c("dam","anim","temp"))

下面给出了我的数据示例:

did <- c("1N657","1N657","1N657","1N657","1N657","1N657","1N657","1N657","1N657","1N657")
aid <- c(101,102,103,104,105,106,107,108,109,110)
temp <- c(36,38,37,39,35,37,36,34,39,38)

data <- cbind(did,aid,temp)

任何帮助将不胜感激。

I am trying to read a csv file with repeated row names but could not. The error message I am getting is Error in read.table(file = file, header = header, sep = sep, quote = quote, : duplicate 'row.names' are not allowed.

The code I am using is:

S1N657 <- read.csv("S1N657.csv",header=T,fill=T,col.names=c("dam","anim","temp"))

An example of my data is given below:

did <- c("1N657","1N657","1N657","1N657","1N657","1N657","1N657","1N657","1N657","1N657")
aid <- c(101,102,103,104,105,106,107,108,109,110)
temp <- c(36,38,37,39,35,37,36,34,39,38)

data <- cbind(did,aid,temp)

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

缱绻入梦 2024-10-07 09:48:14

该函数看到重复的行名称,因此您需要处理它。最简单的方法可能是使用 row.names=NULL,它将强制行编号 - 换句话说,它将您的第一列视为第一个维度,而不是第一个维度行号,因此添加行号(以“1”开头的连续整数。

read.csv("S1N657.csv", header=T,fill=T, col.names=c("dam","anim","temp"), row.names=NULL)

the function is seeing duplicate row names, so you need to deal with that. Probably the easiest way is with row.names=NULL, which will force row numbering--in other words, it treats your first column as the first dimension and not as the row numbers, and so adds row numbers (consecutive integers starting with "1".

read.csv("S1N657.csv", header=T,fill=T, col.names=c("dam","anim","temp"), row.names=NULL)
○闲身 2024-10-07 09:48:14

试试这个:

S1N657 <- read.csv("S1N657.csv",header=T,fill=T,col.names=c("dam","anim","temp"), 
          row.names = NULL)[,-1]

try this:

S1N657 <- read.csv("S1N657.csv",header=T,fill=T,col.names=c("dam","anim","temp"), 
          row.names = NULL)[,-1]
彩虹直至黑白 2024-10-07 09:48:14

猜测你的csv文件是从xlsx转换而来的。在第一行末尾添加一个逗号,删除最后一行,完成

Guessing your csv file was one converted from xlsx.Add a comma to the end of the first row ,remove the last row ,done

随梦而飞# 2024-10-07 09:48:14

我最近遇到的一个问题是标题行中的列数与数据本身的列数不匹配。例如,我的数据是制表符分隔的,所有数据行都有一个尾随制表符。标题行(我手动添加的)没有。

我希望对行进行自动编号,但它却将我的第一行视为行名称。来自文档(重点是我添加的):

row.names 行名称向量。这可以是给出实际行名称的向量,或者给出包含行名称的表列的单个数字,或者给出包含行名称的表列的名称的字符串。

如果有标题且第一行包含的字段数少于列数,则输入中的第一列将用作行名称。否则,如果 row.names 丢失,则行将被编号。

使用 row.names = NULL 强制行编号。缺少或 NULL row.names 生成被视为“自动”的行名称(并且不由 as.matrix 保留)。

在标题行中添加一个额外的制表符,使标题行的列数与数据行的列数相同,从而解决了问题。

An issue I had recently was that the number of columns in the header row did not match the number of columns I had in the data itself. For example, my data was tab-delimited and all of the data rows had a trailing tab character. The header row (which I had manually added) did not.

I wanted the rows to be auto-numbered, but instead it was looking at my first row as the row name. From the docs (emphasis added by me):

row.names a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.

If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names. Otherwise if row.names is missing, the rows are numbered.

Using row.names = NULL forces row numbering. Missing or NULL row.names generate row names that are considered to be ‘automatic’ (and not preserved by as.matrix).

Adding an extra tab character to the header row made the header row have the same number of columns as the data rows, thus solving the problem.

落在眉间の轻吻 2024-10-07 09:48:14

简而言之,检查您的列名称。如果您的第一行是列的名称,则可能会缺少一个或多个名称。

示例:

"a","b","c"
a,b,c,d
a,b,c,d

上面的示例将导致 row.name 错误,因为每行有 4 个值,但只命名了 3 列。

当我从在线资源构建 csv 时,就发生了这种情况。

In short, check your column names. If your first row is the names of columns, you may be missing one or more names.

Example:

"a","b","c"
a,b,c,d
a,b,c,d

The example above will cause a row.name error because each row has 4 values, but only 3 columns are named.

This happened to me when I was building a csv from an online resources.

絕版丫頭 2024-10-07 09:48:14

对于小型 CSV,我收到相同的“不允许重复的‘row.names’”错误。问题是,在我想要的 14x14 图表区域之外的某个地方,有一个带有空格/其他数据的随机单元格。

当我运行“row.names = NULL”并且我的表下方有多行空白数据时发现了答案(因此多个重复的行名称全部为“空白”)。

解决方案是删除表区域之外的所有行/列,并且成功了!

I was getting the same "duplicate 'row.names' are not allowed" error for a small CSV. The problem was that somewhere outside of the 14x14 chart area I wanted there was a random cell with a space/other data.

Discovered the answer when I ran it "row.names = NULL" and there were multiple rows of blank data below my table (and therefore multiple duplicate row names all "blank").

Solution was to delete all rows/columns outside the table area, and it worked!

小…红帽 2024-10-07 09:48:14

就我而言,问题来自 Excel 文件。虽然它看起来组织得很完美,但它不起作用,我总是收到消息: Error in read.table(file = file, header = header, sep = sep, quote = quote, :重复的 'row.names' 是不允许。

我尝试将我的 Excel 矩阵复制粘贴到一个新的空 Excel 工作表中,然后我重新尝试读取它:它有效了!不再有错误消息!

in my case the problem came from the excel file. Although it seemed perfectly organized, it did not worked and I had always the message: Error in read.table(file = file, header = header, sep = sep, quote = quote, : duplicate 'row.names' are not allowed.

I tried to copy-paste my excel matrix to a new empty excel sheet and I retried to read it: it worked ! No error message anymore !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文