# Importing Data in R Programming – Easy to Follow Guide for Beginners!

In this tutorial, we will learn how to import data from various sources into the R programming environment. We will make use of the commands in R such as c(), scan(). Also, we will draw a comparison between read.csv() and read.csv2() in R for importing the various CSV files. Furthermore, we will explore the read.delim() function for importing the data. So, let’s quickly start importing data in R.

Keeping you updated with latest technology trends, Join DataFlair on Telegram

## Process of Importing Data in R

Now, let’s explore the steps to import data in R:

### 1. Using the Combine Command

In R, we make use of c() function to combine or concatenate various data values together.

```> #Author DataFlair
> vector1 <- c(1,2,3)
> vector2 <- c(4,5,6)
> vector3 <- c(7,8,9)
> combined_vector <- c(vector1, vector2, vector3)
> combined_vector
```

Output: In the above example, vector1, vector2 and vector3 store integer values separately. To combine these values together, we make use of the c() function, as demonstrated above.

Before proceeding ahead, first, complete the Binomial and Poisson Distribution in R

### 2. Entering Numerical Items as Data

We can simply enhance numerical data by typing the values separated by commas into the c() command.

Let us create a data set by using the below command:

```> #Author DataFlair
> data1 = c(3, 5, 7, 5, 3, 2, 6, 8, 5, 6, 9)
> data1
```

We will create an object data1 that stores our data. Then, type our values between the two parentheses and these values will be separated by commas. In order to display the dataset, type ‘data1’.

After that, carry out the same with the data object – data2. In this, we will also specify data1 as one of the member components.

```> data2 = c(data1, 4, 5, 7, 3, 4)
> data2```

Output: ### 3. Entering Text Items as Data

In order to enter character data, we make use of single quotes or double-quotes. Whatever is covered by these quotes is interpreted like a character type or a text item.  In the following example, we will take our data in the form of characters as the days of a week. We will store them in the day1 object. Then, we will pass day1 into the same vector along with another text item. However, day1, in this case, is not text but numerical in nature. If there is a combination of numbers and text, R converts the number into text.

```> #Author DataFlair
> day1 = c('Mon', 'Tue', 'Wed', 'Thu')
> day1
> day1 = c(day1, 'Fri')
> day1
```

Output: Don’t forget to check the Numeric and Character Functions in R

### 4. Using the scan() command

Instead of typing input data with the additional specification of commas, we can use the scan() command that does not require you to enter a comma after every input data. scan() can also be used for taking data from files as well as with the clipboards.

scan() command does not take any input between its parentheses. It simply invokes a prompt through which you enter the data.

Let us see this with the help of an example:

```data <- data.frame(x1 = c(1, 2, 3, 4),
x2 = c(5, 6, 7, 8),
x3 = c(9, 10, 11, 12))
data

write.table(data, file = "data.txt", row.names = FALSE)
getwd()
```

Output: In the above example, we created a data frame which is then stored as a file on the local disk called ‘data.txt’. This file can be accessed using the scan function as follows:

```> #Author DataFlair
> scan_data <- scan("data.txt", what = "character")
> scan_data
```

Output: ### 5. Using the Clipboard to Make Data

We can use the clipboard to copy and paste the data more interactively. With the scan() command, we can enter the input data such as spreadsheets.

Following are the key steps for importing spreadsheet data into R:

1. If the spreadsheet contains numerical data, then prior to switching to this spreadsheet, type command in R.
2. We copy the important cells to the clipboard after highlighting them.
3. After returning to R, paste the data from clipboard into R. Then, R waits until an empty line is entered before halting the data entry process in order to facilitate data copying and pasting as required.
4. Finally, a blank line is entered to complete the data entry procedure.

If there are spaces separating the data, then perform simple copy and paste. However, if some other symbol or character is separating the data, then we need to input that into R prior to data import.

Take a deep dive into R Data Visualization

### 6. Using Scan() to Retrieve Data from CSV file

We can use the scan() command to retrieve data from a CSV file. We will save our previously created data frame ‘data’ as a CSV file.

```write.table(data, file = "data.csv",
row.names = FALSE)
getwd()
```

In the next step, we scan our CSV file and specify the what attribute with ‘character’.

```write.table(data, file = "data.csv",
row.names = FALSE)
getwd()
scan_csv <- scan("data.csv", what = "character")
scan_csv
```

Output: This separator needs to be enclosed in quotes. In order to finish the data entry, press Enter.

### 7. Reading a File of Data from a Disk

We can use the scan() command to get data file from the local memory of our system.

With the help of scan(), data can be read from a console and written into a vector. We add the file name in the scan() function as follows:

```#Author DataFlair
Object_Name <- scan("data.txt", what = "character")
Object_Name
getwd()
setwd('Desktop')
getwd()

```

Output: You must definitely explore the Types of R Vector Functions

### 8. Reading Bigger Data Files

Now, we will learn how to read bigger data files in R.

In the above sections, we used the scan() command to read data from simple files. In R, we can enter a large amount of data that contain complicated data. There are various means and measures to read such large data that is stored in a variety of text formats.

• From tables with: > read.table()
• In order to read from files that contain values separated by tabs: > delim()

read.csv() and read.csv2() both are used to read csv but the former makes use of ‘,’ while the latter utilises the separator ‘;’

This was all about importing data into R. We hope that you liked reading our article!

## Summary

In this R tutorial, we studied different methodologies of importing data into the R environment. We studied various commands that can be used for data import and understood their implementation with code examples.

After importing, it’s time for Exporting Data from R to CSV File

Still, if you have a query regarding Importing Data in R, ask in the comment section.