Read.csv error message

Hi,

I'm trying to create a cluster chart/barplot. I have the data in a csv file in excel which I've imported. So I've tried the read.csv function but it isn't working. Can anyone assist me please? This is the code I've tried -

read.csv(filename, header = TRUE, sep = ",", quote = """, dec = ".", fill = TRUE, comment.char = "",)

I'm getting this error message -

Error in read.table(file = file, header = header, sep = sep, quote = quote, :
'file' must be a character string or connection

Any help would be great. Thanks.

Hi @purpleforest ,

I have changed the quote value from """" to '"', since the " symbol is used to indicate double quotes when reading CSV files in R.

read.csv(filename, header = TRUE, sep = ",", quote = '"', dec = ".", fill = TRUE, comment.char = "")

1 Like

Hi

Thanks for the reply. Unfortunately, that doesn't work. I actually inputted the code above slightly wrong. The quote bit was wrong. It should actually look more like this that I'm doing at the moment -

read.csv(file, header = TRUE, sep = ",", quote = """, dec = ".", fill = TRUE, comment.char = "")

Thanks

It's not put it in again for some reason. Let me try typing it out.

read.csv(filename, header = TRUE, sep = ",", quote """, dec = ".", fill = TRUE, comment.char = ""). Just in case it doesn't appear again, there should be a backslash between the first and second quotation marks in the quote part.

I suspect the problem is with the value you are using for filename. I can reproduce your error if I put a NULL in place of the file name.

read.csv(NULL)
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  'file' must be a character string or connection

Please show how you build the value for filename.

Hi

I can't add the filename because its part of a project. But I'm just typing in the filename. When I add .csv to the end of it, I get a + sign on the next line, but still no graph

The + sign after you execute the code shows that R is still looking for the end of the command. This typically means you have an unmatched parenthesis. Here is an example of that

> read.csv("Dummy.csv" 
+ 

Let's simplify your command. The arguments you are including in read.csv() are the same as the default values, so you do not need to set them. Here are the default values for read.csv()

read.csv(file, header = TRUE, sep = ",", quote = "\"",
         dec = ".", fill = TRUE, comment.char = "", ...)

Try running

tmp <- read.csv("YourFileName.csv")

using the name of your file including the .csv file extension name and having quotation marks around the file name.

Hi,

Thanks for the reply.

I gave that a try but it came up with a new error which is below -

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'filename.csv': No such file or directory

The error message means the file is not in your current working directory. You can see what your working directory is by running

getwd()

If the file is in a sub directory of the working directory, you can just include that sub directory in the read.csv() call. If the file is in the Data sub directory of the working directory, run

tmp <- read.csv("Data/YourFileName.csv")

If the file is somewhere else, just write the full directory path. On Windows that might look like

tmp <- read.csv("C:/Users/purlpleforest/Documents/YourFileName.csv")

Hi,

Thanks again for your help.

I tried the getwd and got a path. I then used the template above (tmp <- read.csv followed by the path from the getwd and my filename.csv), but it didn't work again. It produces the same error message.

You might enter
list.files()

to see if the file you are looking for is in your working directory.

@purpleforest - It's very hard to say what the problem is if all we know is the error message. What is the full path to your data file? What command do you run? What is the result of running getwd()?

Hi,

I may make a new post for this too as it's a totally different issue, but I've now got it to recognise the file I think - it wasn't a csv, that was the problem, it was an excel file - xslx, so I needed read_xslx instead of read.csv. But my new issue is that the barplot is not recognising the columns so it's not producing a chart. If you know why this might be, that'd be great. Thanks

Please post the actual code and some or all of your data. There are several ways to make a bar chart. To post data, you can use the dput() function. If your data frame is named DF, run

dput(DF)

Copy the output of that and paste it into a reply here. Put a line with three back ticks before and after the pasted output, like this
```
output from dput() goes here
```
If the data frame is too big to post the whole data set, use the head function to select a subset of row. For 20 rows, use

dput(head(DF, 20))

Adjust the number of rows as appropriate for your data.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.