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 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"