Read a file in table format (.csv
, .txt
, .dat
) and creates a data frame from it.
read.table()
read.csv()
read.csv2()
Write a file in text format.
write.table()
write.csv()
write.csv2()
mytable <- read.table(file = "data/table1.txt", sep = "\t", header = TRUE)
mytable
STATE | Y1 | Y2 | X1 | X2 |
---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> |
AL | 2206.0 | 2186.0 | 92.7 | 91.4 |
AK | 0.7 | 0.7 | 151.0 | 149.0 |
AZ | 73.0 | 74.0 | 61.0 | 56.0 |
AR | 3620.0 | 3737.0 | 86.3 | 91.8 |
CA | 7472.0 | 7444.0 | 63.4 | 58.4 |
CO | 788.0 | 873.0 | 77.8 | 73.0 |
CT | 1029.0 | 948.0 | 106.0 | 104.0 |
DE | 168.0 | 164.0 | 117.0 | 113.0 |
FL | 2568.0 | 2537.0 | 62.0 | 57.2 |
GA | 4302.0 | 4301.0 | 80.6 | 80.8 |
HI | 227.5 | 224.5 | 85.0 | 85.5 |
ID | 187.0 | 203.0 | 79.1 | 72.9 |
IL | 793.0 | 809.0 | 65.0 | 70.5 |
IN | 5445.0 | 5290.0 | 62.7 | 60.1 |
IA | 2151.0 | 2247.0 | 56.5 | 53.0 |
KS | 404.0 | 389.0 | 54.5 | 47.8 |
KY | 412.0 | 483.0 | 67.7 | 73.5 |
LA | 273.0 | 254.0 | 115.0 | 115.0 |
ME | 1069.0 | 1070.0 | 101.0 | 97.0 |
MD | 885.0 | 898.0 | 76.6 | 75.4 |
MA | 235.0 | 237.0 | 105.0 | 102.0 |
MI | 1406.0 | 1396.0 | 58.0 | 53.8 |
MN | 2499.0 | 2697.0 | 57.7 | 54.0 |
MS | 1434.0 | 1468.0 | 87.8 | 86.7 |
MO | 1580.0 | 1622.0 | 55.4 | 51.5 |
MT | 172.0 | 164.0 | 68.0 | 66.0 |
NE | 1202.0 | 1400.0 | 50.3 | 48.9 |
NV | 2.2 | 1.8 | 53.9 | 52.7 |
NH | 43.0 | 49.0 | 109.0 | 104.0 |
NJ | 442.0 | 491.0 | 85.0 | 83.0 |
NM | 283.0 | 302.0 | 74.0 | 70.0 |
NY | 975.0 | 987.0 | 68.1 | 64.0 |
NC | 3033.0 | 3045.0 | 82.8 | 78.7 |
ND | 51.0 | 45.0 | 55.2 | 48.0 |
OH | 4667.0 | 4637.0 | 59.1 | 54.7 |
OK | 869.0 | 830.0 | 101.0 | 100.0 |
OR | 652.0 | 686.0 | 77.0 | 74.6 |
PA | 4976.0 | 5130.0 | 61.0 | 52.0 |
RI | 53.0 | 50.0 | 102.0 | 99.0 |
SC | 1422.0 | 1420.0 | 70.1 | 65.9 |
SD | 435.0 | 602.0 | 48.0 | 45.8 |
TN | 277.0 | 279.0 | 71.0 | 80.7 |
TX | 3317.0 | 3356.0 | 76.7 | 72.6 |
UT | 456.0 | 486.0 | 64.0 | 59.0 |
VT | 31.0 | 30.0 | 106.0 | 102.0 |
VA | 934.0 | 988.0 | 86.3 | 81.2 |
WA | 1287.0 | 1313.0 | 74.1 | 71.5 |
WV | 136.0 | 174.0 | 104.0 | 109.0 |
WI | 910.0 | 873.0 | 60.1 | 54.0 |
WY | 1.7 | 1.7 | 83.0 | 83.0 |
as.matrix(mytable[, 2:5]) # Convert to a Matrix format
Y1 | Y2 | X1 | X2 |
---|---|---|---|
2206.0 | 2186.0 | 92.7 | 91.4 |
0.7 | 0.7 | 151.0 | 149.0 |
73.0 | 74.0 | 61.0 | 56.0 |
3620.0 | 3737.0 | 86.3 | 91.8 |
7472.0 | 7444.0 | 63.4 | 58.4 |
788.0 | 873.0 | 77.8 | 73.0 |
1029.0 | 948.0 | 106.0 | 104.0 |
168.0 | 164.0 | 117.0 | 113.0 |
2568.0 | 2537.0 | 62.0 | 57.2 |
4302.0 | 4301.0 | 80.6 | 80.8 |
227.5 | 224.5 | 85.0 | 85.5 |
187.0 | 203.0 | 79.1 | 72.9 |
793.0 | 809.0 | 65.0 | 70.5 |
5445.0 | 5290.0 | 62.7 | 60.1 |
2151.0 | 2247.0 | 56.5 | 53.0 |
404.0 | 389.0 | 54.5 | 47.8 |
412.0 | 483.0 | 67.7 | 73.5 |
273.0 | 254.0 | 115.0 | 115.0 |
1069.0 | 1070.0 | 101.0 | 97.0 |
885.0 | 898.0 | 76.6 | 75.4 |
235.0 | 237.0 | 105.0 | 102.0 |
1406.0 | 1396.0 | 58.0 | 53.8 |
2499.0 | 2697.0 | 57.7 | 54.0 |
1434.0 | 1468.0 | 87.8 | 86.7 |
1580.0 | 1622.0 | 55.4 | 51.5 |
172.0 | 164.0 | 68.0 | 66.0 |
1202.0 | 1400.0 | 50.3 | 48.9 |
2.2 | 1.8 | 53.9 | 52.7 |
43.0 | 49.0 | 109.0 | 104.0 |
442.0 | 491.0 | 85.0 | 83.0 |
283.0 | 302.0 | 74.0 | 70.0 |
975.0 | 987.0 | 68.1 | 64.0 |
3033.0 | 3045.0 | 82.8 | 78.7 |
51.0 | 45.0 | 55.2 | 48.0 |
4667.0 | 4637.0 | 59.1 | 54.7 |
869.0 | 830.0 | 101.0 | 100.0 |
652.0 | 686.0 | 77.0 | 74.6 |
4976.0 | 5130.0 | 61.0 | 52.0 |
53.0 | 50.0 | 102.0 | 99.0 |
1422.0 | 1420.0 | 70.1 | 65.9 |
435.0 | 602.0 | 48.0 | 45.8 |
277.0 | 279.0 | 71.0 | 80.7 |
3317.0 | 3356.0 | 76.7 | 72.6 |
456.0 | 486.0 | 64.0 | 59.0 |
31.0 | 30.0 | 106.0 | 102.0 |
934.0 | 988.0 | 86.3 | 81.2 |
1287.0 | 1313.0 | 74.1 | 71.5 |
136.0 | 174.0 | 104.0 | 109.0 |
910.0 | 873.0 | 60.1 | 54.0 |
1.7 | 1.7 | 83.0 | 83.0 |
write.table(mytable, file = "mysavedtable.txt", sep = ",") # save with comma separator
openxlsx
package.Read Excel files with read.xlsx()
function
read.xlsx(xlsxFile, sheet, startRow = 1, colNames = TRUE, rowNames = FALSE, ...)
Write Excel files
write.xlsx(x, file, asTable = FALSE, overwrite = TRUE, ...)
# install.packages("openxlsx")
Installing package into ‘/home/fli/.R/library’ (as ‘lib’ is unspecified)
require("openxlsx")
mydata <- read.xlsx("data/UNHBDScore.xlsx",
sheet = 1,
startRow = 1,
colNames = TRUE)
mydata
Team | Slides/Report | Codes | Video | Use.public.data | Use.big.data | Theme | Innovation | Methods | Presentation | Visualization | |
---|---|---|---|---|---|---|---|---|---|---|---|
<chr> | <chr> | <chr> | <chr> | <chr> | <chr> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | |
1 | Scipopulis | Yes | Yes | Yes | Yes | Yes | 4 | 3 | 3 | 4 | 4 |
2 | Analysts Against Poverty | Yes | Yes | Yes | Yes | No | 3 | 3 | 2 | 3 | 2 |
3 | Upside Down | Yes | Yes | No | Yes | Yes | 2 | 2 | 2 | 1 | 2 |
4 | safe-encounter.ai | Yes | Yes | Yes | Yes | Yes | 3 | 2 | 1 | 3 | 1 |
5 | Big Data and Disasters - ADB | Yes | Yes | Yes | Yes | Yes | 5 | 4 | 5 | 4 | 4 |
6 | Datains - Nippon Koei | Yes | Yes | Yes | Yes | Yes | 5 | 3 | 3 | 4 | 4 |
7 | Frame | Yes | Yes | Yes | Yes | Yes | 5 | 3 | 2 | 3 | 2 |
8 | GC-TICS | Yes | Yes | Yes | Yes | Yes | 3 | 3 | 3 | 4 | 2 |
9 | Meaning Maker | Yes | Yes | Yes | Yes | Yes | 4 | 3 | 3 | 4 | 4 |
10 | Satria Garuda | Yes | Yes | Yes | Yes | Yes | 3 | 2 | 2 | 2 | 1 |
11 | Vegetation Growth | Yes | Yes | Yes | Yes | Yes | 3 | 3 | 3 | 3 | 2 |
12 | YOLO (You Only Live Once) | Yes | Yes | Yes | Yes | Yes | 4 | 4 | 3 | 3 | 3 |
13 | ZU_ProMinds | Yes | Yes | Yes | Yes | Yes | 3 | 3 | 3 | 3 | 3 |
14 | GEMy | Yes | Yes | Yes | Yes | Yes | 4 | 3 | 3 | 3 | 2 |
15 | Not wee Data | Yes | Yes | Yes | Yes | Yes | 3 | 3 | 3 | 3 | 2 |
16 | MKIT_AIMS2018 | No | Yes | No | Yes | Yes | 2 | 2 | 2 | 1 | 1 |
17 | Explorers | Yes | Yes | Yes | Yes | Yes | 4 | 4 | 5 | 5 | 5 |
18 | Multiverse of Data | Yes | Yes | Yes | Yes | Yes | 4 | 3 | 4 | 3 | 3 |
19 | QUT Centre for Data Science | No | Yes | No | Yes | Yes | 3 | 2 | 2 | 1 | 1 |
20 | UAE Diversified Big Data Expert Team | Yes | Yes | Yes | Yes | Yes | 4 | 4 | 3 | 2 | 3 |
R.matlab
package.readMat()
and writeMat()
for reading and writing MAT files. # install.packages(R.matlab)
library("R.matlab")
readMat("data/RajanData.mat")
R.matlab v3.7.0 (2022-08-25 21:52:34 UTC) successfully loaded. See ?R.matlab for help. Attaching package: ‘R.matlab’ The following objects are masked from ‘package:base’: getOption, isOpen
1 | 0.15283128 | 1.0452750 | 6.046561 | 0.08774734 |
1 | 0.59341052 | 1.6479298 | 3.939774 | 0.18836954 |
1 | 0.24067336 | 2.8577454 | 5.757311 | 0.24105133 |
1 | 0.37908270 | 0.6859607 | 4.942442 | 0.10643459 |
1 | 0.22463373 | 0.8153565 | 3.644353 | 0.10099751 |
1 | 0.28268554 | 2.0006900 | 5.687355 | 0.10290431 |
1 | 0.16413260 | 0.8567230 | 7.444632 | 0.07588428 |
1 | 0.64726939 | 1.0547738 | 6.007547 | 0.10247049 |
1 | 0.10341836 | 0.8760556 | 6.427695 | -0.04920358 |
1 | 0.12944697 | 2.0706480 | 6.069407 | 0.14460620 |
1 | 0.10986147 | 1.4878637 | 6.850210 | 0.18828204 |
1 | 0.22716095 | 0.8828160 | 4.145006 | 0.10592493 |
1 | 0.10519763 | 3.6674837 | 2.216809 | 0.41223156 |
1 | 0.88186163 | 1.0194852 | 4.973619 | 0.31369309 |
1 | 0.44649656 | 4.1762986 | 8.968512 | 0.31245069 |
1 | 0.68209559 | 0.9098037 | 4.418684 | 0.08361230 |
1 | 0.09845585 | 1.2532836 | 4.988076 | 0.12320417 |
1 | 0.30317060 | 0.8965770 | 5.179500 | 0.10322459 |
1 | 0.27682052 | 1.2650914 | 4.286548 | 0.11287619 |
1 | 0.19914411 | 1.0042223 | 3.970990 | 0.05652562 |
1 | 0.21840969 | 0.9008426 | 4.563952 | 0.08914537 |
1 | 0.36891574 | 0.7764582 | 1.599792 | 0.04900973 |
1 | 0.08318394 | 16.7516680 | 2.215719 | 0.03011832 |
1 | 0.07090499 | 3.2940323 | 4.797549 | 0.19508910 |
1 | 0.30679425 | 0.9037179 | 4.584620 | 0.05960136 |
1 | 0.13470736 | 1.2151619 | 6.310098 | 0.05064859 |
1 | 0.04878252 | 1.0363831 | 6.610284 | 0.10701220 |
1 | 0.43966075 | 2.0306009 | 4.536859 | 0.28045713 |
1 | 0.10106310 | 0.7912812 | 3.201648 | 0.12439597 |
1 | 0.16227839 | 0.8357908 | 3.221033 | 0.06801826 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
1 | 0.23163057 | 1.4541071 | 1.5096176 | -0.45443400 |
1 | 0.01412514 | 1.9036772 | 4.4757333 | 0.10464538 |
1 | 0.10329101 | 3.1218296 | 3.6441697 | 0.20205401 |
1 | 0.06915715 | 2.6923948 | 3.2967624 | 0.44818360 |
1 | 0.24796527 | 4.5749457 | 0.4187103 | -0.25908844 |
1 | 0.06344315 | 4.3150962 | 4.2255336 | 0.17914748 |
1 | 0.06135137 | 3.0522308 | 3.3308107 | 0.24496727 |
1 | 0.04014119 | 4.1432166 | 2.8499548 | 0.14002478 |
1 | 0.01068510 | 4.4860544 | 1.1749559 | 0.10708674 |
1 | 0.07208257 | 6.1268956 | 3.6317269 | 0.11618639 |
1 | 0.02340338 | 4.1160044 | 2.0272268 | 0.13732224 |
1 | 0.36795217 | 3.1915709 | 3.2789544 | 0.10186379 |
1 | 0.33090041 | 0.8523913 | 6.4572217 | 0.05928239 |
1 | 0.30685709 | 1.4329358 | 4.5266571 | 0.23338493 |
1 | 0.31943752 | 1.6375911 | 7.9338800 | 0.16894862 |
1 | 0.66418733 | 1.2743732 | 9.3798299 | 0.12295684 |
1 | 0.24086422 | 0.9936688 | 9.4355879 | 0.04494273 |
1 | 0.69266564 | 0.9936683 | 6.1993483 | 0.17269039 |
1 | 0.89899956 | 0.1482478 | 5.6734504 | 0.04860495 |
1 | 0.93314590 | 0.3128039 | 6.0399737 | 0.07985335 |
1 | 0.70725370 | 0.9642831 | 7.9493340 | 0.18984009 |
1 | 0.60640193 | 1.4674369 | 6.7021385 | 0.09358254 |
1 | 0.71417070 | 1.3699697 | 5.3074902 | 0.20615239 |
1 | 0.12461827 | 1.1742563 | 10.2837032 | 0.07930871 |
1 | 0.35718510 | 1.8335471 | 6.7345917 | 0.17267889 |
1 | 0.54307637 | 1.1319547 | 5.1073566 | 0.08069173 |
1 | 0.49945522 | 0.7121362 | 6.1899834 | 0.04285418 |
1 | 0.95506871 | 0.7826259 | 4.3393540 | 0.05073775 |
1 | 0.27126570 | 1.4070131 | 4.5467993 | 0.02018504 |
1 | 0.55291384 | 1.2432457 | 7.7006338 | 0.09380608 |
0.48061406 |
0.49789298 |
0.08513211 |
0.72337394 |
0.87229293 |
0.25224938 |
0.65029221 |
0.82784745 |
0.96586876 |
0.29379814 |
0.25159485 |
0.69433447 |
0.08579703 |
0.52781013 |
0.12396548 |
0.82881142 |
0.25703244 |
0.52647353 |
0.47460682 |
0.56260611 |
0.69551821 |
0.27355197 |
0.05781200 |
0.13346465 |
0.57969422 |
0.69786571 |
0.23100059 |
0.13815073 |
0.32689204 |
0.79901292 |
⋮ |
0.14383677 |
0.30137774 |
0.32939544 |
0.68419846 |
0.07815812 |
0.06176844 |
0.07326558 |
0.04638021 |
0.03154198 |
0.04460804 |
0.01265112 |
0.18559067 |
0.79289621 |
0.36263210 |
0.33696546 |
0.60398084 |
0.65082213 |
0.56969612 |
0.43006658 |
0.18838975 |
0.57448000 |
0.44272956 |
0.53810913 |
0.67301374 |
0.30994720 |
0.48864363 |
0.65195421 |
0.93939365 |
0.19348394 |
0.45197044 |
debtratio |
Rajan-Zingales JF 1995 data, but with definitions from Cook, Kieschnick and McCullough, Journal of Empirical Finance 2008 |
const | tang | mbtr | logsale | profit |
The recommended R package foreign
provides import facilities for files
produced by these statistical systems.
Function read.xport()
reads a file in SAS Transport (XPORT) format
and return a list of data frames.
Function read.mtp()
imports a 'Minitab Portable Worksheet'. This
returns the components of the worksheet as an R list
Function read.spss()
can read files created by the 'save' and
'export' commands in SPSS
Files from Stata can be read and written by functions read.dta()
and write.dta()
.
JPEG Format
install.packages("jpeg")
require("jpeg")
readJPEG()
rasterImage()
Packages bmp, jpeg and png read the formats after which they are named. See also packages biOps and Momocs, and Bioconductor package EBImage.
# install.packages("jpeg")
library("jpeg")
dim(myprofile)
image(1:1539, 1:1154, myprofile[, , 1])
image(1:1539, 1:1154, myprofile[, , 2])
image(1:1539, 1:1154, myprofile[, , 3])
plot(c(100, 250), c(300, 450), type = "n", xlab = "", ylab = "")
rasterImage(myprofile, 100, 300, 150, 350, interpolate = FALSE)
rasterImage(myprofile, 100, 400, 150, 450)
rasterImage(myprofile, 200, 300, 200 + xinch(.5),
300 + yinch(.3), interpolate = FALSE)
rasterImage(myprofile, 200, 400, 250, 450,
angle = 15, interpolate = FALSE)
SQL has limited numerical and statistical features. For example, it has no least squares fitting procedures, and to find quantiles requires a sophisticated query.
Not only are basic statistical functions missing from SQL, but in many cases the numerical algorithms used in the basic aggregate functions are not implemented to safeguard numerical accuracy.
For these reasons, it may be desirable or even necessary to perform a statistical analysis in a statistical package rather than in the database. One way to do this, is to extract the data from the database and import it into statistical software.
The RODBC
package provides access to databases (including
Microsoft Access and Microsoft SQL Server) through an ODBC
interface.
odbcConnect(dsn, uid="", pwd="")
sqlFetch(channel, sqtable)
sqlQuery(channel, query)
sqlSave(channel, mydf, tablename = sqtable, append = FALSE)
sqlDrop(channel, sqtable)
Use the RODBC
package to read Oracle Database.
The DBI
package in R provides a uniform, client- side interface
to different database management systems, such as MySQL, PostgreSQL,
and Oracle.
The RMySQL
package provides an interface to MySQL.
The ROracle
package provides an interface for Oracle.
The RJDBC
package provides access to databases through a JDBC
interface.
R Data Import/Export