读入csv,列中的列数多于R中的列名

时间:2022-09-15 14:03:06

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.

这是因为日期不是唯一的,因为每天都会玩多个游戏。因此,我尝试使用此删除最后一列,但我仍然得到相同的错误。

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将无法正常工作,因为这需要外推到更多csvs这样的东西

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

1 个解决方案

#1


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?

以上解决了吗?

#1


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?

以上解决了吗?