create a dataframe in r

In R programming, data frames are used to store tabular data in the form of rows and columns. Here's an example of how to create a data frame:

main.r
# Create a data frame
df <- data.frame(
  name = c("John", "Emily", "Michael", "Jessica"),
  age = c(25, 30, 21, 29),
  gender = c("M", "F", "M", "F"),
  stringsAsFactors = FALSE # optional argument to avoid converting character vectors to factors
)

# Print the data frame
print(df)
284 chars
11 lines

Output:

main.r
     name age gender
1    John  25      M
2   Emily  30      F
3 Michael  21      M
4 Jessica  29      F
105 chars
6 lines

In the above example, we created a data frame df with four columns name, age, gender, and stringsAsFactors. You can add additional rows or columns to the data frame using various data manipulation functions available in R. Once you have the data organized in a data frame, you can use various data analysis and visualization functions to extract insights from the data.

gistlibby LogSnag