We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !
R language provides several useful functions for importing delimited files and creating data frames. These delimited files are often stored in local computer directory, with extensions ‘txt’, ‘csv’, ‘dat’. The mostly widely used functions for importing these files in R are read.table() and read.csv().
- read.table()
By specifying different delimiter in the function, read.table() can accommodate importing data files with type ‘txt, ‘csv’, ‘dat’ and more. The basic form of the function is :
df <- read.table(f, options)
Where:
f is a delimited file,
options includes the following characteristics:
header: whether the the first line in the file is used for variable names
sep: The delimiter in the file to separate data values.
sep=”” – spaces, tabs, new line used for delimiting
sep=”,” – comma used for delimiting
sep=”\t” – tab used for delimiting
row.names: specifying one or more variables to represent row identifiers
col.names: specify a character vector containing the variable names
na.strings – value codes which will be converted to “NA” in data frame
stringsAsFactors – whether character variables should be converted to factors, default is FALSE.
In the next lines, we show a csv file containing student exam scores, and it is stored in the computer’s local directory.
StudentID,Fullname,Race,Gender,Country,Age,Math,Physics,Chemistry,Date
1,James Zhang,A,Male,US,23,73,70,87,10/31/2008
2,Wilson Li,E,Female,UK,26,95,76,83,3/16/2008
3,Richard Nuan Ye,A,Male,UK,35,77,83,92,5/22/2008
4,Mary Deng,E,Female,US,21,60,99,84,1/24/2009
5,Jason Wilson,A,Male,UK,19,77,89,93,7/30/2009
6,Jennifer Hopkin,A,Female,UK,43,79,64,83,4/5/2009
7,Kari Gjendem,E,Female,US,37,87,99,67,11/24/2008
8,Wenche Dale,E,Female,US,28,95,87,93,10/2/2008
9,Jane Larsen,A,Female,US,19,73,92,84,6/5/2009
10,Steinar Hansen,A,Male,US,25,66,93,65,8/1/2008
11,Michael Chen,A,Male,UK,42,83,90,77,10/24/2008
12,Josef Curton,E,Male,US,32,71,63,96,11/8/2009
13,Jennifer Jones,E,Male,US,27,79,76,82,10/29/2008
14,Gary Grant,E,Female,UK,35,90,78,92,10/24/2008
15,Phil Yao,A,Male,UK,21,69,69,83,10/15/2008
16,Nora Spears,E,Female,US,29,79,83,76,3/11/2009
17,Goril Nordmann,A,Female,UK,36,91,79,69,5/24/2008
18,Lisa Bondvik,E,Female,US,39,65,73,87,7/9/2009
19,Guri Olsen,E,Female,US,24,87,72,89,8/12/2009
20,Martin Jones,A,Male,US,25,82,73,62,3/27/2008
The following code shows how to import this csv file and generate a data frame, by setting the delimiter as ‘,’, and the first row of the csv file as data frame’s column variable labels.
# to set working directory
> setwd("d:\\RStatistics-Tutorial")
>
#use read.table() to read a csv file to create a data frame
> grade <- read.table("University-Fullname-full.csv", header=TRUE, row.names="StudentID", sep=",")
> grade
Fullname Race Gender Country Age Math Physics Chemistry
1 James Zhang A Male US 23 73 70 87
2 Wilson Li E Female UK 26 95 76 83
3 Richard Nuan Ye A Male UK 35 77 83 92
4 Mary Deng E Female US 21 60 99 84
5 Jason Wilson A Male UK 19 77 89 93
6 Jennifer Hopkin A Female UK 43 79 64 83
7 Kari Gjendem E Female US 37 87 99 67
8 Wenche Dale E Female US 28 95 87 93
9 Jane Larsen A Female US 19 73 92 84
10 Steinar Hansen A Male US 25 66 93 65
11 Michael Chen A Male UK 42 83 90 77
12 Josef Curton E Male US 32 71 63 96
13 Jennifer Jones E Male US 27 79 76 82
14 Gary Grant E Female UK 35 90 78 92
15 Phil Yao A Male UK 21 69 69 83
16 Nora Spears E Female US 29 79 83 76
17 Goril Nordmann A Female UK 36 91 79 69
18 Lisa Bondvik E Female US 39 65 73 87
19 Guri Olsen E Female US 24 87 72 89
20 Martin Jones A Male US 25 82 73 62
Date
1 10/31/2008
2 3/16/2008
3 5/22/2008
4 1/24/2009
5 7/30/2009
6 4/5/2009
7 11/24/2008
8 10/2/2008
9 6/5/2009
10 8/1/2008
11 10/24/2008
12 11/8/2009
13 10/29/2008
14 10/24/2008
15 10/15/2008
16 3/11/2009
17 5/24/2008
18 7/9/2009
19 8/12/2009
20 3/27/2008
#show the structure of the data frame
str(grade)
'data.frame': 20 obs. of 9 variables:
$ Fullname : chr "James Zhang" "Wilson Li" "Richard Nuan Ye" "Mary Deng" ...
$ Race : chr "A" "E" "A" "E" ...
$ Gender : chr "Male" "Female" "Male" "Female" ...
$ Country : chr "US" "UK" "UK" "US" ...
$ Age : int 23 26 35 21 19 43 37 28 19 25 ...
$ Math : int 73 95 77 60 77 79 87 95 73 66 ...
$ Physics : int 70 76 83 99 89 64 99 87 92 93 ...
$ Chemistry: int 87 83 92 84 93 83 67 93 84 65 ...
$ Date : chr "10/31/2008" "3/16/2008" "5/22/2008" "1/24/2009" ...
2. read.csv()
You can also use read.csv() to import a csv file and generate a data frame.
The basic form of the function is
read.csv(f)
Where f is a csv file stored on computer, and the effect is same as using read.table() with header =TRUE, and sep=”,”
The next code example shows the reading same file using read.csv()
# to set working directory
> setwd("d:\\RStatistics-Tutorial")
>
#using read.csv() to import a csv file and generate a data #frame
> grade <- read.csv("University-Fullname-full.csv")
> grade
StudentID Fullname Race Gender Country Age Math Physics
1 1 James Zhang A Male US 23 73 70
2 2 Wilson Li E Female UK 26 95 76
3 3 Richard Nuan Ye A Male UK 35 77 83
4 4 Mary Deng E Female US 21 60 99
5 5 Jason Wilson A Male UK 19 77 89
6 6 Jennifer Hopkin A Female UK 43 79 64
7 7 Kari Gjendem E Female US 37 87 99
8 8 Wenche Dale E Female US 28 95 87
9 9 Jane Larsen A Female US 19 73 92
10 10 Steinar Hansen A Male US 25 66 93
11 11 Michael Chen A Male UK 42 83 90
12 12 Josef Curton E Male US 32 71 63
13 13 Jennifer Jones E Male US 27 79 76
14 14 Gary Grant E Female UK 35 90 78
15 15 Phil Yao A Male UK 21 69 69
16 16 Nora Spears E Female US 29 79 83
17 17 Goril Nordmann A Female UK 36 91 79
18 18 Lisa Bondvik E Female US 39 65 73
19 19 Guri Olsen E Female US 24 87 72
20 20 Martin Jones A Male US 25 82 73
Chemistry Date
1 87 10/31/2008
2 83 3/16/2008
3 92 5/22/2008
4 84 1/24/2009
5 93 7/30/2009
6 83 4/5/2009
7 67 11/24/2008
8 93 10/2/2008
9 84 6/5/2009
10 65 8/1/2008
11 77 10/24/2008
12 96 11/8/2009
13 82 10/29/2008
14 92 10/24/2008
15 83 10/15/2008
16 76 3/11/2009
17 69 5/24/2008
18 87 7/9/2009
19 89 8/12/2009
20 62 3/27/2008
>
You can also watch full video on R tutorial from our YouTube channel.
0 Comments