Entering Data
Site: | Saylor Academy |
Course: | PRDV420: Introduction to R Programming |
Book: | Entering Data |
Printed by: | Guest user |
Date: | Tuesday, July 1, 2025, 1:27 AM |
Description
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.
Entering Data
Source: Datalab, https://www.youtube.com/watch?v=bXdMQ-8ZDBg This work is licensed under a Creative Commons Attribution 3.0 License.
How to Manually Enter Raw Data in R?
This article will discuss manually entering raw data in the R Programming Language. In the R Language, we work with loads of different datasets by importing them through a variety of file formats. But Sometimes, we need to enter our own raw data in the form of a character vector, a data frame, or a matrix. Multiple methods exist to enter the raw data in the R Language manually.
Enter data as a vector
To enter data as a vector in the R Language, we use the combine function i.e. c(). The c() function is a generic function that combines its arguments to form a vector. All arguments are coerced to a common type. To create a numeric vector we pass numbers as arguments to the c() function. To create a character vector we pass the strings or characters as arguments to the c() function.
Syntax: sample_vector <- c( data1, data2, ….. , datan )
where: data1, data2…: determines the numeric values that comprise the vector.
Example: Demonstrating basic character and numeric vectors.
R
# create numeric vector
numeric <- c(1,2,3,4,5)
# create character vector
character <- c("geeks", "for", "geeks")
# print vectors and their class
print("Character vector:")
character
print("Class:")
class(character)
print("Numeric vector:")
numeric
print("Class:")class(numeric)
Output:
Character vector: "geeks" "for" "geeks" Class: "character" Numeric vector: 1 2 3 4 5 Class: "numeric"
Enter data as a data frame
To enter data as a data frame in the R Language, we use the data.frame() function. The data.frame() function creates data frames that are tightly coupled collections of variables. These data frames are widely used as the fundamental data structure in the R Language. A single data frame can contain different vectors of different classes together thus it becomes one data structure for all the needs.
Syntax:
data_frame <- data.frame( column_name1 = vector1, column_name2 = vector2 )
where,
- column_name1, column_name2: determines the name for columns in data frame
- vector1, vector2: determines the data vector that contain data values for data frame columns.
Example: Basic data frame that contains one numeric vector and one character vector.
R
# create data frame
data_frame <- data.frame( id = c(1,2,3),
name = c("geeks", "for",
"geeks") )
# print dataframe, summary and its class
print("Data Frame:")
data_frame
print("Class:")
class(data_frame)
print("Summary:")
summary(data_frame)
Output:
Data Frame: id name 1 1 geeks 2 2 for 3 3 geeks Class: "data.frame" Summary: id name Min. :1.0 Length:3 1st Qu.:1.5 Class :character Median :2.0 Mode :character Mean :2.0 3rd Qu.:2.5 Max. :3.0
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