R Programming: read.csv Data Set

Fig. 1: csv_table.csv

A data set in text-based .csv format can be stored into a variable using the read.csv() function. Below the data set is stored in a new variable named people. The attribute head=TRUE indicates that the first line of the data set contains column headings. Then, as you might have guessed, the sep attribute indicates that the data set contains comma-separated values (see Fig. 1).

An R interpreter in a Unix-like operating system can be used to analyze the data. The attributes() function can be used to list data set attributes, including column names, data class and row names. The summary() function lists statistical details. The names() function lists column names. The people$age attribute lists numeric elements in the age column.

> people <- read.csv(file="csv_table.csv", head=TRUE, sep=",")
> print(people)
name sex age
1 John male 29
2 Sheryl female 35
3 Ralph male 42
4 Susan female 21
5 Joseph male 18
> attributes(people)
[1] "name" "sex" "age"
[1] "data.frame"
[1] 1 2 3 4 5
> summary(people)
name sex age
John :1 female:2 Min. :18
Joseph:1 male :3 1st Qu.:21
Ralph :1 Median :29
Sheryl:1 Mean :29
Susan :1 3rd Qu.:35
Max. :42
> names(people)
[1] "name" "sex" "age"
> print(people$age)
[1] 29 35 42 21 18

Your Turn

Use a text editor to create a comma-separated .csv file, like Fig.1. Then navigate to the directory where your .csv file is stored, using a Unix-like terminal emulator. Then start R interpreter, and use your version of the code above to access data.