The scan() function is used to read data from file in vector or list format in R.
The following method shows how you can do it with syntax.
Method 1: Use scan() Function
scan(file="name_of_file",what=type_of_data_to_read)
The following example shows how to use scan() function in R.
Use scan() function
Suppose I have Machine_data.csv file in my working directory and want to read data from file in list format:
# Read from CSV file into list format
data <- scan("Machine_data.csv", what = list("", "", "", "", ""))
# Show data
print(data)
Output:
[[1]]
[1] "Date,Machine_type,Status,Pressure,Temperature"
[2] "01-11-2023,Mach_A,FALSE,89,10.1"
[[2]]
[1] "21-10-2023,Mach_1,TRUE,11,45.6" "02-11-2023,Mach_B,TRUE,85,12.8"
[[3]]
[1] "22-10-2023,Mach_2,TRUE,12,47.2" "03-11-2023,Mach_C,FALSE,87,13.74"
[[4]]
[1] "23-10-2023,Mach_3,FALSE,10,46.1" "04-11-2023,Mach_D,FALSE,90,9.55"
[[5]]
[1] "24-10-2023,Mach_4,TRUE,11,46.85" ""
The output shows data from Machine_data.txt file in list format.
We can check data is list or not by using class() function:
# Check class of data
class(data)
Output:
[1] "list"
As we can see in output the class of data is list.