How to Manually Enter Raw Data in R?
Last Updated :
19 Dec, 2021
In this article, we will discuss how to manually enter 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. There are multiple methods to enter the raw data manually in the R Language.
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
Similar Reads
How to Read Many ASCII Files into R? Reading data from ASCII files into R is a common task in data analysis and statistical computing. ASCII files, known for their simplicity and wide compatibility, often contain text data that can be easily processed in R. Here we read multiple ASCII files into R Programming Language. What are ASCII F
4 min read
How to Install readr in Anaconda In R Programming Language readr is a powerful R package designed for reading and writing rectangular data, such as CSV files, with speed and efficiency. Anaconda, a comprehensive distribution for data science and machine learning, provides an easy way to manage R environments. This article provides
2 min read
How to Install readxl in Anaconda The readxl package can be an essential tool for data analysts and scientists who work with Excel files in R. This package can allow you to import Excel files directly into R. Making it easier to manipulate, analyze, and visualize the data. Installing the readxl with an Anaconda environment combines
3 min read
How to Write Entire Dataframe into MySQL Table in R In this article, we are going to learn how to write the entire data frame into a MySQL table in R programming language. To perform this task we need some basic requirements which are listed below: Install MySQLInstall R StudioBasic knowledge of SQL queries. Using SQL queries create a database "gfg_d
3 min read
How to Install tidyr in Anaconda The tidyr package is a crucial tool in the R programming language for data cleaning and tidying. If you're using Anaconda, a popular open-source distribution for Python and R, you can easily manage and install packages, including tidyr. This guide will walk you through the steps to install tidyr in
2 min read
How to Install data.table in Anaconda data. table is a highly optimized R package designed for fast and flexible data manipulation and aggregation. Anaconda is the distribution of Python and R for specific computing and data science, making it an ideal platform to manage and deploy packages like data. table. This article will provide a
3 min read