Scan and Read Data from a File in R Programming – scan() Function

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# take input from keyboard
z <- scan() 
1
2
3
4
   
# print output on the screen
print(z)

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.