Create a database in R

Create a database in R

6/4/20232 min read

We want to create a database with 4 students, from 4 different universities. We want to have student's ID, student's name, university name, age of each student and their scores.

Create a database in R

When we want to create a variable, first, we have to name it, let's name our variable "name". The name of the variable, should be followed by the
' <- ' operator, which indicates that the word on the left is the name of the object we are creating.

Next, after the <- operator, we have to write c(). This c() stands for combine, that means that we want to combine everything we put in bracket. If we want to combine numbers, it is enough to write numbers and separate them with comas, for example to create the variable 'age' we should write: c(21, 23, 21, 24). If we want to combine words, or names, or characters like letters, we have to remember to put them in (single or double) quotation marks. Since we want to include the names of the students, we have to use quotation marks and write: c('Jill', 'David', 'Chris', 'Ana').

So our first variable would look like this: name <- c('Jill', 'David', 'Chris', 'Ana').

To create our other variables, we have to include the following code:

ID <- c(1,2,3,4)

university <- c('Stanford', 'Harvard', 'Princeton', 'UCLA')

age <- c(21,23,21,24)

score <- c(10.2, 11.7, 14.3, 19.2)

Now, when we have created 5 variables, we need to combine it all together to build a dataframe. First, we need to name our dataframe. Let's say we want it to be called 'students'. We want R to combine it to dataframe format, so we're going to use a dataframe() function, and indicate that the 'students' dataframe needs to include our 5 variables. The code will be as follows:

students <- dataframe(ID, name, university, age, score)

Now, to see if our dataframe looks as we want it to look, write the following code:

print(students)

If you want, you can create a dataframe and variables at once, to do it you should write the following code: