Excel file import problem

Hello, I really need help importing excel file as for some reason R studio is not recognizing it as a xls or an xlsx file, can someone please help me with this?

Welcome to the forum.
Can you give us the code you are using?

Are you getting any errors?

Are you sure you have the correct file path?

This will show your current, working directory

getwd()

For general information on asking questions here see FAQ Asking Questions

Welcome to the community @Wlang16. Any chance the file is .csv instead of .xls or .xlsx? If the file is .xls or .xlsx, can you share the code you are using to import (and possibly the file itself)?

Here is the code that I am using

ratings <- read.csv('ratings_seasons.csv')

ratings <- read.csv('ratings_seasons.csv')
View(ratings)

As you can see it is a csv file so therefore, how would I import this data?

What happens when you execute View(ratings) does it show an error?
If there is no error you have correctly imported the data.

If it says Error in View : object 'ratings' not found then it means that the file is not in your working directory. You have two options

  1. put the command setwd(...) to the directory where the file is before calling the read command or
  2. put the reference to the directory in the command:
    ratings <- read.csv('C:\......\ratings_seasons.csv')

I hope this helps.

Okay, that code should word if you you are reading a .csv file but you sad that you are trying to read an Excel file?

Have a look at @ scottyd22's question. What is the file extension on your file?

In what folder (directory) is the file 'ratings_seasons.csv'? Does the file exist? Could it be 'rating_seasons.xlsx' instead?
What is the output if you do

getwd()

If it is an Excel file with an Excel extension as @ scottyd22 points out you can install a package to read Excel files.

Try this

install.packages(" readxl")
library(readxl)
ratings  <- read_excel("ratings_seasons")
ratings

Hi Rolando, what do you put in the spaces? (the ... in between the commands)

For setwd(...) you need to put in the filepath.

For example if your file is in C:\Rwork you need

setwd(C:/Rwork)

Then you should be able to read in the file with

ratings <- read.csv("ratings_seasons.csv")

otherwise try

ratings <- read.csv('C:/Rwork/ratings_seasons.csv')

Note that in R we use / rather than \ as the path separator.

Hi William (@Wlang16 ),
As @jrkrideau says you should put the file path there.
An example in my system would be:

setwd("~/Rolandos files/R_projects/macro_analysis")

I only work in MacOS and Linux and the path separators are / but I suspect that in Windows it is \
I hope this helps.

@ Rolando

No re \ . R requires a Linux or Mac / separator. One must escape \ for it to work

So we would need something like

read.csv("C:\\Rwork\\ratings_seasons.csv")

I had to run Windows on a corporate machine and it drove me crazy.

1 Like

You can use / on all platforms. No need for \\ on Windows.

Ah, thanks. It was a long time ago when I was enduring Windows.

But what I meant was that if you do not use / then you need to escape the \ .

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.