create a dataframe from csv file in r

To create a dataframe from a CSV file in R, you can use the read.csv() function, which reads the data from a CSV file and converts it into a dataframe.

main.r
# set the file path to the CSV file 
file_path <- "path/to/your/csv/file.csv"

# read the CSV file into a dataframe
df <- read.csv(file_path)
142 chars
6 lines

By default, the first row of the CSV file is assumed to be the header of the dataframe. If your CSV file does not have a header row, you can set the header parameter to FALSE so that R will generate column names automatically.

main.r
# set the file path to the CSV file 
file_path <- "path/to/your/csv/file.csv"

# read the CSV file into a dataframe without header
df_no_header <- read.csv(file_path, header = FALSE)

# set column names
colnames(df_no_header) <- c("col1", "col2", "col3", "col4")
263 chars
9 lines

Once you have read the CSV file into a dataframe, you can perform various data manipulation tasks such as filtering, sorting and aggregating the data.

gistlibby LogSnag