在 R 中读取具有重复行名称的 csv 文件
我正在尝试读取具有重复行名称的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该函数看到重复的行名称,因此您需要处理它。最简单的方法可能是使用 row.names=NULL,它将强制行编号 - 换句话说,它将您的第一列视为第一个维度,而不是第一个维度行号,因此添加行号(以“1”开头的连续整数。
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".
试试这个:
try this:
猜测你的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
我最近遇到的一个问题是标题行中的列数与数据本身的列数不匹配。例如,我的数据是制表符分隔的,所有数据行都有一个尾随制表符。标题行(我手动添加的)没有。
我希望对行进行自动编号,但它却将我的第一行视为行名称。来自文档(重点是我添加的):
在标题行中添加一个额外的制表符,使标题行的列数与数据行的列数相同,从而解决了问题。
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):
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.
简而言之,检查您的列名称。如果您的第一行是列的名称,则可能会缺少一个或多个名称。
示例:
上面的示例将导致 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:
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.
对于小型 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!
就我而言,问题来自 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 !