简体   繁体   English

在csv中读取的列多于R中的列名

[英]Read in csv with more columns than column names in R

So I am trying to read a csv into R, and if I use 所以我试图将csv读入R,如果我使用

data = read.csv("2013_NBAseason.csv", header = T)

I get an error 我得到一个错误

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
duplicate 'row.names' are not allowed"

Which is because the dates aren't unique because multiple games are played everyday. 这是因为日期不是唯一的,因为每天都会玩多个游戏。 Thus, I tried removing the last column using this , but I still get the same error. 因此,我尝试使用this删除最后一列,但仍然出现相同的错误。

The cause of the problem, I think after reading this is because my last column doesn't have a header 问题的原因,我认为在阅读本文之后是因为我的最后一列没有标题

Thus I have done this 因此,我做到了

data = read.csv("2013_NBAseason.csv", header = T, 
                 colClasses=c(rep(NA,7),"NULL"), row.names=NULL)

Now I have a dataframe that has all my column names shifted over and an empty column to the right 现在我有一个数据框,所有列名都移了,右边有一个空列

head(data)
          row.names      Date          Box.Score Away          Away_Points Home  Home_Points
1 Tue, Oct 30, 2012 Box Score Washington Wizards   84  Cleveland Cavaliers   94
2 Tue, Oct 30, 2012 Box Score   Dallas Mavericks   99   Los Angeles Lakers   91
3 Tue, Oct 30, 2012 Box Score     Boston Celtics  107           Miami Heat  120
4 Wed, Oct 31, 2012 Box Score   Sacramento Kings   87        Chicago Bulls   93

What is the best way to solve this, or to avoid the problem to start with? 解决此问题或避免出现问题的最佳方法是什么?

Also if someone tells me how to add the csv, I can upload it so that you guys can see the raw data. 另外,如果有人告诉我如何添加csv,我可以上传它,以便你们可以看到原始数据。

Also, manually changing the csv won't work, because this needs to be extrapolated to many more csvs with something like this 另外,手动更改csv将不起作用,因为需要使用类似这样的方法将其推断到更多csv

temp = list.files(pattern="*.csv")
data = do.call("rbind", lapply(temp, read.csv, ...

Why don't you try not using header = T 为什么不尝试不使用header = T

do this: 做这个:

#read data without any row names
data <- read.csv("2013_NBAseason.csv")

#enter string "home_points" to last column. I am assuming it is column 6.
data[1, 6] <- "Home_Points"

#make row 1, your column names
colnames(data) = data[1, ]

Does the above solve it? 以上解决了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM