3.3 Load/import your data into R-studio


R reads data as objects. For example number, vector, dataframe or matrix. I our case, we will mainly work with  dataframes.

There are several ways of importing data from text, csv or excel files, which are described here using R without R-Studio.

I recommend importing from text files (.txt). If it works, importing directly from excel is the fastest, especially if you edit your excel file to add or remove data and want to re-run your script. But it is easier to run into trouble with the cell-formats. Find out for yourself what you prefer.

For importing a text file you need the following line of code:

[code language=”r”]Data_in <- read.table("C:/Users/x/AB202/R/plankton.txt", header=TRUE, sep="\t", dec=",")[/code]

In this code we specify first the file path, or location of the file to open: “C:/Users/x/AB202/R/plankton.txt”. Then we tell R that the first row of the file are the column names: header=TRUE. Further we tell R that the file is tab-delimitated (sep=”\t”) and that decimal places in our original file are seperated by comma (dec=”,”).

After importing your dataframe, it should appear in the environment-tab, with the name you assigned, number of observation and variables in the dataframe:

By clicking the small blue button, you can inspect details of the variables in the dataframe:

Double-click on the data-object will open the dataframe in a separate tab (close to the editor tab):

Now you can start working with your data!

  Fant du det du lette etter? Did you find this helpful?
[Average: 0]