how to create a dataframe in r

To create a dataframe in R, we can use the data.frame() function.

Here's an example:

main.r
# Creating a sample dataframe
df <- data.frame(
  name = c("John", "Jane", "James", "Sophie"),
  age = c(25, 32, 18, 47),
  is_student = c(TRUE, FALSE, TRUE, FALSE)
)

# Printing the dataframe
df
196 chars
10 lines

In this example, we create a dataframe with three columns (Name, Age, and Is_Student) and four rows. We pass the values for each column as vectors to the data.frame() function.

Note that we assign the resulting dataframe to a variable df so that we can use it later in our code. We then print the dataframe using the print() function.

Output:

main.r
    name age is_student
1   John  25       TRUE
2   Jane  32      FALSE
3  James  18       TRUE
4 Sophie  47      FALSE
120 chars
6 lines

This is how we can create a simple dataframe in R using data.frame().

gistlibby LogSnag