English 中文(简体)
姓名不允许错误
原标题:duplicate row.names are not allowed error
  • 时间:2012-01-13 16:41:46
  •  标签:
  • r
  • csv
  • r-faq

我正试图装上一个有14栏的剪辑:

StartDate, var1, var2, var3, ..., var14

当我发出这一指令时:

systems <- read.table("http://getfile.pl?test.csv", header = TRUE, sep = ",")

我收到错误的信息。

姓名不允许。

在我看来,第一栏名正在引起这一问题。 当我手工下载档案和从档案中删除<编码>StartDate名称时,R成功地读了文档,并将第一栏改为。 X 。 谁能告诉我什么是正在做的? 档案是一份(通勤)文件。

最佳回答

之后改为:not to use row.names:

systems <- read.table("http://getfile.pl?test.csv", 
                      header=TRUE, sep=",", row.names=NULL)

而现在,你们的行文将只是编号。

并参看read.csv, 它是read.table 的包裹,其中已经确定了sep= ,header=TRUE 的论点,以便你的发言简化。

systems <- read.csv("http://getfile.pl?test.csv", row.names=NULL)
问题回答

This related question points out a part of the ?read.table documentation that explains your problem:

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.

页: 1 可以通过使用以下两种解决办法之一来解决这一问题:

  1. adding a delimiter (ie or ,) to the front or end of your header row in the source file, or,
  2. removing any trailing delimiters in your data

The choice will depend on the structure of your data.

test.csv Example:
If your test.csv looks like this:

v1,v2,v3 
a1,a2,a3,
b1,b2,b3,

By default, read.table interprets this file as having one fewer header columns than the data because the delimiters don t match. This is how it is interpreted by default:

   v1,v2,v3   # 3 items!! (header row)
a1,a2,a3,  # 4 items
b1,b2,b3,  # 4 items

第一栏(无标题)的数值为行文:<代码>a1b1。 如果该栏含有完全可能的重复价值,那么,你可以取得<条码>复制的行文,姓名不允许错误。

如果你设定<条码>行号:姓名=FALSE,则头角的变动就没有发生,但主角和数据中的栏目仍然有误。

这就是如何用<代码>行文加以解释。

v1,v2,v3   # 3 items!! (header row)
a1,a2,a3,  # 4 items
b1,b2,b3,  # 4 items

Solution 1 Add trailing delimiter to header row:

v1,v2,v3,  # 4 items!!
a1,a2,a3,  # 4 items
b1,b2,b3,  # 4 items

或者,增加主角:

,v1,v2,v3  # 4 items!!
a1,a2,a3,  # 4 items
b1,b2,b3,  # 4 items

Solution 2 Remove excess trailing delimiter from non-header rows:

v1,v2,v3   # 3 items
a1,a2,a3   # 3 items!!
b1,b2,b3   # 3 items!!

在我的案件中,每一行的末尾都是 com。 取消工作

我在开立CSV档案时有这一错误,其中一个田地已经穿透。 实地引述了它,我已经删除了它中的“引用”。 一旦我引用了“”的话,“表号”的缺席行为就接过了这一问题并造成死亡。 因此,我来了:

systems <- read.table("http://getfile.pl?test.csv", header=TRUE, sep=",", quote="")

为此:

systems <- read.table("http://getfile.pl?test.csv", header=TRUE, sep=",")

http://www.sthda.com/english/wiki/fast-reading-of-data from-txt-csv-files-into-r-readr- Package” rel=“nofollow noreferer”

In my experience, the parameter row.names=NULL in the read.csv function will lead to a wrong reading of the file if a column name is missing, i.e. every column will be shifted.

解决这一问题。

造成这一错误的另一个可能原因是,你有整整整行重复。 如果是这样的话,问题通过删除重复增长来解决。

The response here ( https://stackoverflow.com/a/22408965/2236315) by @adrianoesch should help (e.g,sols “如果你知道一种解决办法,不需要你在评论中提及的宽松工作(一栏名称,复制数据),那将是巨大的,“......要求数据复制”由@Frank提议。

请注意,如果你在一些文本编辑中开张,你就应当看到标题栏下栏数少于栏数。 就我而言,该数据集在最后一个标题领域结束时丢失了“”。

问题似乎不止一个原因。 在我有同样的错误时,采取了两个步骤。

  1. I saved my file as MS-DOS csv. ( Earlier it was saved in as just csv , excel starter 2010 ). Opened the csv in notepad++. No coma was inconsistent (consistency as described above @Brian).
  2. Noticed I was not using argument sep="," . I used and it worked ( even though that is default argument!)




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...