How to use the scan function in R for inputting data?
In R language, you can use the scan() function to input data.
The basic usage of the scan() function includes specifying parameters such as file, what, nmax, n, sep, dec, quote, skip, na.strings, flush, fill, strip.white, quiet, blank.lines.skip, multi.line, comment.char, allowEscapes, and encoding.
Here are the steps for entering data using the scan() function:
- perform a scan
- Enter data in the console, separating each datum with either a space or a newline character.
- Press the Enter key to finish inputting data after entering the final data.
- View the input data by printing the variable.
Here is an example:
# 输入整数型数据
x <- scan()
1 2 3 4 5
# 输出输入的数据
print(x)
Output:
[1] 1 2 3 4 5
In this example, we input a set of integer data using the scan() function and assign it to the variable x. By printing variable x, we can see the input data.