How To Create DataFrame From csv file?
A Comma-Separated Values (CSV) file is a plain text file that contains a list of data. It is sometimes referred to as character-separated values or comma-delimited files. A csv file typically use commas to separate data but can also use other characters like semicolons. Csv file is commonly used for data exchange between different applications.
Syntax: read.csv()
Example:
Supposed we have a file called employees1.csv. It has commas as data separator as a structure file like this.
id,name,salary,start_date,division
1,Saleem,700.3,2022-02-01,IT
2,Jim,500.2,2023-09-20,Operations
3,Ahmed,600,2024-08-16,IT
4,Maya,740,2024-05-13,HR
5,Maria,870.25,2022-11-28,Finance
6,Ben,520,2023-05-22,IT
7,Omar,952.8,2023-10-30,Operations
8,Jhon,733.5,2024-06-10,Finance
9,Mike,750.5,2024-09-12,HR
10,Ameer,760.5,2024-07-17,Finance
We also have another file called employees2.csv with semicolons as data separator like this
id;name;salary;start_date;division
1;Saleem;700.3;2022-02-01;IT
2;Jim;500.2;2023-09-20;Operations
3;Ahmed;600;2024-08-16;IT
4;Maya;740;2024-05-13;HR
5;Maria;870.25;2022-11-28;Finance
6;Ben;520;2023-05-22;IT
7;Omar;952.8;2023-10-30;Operations
8;Jhon;733.5;2024-06-10;Finance
9;Mike;750.5;2024-09-12;HR
10;Ameer;760.5;2024-07-17;Finance
Now we can read the file and create a dataframe using the syntax like this
#using commas as data separator
df1 <- read.csv("employees1.csv",sep = ",")
print(df1)
#output:
id name salary start_date division
1 1 Saleem 700.30 2022-02-01 IT
2 2 Jim 500.20 2023-09-20 Operations
3 3 Ahmed 600.00 2024-08-16 IT
4 4 Maya 740.00 2024-05-13 HR
5 5 Maria 870.25 2022-11-28 Finance
6 6 Ben 520.00 2023-05-22 IT
7 7 Omar 952.80 2023-10-30 Operations
8 8 Jhon 733.50 2024-06-10 Finance
9 9 Mike 750.50 2024-09-12 HR
10 10 Ameer 760.50 2024-07-17 Finance
#using semicolons as data separator
df2 <- read.csv("employees2.csv",sep = ";")
print(df2)
#output:
id name salary start_date division
1 1 Saleem 700.30 2022-02-01 IT
2 2 Jim 500.20 2023-09-20 Operations
3 3 Ahmed 600.00 2024-08-16 IT
4 4 Maya 740.00 2024-05-13 HR
5 5 Maria 870.25 2022-11-28 Finance
6 6 Ben 520.00 2023-05-22 IT
7 7 Omar 952.80 2023-10-30 Operations
8 8 Jhon 733.50 2024-06-10 Finance
9 9 Mike 750.50 2024-09-12 HR
10 10 Ameer 760.50 2024-07-17 Finance
Now we want to make sure what we have done is right by checking the dataframe
print(is.data.frame(df1))
print(ncol(df1))
print(nrow(df1))
#output:
[1] TRUE
[1] 5
[1] 10
Conclusion:
Creating dataframe from csv file in R programming is done by calling read.csv() function and add the csv file as an argument.
