Reading a table in R is quite straight-forward. We are going to read a table from the web and also one locally.
If you have come across a .dat file and want to import that table into R you’ll find this to be incredibly easy compared to other imports.
We will use a Princeton dataset, get the location of the file and use the function read.table.
> tableTest <- read.table("http://data.princeton.edu/wws509/datasets/effort.dat")
> tableTest
setting effort change
Bolivia 46 0 1
Brazil 74 0 10
Chile 89 16 29
Colombia 77 16 25
CostaRica 84 21 29
Cuba 89 15 40
DominicanRep 68 14 21
Ecuador 70 6 0
ElSalvador 60 13 13
Guatemala 55 9 4
Haiti 35 3 0
Honduras 51 7 7
Jamaica 87 23 21
Mexico 83 4 9
Nicaragua 68 0 7
Panama 84 19 22
Paraguay 74 3 6
Peru 73 0 2
TrinidadTobago 84 15 29
Venezuela 91 7 11
The above took the table and imported and now we can utilize the data in many different ways. We will get into evaluating and utilizing data more in the future, but next we will show you how to take your own table and import that data into R locally.
We created a generic table file like this:
T B D
1 2 3
4 5 6
We saved this file from our text editor as a tbd.dat. Then saved it to our working R directory. With a similar command as before we were able to import with the following:
> testTable2 = read.table("tbd.dat")
> testTable2
V1 V2 V3
1 T B D
2 1 2 3
3 4 5 6
Now lets say we want to name our headers we would have imported the table this way:
> testTable3 = read.table("tbd.dat", col.names = c("A", "B", "C"))
> testTable3
A B C
1 T B D
2 1 2 3
3 4 5 6
Above we set the col.names with the associated A, B, C titles and were able to label all of our columns and that is it.