data.frame function is similar like structure in C language, which can contain different types of data as a union, helping us to manage data more easily.
1. Reading a column from a data frame.
> treasure <- data.frame(weights, prices, types)
> treasure[[2]]
> treasure[["weights"]]
> treasure$prices
2. Loading a dataset from a package
> data(meuse, package = "sp")
3. Loading a data frame from files
First of all, we can use list.files() to show all the docs in current working folder.
Secondly, read.csv() could load a file into workspace.
> list.files()
[1] "__MACOSX" "Groundwater_Temperature.csv"
[3] "Groundwater_Temperature.xls" "gstat.pdf"
[5] "gstat_1.1-1" "gstat_1.1-1.zip"
> read.csv("Groundwater_Temperature.csv")
Name X.Koordinate Y.Koordinate Oberfl盲che Datum Temperatur
1 ABP9922 4459725 5340978 497.50 7/25/09 9.9
2 ABP9926 4460005 5341246 498.90 7/25/09 12.0
3 BP 25 4464282 5331784 534.17 7/26/09 9.9
4. Formatting a txt file
Original of "infantry.txt":
Port Infantry
Porto Bello 700
Cartagena 500
Panama City 1500
Havana 2000
> read.table("infantry.txt", sep = "\t", header = TRUE)
Port Infantry
1 Porto Bello 700
2 Cartagena 500
3 Panama City 1500
4 Havana 2000
5. Printing
sprintf("the coordinate of 30th record is %g %f", meuse[30,1], meuse[30,2] )
%f -- float
%e -- Scientific notation
%g -- integer
print(paste0("Current working dir: ", wd))
paste()/paste0() -- convert vector into character