It can be a good idea to put down a few values directly in your code to create an object to try things on. First, you can use this new "synthetic" dataset to write more code while waiting for the real data. Second, you can use this dataset to debug your code (find the source of an error and fix it). When you complete this section, you will know several ways of creating data objects manually.
How to Manually Enter Raw Data in R?
Enter data as a matrix
To enter data as a matrix in the R Language, we create all the columns of the matrix as a vector and then use the column binding function that is cbind() to merge them together into a matrix. The cbind() function is a merge function that combines two data frames or vectors with the same number of rows into a single data frame.
Syntax: mat <- cbind( col1, col2 )
where, col1, col2: determines the column vectors that are to be merged to form a matrix.
Example:
Here, is a basic 3X3 matrix in the R Language made using the cbind() function.
R
# create 3 column vectors with 3
# rows each for a 3X3 matrix
col1 <- c(1,2,3)
col2 <- c(4,5,6)
col3 <- c(7,8,9)
# merge three column vectors into a matrix
mat <- cbind(col1, col2, col3)
# print matrix, its class and summary
print("Matrix:")
mat
print("Class:")
class(mat)
print("Summary:")
summary(mat)
Output:
Matrix: col1 col2 col3 [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 Class: "matrix" "array" Summary: col1 col2 col3 Min. :1.0 Min. :4.0 Min. :7.0 1st Qu.:1.5 1st Qu.:4.5 1st Qu.:7.5 Median :2.0 Median :5.0 Median :8.0 Mean :2.0 Mean :5.0 Mean :8.0 3rd Qu.:2.5 3rd Qu.:5.5 3rd Qu.:8.5 Max. :3.0 Max. :6.0 Max. :9.0