5
votes

I'm new, and I have a problem:

I got a dataset (csv file) with the 15 columns and 33,000 rows.

When I view the data in Excel it looks good, but when I try to load the data into R- studio I have a problem:

I used the code:

x <- read.csv(file = "1energy.csv", head = TRUE, sep="")
View(x)

The result is that the columnnames are good, but the data (row 2 and further) are all in my first column.

In the first column the data is separated with ; . But when i try the code:

x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";")

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

So i made the code:

x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";", row.names = NULL)

And it looks liked it worked.... But now the data is in the wrong columns (for example, the "name" column contains now the "time" value, and the "time" column contains the "costs" value.

Does anybody know how to fix this? I can rename columns but i think that is not the best way.

7
Can you show some of the data?Heroka
According to your error, it's likely the header and values are not the same size, or you have a blank header somewhere (or header with spaces). Show the 5 first lines of your file.Tensibai

7 Answers

6
votes

Excel, in its English version at least, may use comma as separator, so you may want to try

x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=",")

I once had a similar problem where header had a long entry that contained a character that read.csv mistook for column separator. In reality it was a part of a long name that wasn’t quoted properly. Try skipping header and see if the problem persists

x1 <- read.csv(file = "1energy.csv", skip = 1, head = FALSE, sep=";")

In reply to your comment: Two things you can do. Simplest one is to assign names manually:

myColNames <- c(“col1.name”,”col2.name”)
names(x1) <- myColNames

the other way is to read just the name row (the first line in you file) read only the first line, split it into character vector

nameLine <- readLines(="1energy.csv", n=1)
fileColNames <- unlist(strsplit(nameLine,”;”))

then see how you can fix the problem, then assign name s to your x1 data frame. I don’t know what exactly is wrong with your first line, so I can’t tell you how to fix it.

Yet another cruder option is to open your csv file using a text editor and edit column names.

3
votes

It happens because of Exel's specifics. The easy solution is just to copy all your data Ctrl+C to Notepad and Save it again from Notepad as filename.csv (don't forget to remove .txt if necessary). It worked well for me. R opened this newly created csv file correctly, all data was separated at columns right.

0
votes

This problem can arise due to regional settings on the excel application where the .csv file was created.

While in most places a "," separates the columns in a COMMA separated file (which makes sense), in other places it is a ";"

Depending on your regional settings, you can experiment with:

x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=",") #used in North America

or,

   x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";") #used in some parts of Asia and Europe 
0
votes

You can transform the data by arranging the data into many cells corresponding to columns.

1.Open your csv file 2.copy the content and paste it into txt file save and copy its content

3.open new excell file 4.in excell go to the section responsible for data . it is acually called "Data" 5.then on the left side go to external data query , in german "externe Daten abfragen" 6.go ahead step by step and seperate by commas 7. save your file as csv

0
votes

Open your file in text edit and see if it really is separated with commas... Sometimes .csv files are separated with tabs instead of commas or semicolon and when opening in excel it has no problem but in R you have to specify the separator like this:

x <- read.csv(file = "1energy.csv", head = TRUE, sep="\t")

I once had the same problem, this was my solution. Hope it works for you.

0
votes

You could use -

df <- read.csv("filename.csv", sep = ";", quote = "")

It solved one my problems similar to yours.

-3
votes

I had the same problem and it was frustrating...

However, I found the ultimate solution First take this (csv file) and then convert it online to Json file and download it ... then redo the whole thing backwards (re-convert Jason to csv) online... download the converted file... give it a name...

then put it on your Rstudio

file name <- read.csv(file='name your file.csv') ... took me 4 days to think out of the box... 🙂🙂🙂