Scan and Read Data from a File in R Programming - scan() Function Last Updated : 30 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report scan() function in R Language is used to scan and read data. It is usually used to read data into vector or list or from file in R Language. Syntax: scan("data.txt", what = "character") Parameter: data.txt: Text file to be scanned Returns: Scanned output Example 1: Scanning of Text r # R Program to scan a file # Create example data.frame data <- data.frame(x1 = c(1, 2, 2, 3), x2 = c(4, 5, 6, 7), x3 = c(8, 9, 10, 11)) data # Write data as txt file to directory write.table(data, file = "data.txt", row.names = FALSE) # Get currently used directory getwd() # Apply scan function to txt file data <- scan("data.txt", what = "character") data Output : x1 x2 x3 1 1 4 8 2 2 5 9 3 2 6 10 4 3 7 11 [1] "/home/cg/root/8121844" [1] "x1" "x2" "x3" "1" "4" "8" "2" "5" "9" "2" "6" "10" "3" "7" "11" Read 15 items Example 2: Scan data as text r # R Program to scan a file # Create example data.frame data <- data.frame(x1 = c(1, 2, 2, 3), x2 = c(4, 5, 6, 7), x3 = c(8, 9, 10, 11)) data # Write data as txt file to directory write.table(data, file = "data.txt", row.names = FALSE) # Read txt file into list data <- scan("data.txt", what = list("", "", "")) data Output: x1 x2 x3 1 1 4 8 2 2 5 9 3 2 6 10 4 3 7 11 [[1]] [1] "x1" "1" "2" "2" "3" [[2]] [1] "x2" "4" "5" "6" "7" [[3]] [1] "x3" "8" "9" "10" "11" Read 5 records Example 3: Skip lines with scan function r # R Program to scan a file # Create example data.frame data <- data.frame(x1 = c(1, 2, 2, 3), x2 = c(4, 5, 6, 7), x3 = c(8, 9, 10, 11)) data # Write data as txt file to directory write.table(data, file = "data.txt", row.names = FALSE) # Skip first line of txt file data <- scan("data.txt", skip = 1) data Output: x1 x2 x3 1 1 4 8 2 2 5 9 3 2 6 10 4 3 7 11 [1] 1 4 8 2 5 9 2 6 10 3 7 11 Read 12 items Example 4: Scan Excel CSV File r # R Program to scan a file # Create example data.frame data <- data.frame(x1 = c(1, 2, 2, 3), x2 = c(4, 5, 6, 7), x3 = c(8, 9, 10, 11)) data # Write data as csv file to directory write.table(data, file = "data.csv", row.names = FALSE) # Apply scan function to csv file data <- scan("data.csv", what = "character") data Output: x1 x2 x3 x1 x2 x3 1 1 4 8 2 2 5 9 3 2 6 10 4 3 7 11 Read 15 items [1] "x1" "x2" "x3" "1" "4" "8" "2" "5" "9" "2" "6" "10" "3" "7" "11" Example 5: Read input from the keyboard. r # take input from keyboard z <- scan() 1 2 3 4 # print output on the screen print(z) Output: Read 4 items [1] 1 2 3 4 Here, in the above code, the scan() function will take input from the user and print value using the print() function. Comment More infoAdvertise with us Next Article Scan and Read Data from a File in R Programming - scan() Function K kaurbal1698 Follow Improve Article Tags : R Language R Functions R-FileHandling R-Input/Output Similar Reads Read Lines from a File in R Programming - readLines() Function readLines() function in R Language reads text lines from an input file. The readLines() function is perfect for text files since it reads the text line by line and creates character objects for each of the lines. Syntax: readLines(path) Parameter: path: path of the file Example 1: Python3 # R progra 2 min read Reading Tabular Data from files in R Programming Often, the data which is to be read and worked upon is already stored in a file but is present outside the R environment. Hence, importing data into R is a mandatory task in such circumstances. The formats which are supported by R are CSV, JSON, Excel, Text, XML, etc. The majority of times, the data 4 min read Reading contents of a Text File in R Programming - read.table() Function The read.table() function in R can be used to read a text file's contents. A versatile and often used function for reading tabular data from different file formats, including text files, is read.table(). It returns the data in the form of a table.Syntax: read.table(filename, header = FALSE, sep = "" 2 min read How To Import Data from a File in R Programming The collection of facts is known as data. Data can be in different forms. To analyze data using R programming Language, data should be first imported in R which can be in different formats like txt, CSV, or any other delimiter-separated files. After importing data then manipulate, analyze, and repor 4 min read Read contents of a CSV File in R Programming - read.csv() Function read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, sele 3 min read Like