When you execute R, it runs in a given folder in your computer (the working directory). You can display the working directory being used with:
getwd()
## [1] "/media/ajesusmp/328b5268-844c-4e12-b24c-44c11a4a02b7/GoogleDrive/EEG/R_sesiones"
To change the working directory use:
setwd("path/in/your/computer")
IMPORTANT NOTE: the path must be within quotes. In Windows, forward slash (/, instead of the backslash ) must be used.
Thus, there are three ways of interacting with any file in our computer (doesn't mind if it is an excel or any othe file):
1.- Changing R working directory to the folder containing the file:
setwd("path/in/your/computer/containing/the/file")
2.- Moving the file to the R working directory (use getwd() to be sure of the working directory and copy-paste the file tho this folder)
After doing any of the two options above, the folder containing the file and the working directory will be the same. In this case, we can read a file using its name (IMPORTANT: for windows users, it is possible that you will not see the complete name, because ".txt",".xlsx",.. are usually not shown by default. You HAVE TO USE THE ENTIRE NAME of the file, including these extensions).
There are three ways of reading an excel file:
Using a package containing a function to read excel files (if you want to do it, just google it)
Exporting the excel file to a plain txt file. For that, you can find within the menus the way to export the excel to a csv file. Doing so, you have to define several things such as the character used to separate columns in the new file (usually changing the "," -by default- to TAB "" can be useful). Another way of doing that is to copy the table you want to export from the excel file and paste it directly into an empty text file. In both cases you will generate a new text file containing the information you had in the excel file. You can read it using the read.table() function. There, you must provide the name of the file (mandatory) within quotes! and also set other important arguments, such as the character used to separate columns (",", "", etc.., use sep="" for that), the existence (or not) of columns names in the first row of the file (use header=T or header=F for that), the character used as decimal separator ("." by default, can be changed using dec=""), etc. Take a look on the help file to see all the options available and to understand, at least, the few mentioned above (sep, header, dec).
?read.table
To read a file use:
read.table(file="FileName.whatever")
This will read and print the file, but remember that you must create an object (a data.frame in this case) if you want to work with this table afterwards:
my.tab<-read.table(file="FileName.whatever")
If you do not use any other argument, all are defined by default. If default values match with your preferences, everything is fine. Otherwise, the data.frame you will produce will be wrong. For that reason, after reading a file, you must check the format of your dataframe. For that, use:
r # This will show the structure of the object str(my.tab) # This will show the entire data.frame my.tab
If the file does not look as you wish, change the arguments accordingly.
In this link you can download several files you train. These files are plain text tables, differing in the separator, the presence of header, the character used as decimal separator, etc. Try to properly read all of them (NOTE: Example number 4 is saved in three versions, differing in the code using for missing data, being NA, -99, and NotMeasured, respectively)
There is a third way of reading a file. In this case you will not need either change wd nor file folder:
3.- Use the entire path of the file:
my.tab<-setwd("path/in/your/computer/containing/the/file")
There is a third way of reading an excel file.
my.tab<-read.table(file="clipboard")
As you may guess, this will save information from the clipboard as a data.frame with the name my.tab.