[Entering patterned data | Entering data from a file | Data frames ] |
> a_c(1:10) > a [1] 1 2 3 4 5 6 7 8 9 10 > seq(-5,5,by=.2) [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 [16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 [31] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 [46] 4.0 4.2 4.4 4.6 4.8 5.0 > seq(length=10,from=1,by=.10) [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 To get repetitions: > rep(2,times=3) [1] 2 2 2 > rep(2,3) [1] 2 2 2 > a_c(rep(2,3),4,5,rep(1,5)) a> [1] 2 2 2 4 5 1 1 1 1 1 > a_c( rep(c(12, 15,16),c(3,2,1)) ) > a [1] 12 12 12 15 15 16 You can also indicate the length, beginning and separation of the sequence: > seq(length=100,from=2,by=0.2) [1] 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 [16] 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 [31] 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0 10.2 10.4 10.6 10.8 [46] 11.0 11.2 11.4 11.6 11.8 12.0 12.2 12.4 12.6 12.8 13.0 13.2 13.4 13.6 13.8 [61] 14.0 14.2 14.4 14.6 14.8 15.0 15.2 15.4 15.6 15.8 16.0 16.2 16.4 16.6 16.8 [76] 17.0 17.2 17.4 17.6 17.8 18.0 18.2 18.4 18.6 18.8 19.0 19.2 19.4 19.6 19.8 [91] 20.0 20.2 20.4 20.6 20.8 21.0 21.2 21.4 21.6 21.8 We can revert the order of vector by doing >a_rev(a) This allows to use the entered patterned data in the reversed way > a2_rev(c(1:10)) > a2 [1] 10 9 8 7 6 5 4 3 2 1
> b_c(scan("/home/arcones/splus/datasets/car.gals"))
To read data from a file, you can use the command read.table. For example; > colvars [1] "id" "fname" "lname" "age" "junk" > test9 <- read.table("test.dat",col.names=colvars) > test9 id fname lname age junk 1 12 12 12 12 12 2 134 134 12 12 12 3 14 14 14 14 14 col.names gives the names for the variables
To read data from a file, you can use the commands write.table and cat. For example; > write.table(test9, file="mat1.dat",sep=" ") creates the unix file "mat1.dat" with the data in test9. > age [1] 22 31 37 52 27 60 34 53 > cat (age , file="mat3.dat",sep=" ") creates the unix file "mat3.dat" with the data in age. A simple way to output Splus objects to an ASCII file is to use the function "sink()". For example, the following commands >sink("outfile") >mydata >grades >sink() forward output that usually appear on screen to the file "outfile". The last "sink()" stop the output to file "outfile", and resumes the output to screen.
Splus has a library of datasets available with which to experiment. One of these, cu.summary, contains data from Consumer Reports' yearly automobile issue. The following is a subset of this dataset:
> cu.summary[1:10,] Price Country Reliability Mileage Type Acura Integra 4 11950 Japan Much better NA Small Dodge Colt 4 6851 Japan NA NA Small Dodge Omni 4 6995 USA Much worse NA Small Eagle Summit 4 8895 USA better 33 Small Ford Escort 4 7402 USA worse 33 Small Ford Festiva 4 6319 Korea better 37 Small GEO Metro 3 6695 Japan NA NA Small GEO Prizm 4 10125 Japan/USA Much better NA Small Honda Civic 4 6635 Japan/USA Much better 32 Small Hyundai Excel 4 5899 Korea worse NA SmallData frames can be created by using the read.table(), data.frame(), expand.grid(), or the as.data.frame() functions.
Suppose the table above is recorded in a text file called cars. Character strings by default cannot contain white spaces, the data would therefore have to be edited before being read into Splus using the read.table() function. There are three ways in which this could be done:
1) quote the strings which contain white spaces: "Acura Integra 4" 11950 Japan "Much better" NA Small > cars_read.table("cars") * read.table() reads in a text file and saves the data in a data frame 2) use an explicit default field separator character: Acura Integra 4:11950:Japan:Much better:NA:Small > cars_read.table("cars",sep=":") 3) organize the data into fixed format fields: Col. 1 17 27 33 50 53 Acura Integra 4 11950 Japan Much better NA SmallIf each field starts in the same column on each line, then the data do not need to be edited and could be read in with the following commands:
> columns_c(1,17,27,33,50,53) > cars_read.table("cars", sep=columns, header = T)When explicit columns are used as separators, Splus cannot automatically tell if the first line contains headers, so the argument header=T is specified.
Splus assumes that the first row in the text file contains the variable names, and that the first column with non-numeric non-duplicated names contains the row names. If the first row and/or the first column are not valid variable names (ie.: non-numeric with no duplicates), Splus uses the row numbers for the rows and/or "V1", "V2", etc. for the columns. Row names and column names may also be specified as arguments to the read.table() function. Suppose the text file cars2 does not contain any row or column names:
> car.names_c("Acura Integra 4", "Dodge Colt 4", ... > car.vars_c("Price", "Country", "Reliabilty", "Mileage", "Type") > cars_read.table("cars2",row.names=car.names,col.names=car.vars)If the text file does contain column labels, specifying header=T will let Splus know that the first line is a header line and should be ignored. It is also possible to use row.names=field # (to specify from which field row names are to come from) or row.names= var. name (to specify the name of the variable to be used as row names).
Variables in data frames can be anything that is indexed by the set of rows. The following can be used for statistical models:
> sapply(cars,data.class) Price Country Reliabilty Mileage Type "numeric" "factor" "factor" "numeric" "factor" * the function sapply() is similar to the function apply() but is used specifically with lists or data framesA numeric vector can be coerced to mode factor using the function as.factor(). Similarly, a matrix or a list can be coerced into a data frame using the function as.data.frame(). The function data.frame() is used to combine Splus objects into a data frame. Character or logical vectors are converted into factors, matrices contribute one variable per column in the matrix, lists contribute one variable for each component of the list, applied recursively, and the variables in data frames become variables in the new data frame. Numeric vectors, factors, and ordered factors each contribute a single variable. If any argument to data.frame() is of the form I(x), then x will retain its class in the new data frame (ie.: a character vector will not be converted into a factor).
Suppose we have a model which predicts cholesterol values by systolic blood pressure and age. We might want to use the model to predict cholesterol values over a regular grid of blood pressures and ages.
> systol_seq(110,130,by=10) > age_seq(20,50,by=10)There are 3 values of systol, 4 values of age, and 12 pairs of values. Since predict() and similar computations require the new data to be a data frame, the function expand.grid() is used to create a data frame with every combination of the two variables:
> chol.grid_expand.grid(systol=systol,age=age) > chol.grid systol age 1 110 20 2 120 20 3 130 20 4 110 30 5 120 30 6 130 30 7 110 40 8 120 40 9 130 40 10 110 50 11 120 50 12 130 50Data frames are a class of objects to represent data which are usually used in fitting models. They are similar to matrices in that the variables can be treated as columns and the observations as rows. They are more general than matrices in the sense that matrices in Splus assume all the elements to be of the same mode (all numeric, logical, character strings, etc. ).
Splus has a library of datasets available with which to experiment. One of these, cu.summary, contains data from Consumer Reports' yearly automobile issue. The following is a subset of this dataset:
> cu.summary[1:10,] Price Country Reliability Mileage Type Acura Integra 4 11950 Japan Much better NA Small Dodge Colt 4 6851 Japan NA NA Small Dodge Omni 4 6995 USA Much worse NA Small Eagle Summit 4 8895 USA better 33 Small Ford Escort 4 7402 USA worse 33 Small Ford Festiva 4 6319 Korea better 37 Small GEO Metro 3 6695 Japan NA NA Small GEO Prizm 4 10125 Japan/USA Much better NA Small Honda Civic 4 6635 Japan/USA Much better 32 Small Hyundai Excel 4 5899 Korea worse NA SmallData frames can be created by using the read.table(), data.frame(), expand.grid(), or the as.data.frame() functions.
Suppose the table above is recorded in a text file called cars. Character strings by default cannot contain white spaces, the data would therefore have to be edited before being read into Splus using the read.table() function. There are three ways in which this could be done:
1) quote the strings which contain white spaces: "Acura Integra 4" 11950 Japan "Much better" NA Small > cars_read.table("cars") * read.table() reads in a text file and saves the data in a data frame 2) use an explicit default field separator character: Acura Integra 4:11950:Japan:Much better:NA:Small > cars_read.table("cars",sep=":") 3) organize the data into fixed format fields: Col. 1 17 27 33 50 53 Acura Integra 4 11950 Japan Much better NA SmallIf each field starts in the same column on each line, then the data do not need to be edited and could be read in with the following commands:
> columns_c(1,17,27,33,50,53) > cars_read.table("cars", sep=columns, header = T)When explicit columns are used as separators, Splus cannot automatically tell if the first line contains headers, so the argument header=T is specified.
Splus assumes that the first row in the text file contains the variable names, and that the first column with non-numeric non-duplicated names contains the row names. If the first row and/or the first column are not valid variable names (ie.: non-numeric with no duplicates), Splus uses the row numbers for the rows and/or "V1", "V2", etc. for the columns. Row names and column names may also be specified as arguments to the read.table() function. Suppose the text file cars2 does not contain any row or column names:
> car.names_c("Acura Integra 4", "Dodge Colt 4", ... > car.vars_c("Price", "Country", "Reliabilty", "Mileage", "Type") > cars_read.table("cars2",row.names=car.names,col.names=car.vars)If the text file does contain column labels, specifying header=T will let Splus know that the first line is a header line and should be ignored. It is also possible to use row.names=field # (to specify from which field row names are to come from) or row.names= var. name (to specify the name of the variable to be used as row names).
Variables in data frames can be anything that is indexed by the set of rows. The following can be used for statistical models:
> sapply(cars,data.class) Price Country Reliabilty Mileage Type "numeric" "factor" "factor" "numeric" "factor" * the function sapply() is similar to the function apply() but is used specifically with lists or data framesA numeric vector can be coerced to mode factor using the function as.factor(). Similarly, a matrix or a list can be coerced into a data frame using the function as.data.frame(). The function data.frame() is used to combine Splus objects into a data frame. Character or logical vectors are converted into factors, matrices contribute one variable per column in the matrix, lists contribute one variable for each component of the list, applied recursively, and the variables in data frames become variables in the new data frame. Numeric vectors, factors, and ordered factors each contribute a single variable. If any argument to data.frame() is of the form I(x), then x will retain its class in the new data frame (ie.: a character vector will not be converted into a factor).
Suppose we have a model which predicts cholesterol values by systolic blood pressure and age. We might want to use the model to predict cholesterol values over a regular grid of blood pressures and ages.
> systol_seq(110,130,by=10) > age_seq(20,50,by=10)There are 3 values of systol, 4 values of age, and 12 pairs of values. Since predict() and similar computations require the new data to be a data frame, the function expand.grid() is used to create a data frame with every combination of the two variables:
> chol.grid_expand.grid(systol=systol,age=age) > chol.grid systol age 1 110 20 2 120 20 3 130 20 4 110 30 5 120 30 6 130 30 7 110 40 8 120 40 9 130 40 10 110 50 11 120 50 12 130 50