Information Technology Reference
In-Depth Information
> tbl <- read.csv("table-data.csv", as.is=TRUE)
> str(tbl)
'data.frame': 3 obs. of 3 variables:
$ label : chr "low" "mid" "high"
$ lbound: num 0 0.674 1.64
$ ubound: num 0.674 1.64 2.33
Notice that the label variable now has character-string values and is no longer a factor.
Another useful feature is that input lines starting with a pound sign ( # ) are ignored,
which lets you embed comments in your datafile. Disable this feature by specifying
comment.char="" .
The read.csv function has many useful bells and whistles. These include the ability to
skip leading lines in the input file, control the conversion of individual columns, fill out
short rows, limit the number of lines, and control the quoting of strings. See the R help
page for details.
See Also
See the R help page for read.table , which is the basis for read.csv . See the write.csv
function for writing CSV files.
1.7 Creating a Vector
Problem
You want to create a vector.
Solution
Use the c(...) operator to construct a vector from given values.
Discussion
Vectors are a central component of R, not just another data structure. A vector can
contain numbers, strings, or logical values, but not a mixture.
The c(...) operator can construct a vector from simple elements:
> c(1,1,2,3,5,8,13,21)
[1] 1 1 2 3 5 8 13 21
> c(1*pi, 2*pi, 3*pi, 4*pi)
[1] 3.141593 6.283185 9.424778 12.566371
> c("Everyone", "loves", "stats.")
[1] "Everyone" "loves" "stats."
> c(TRUE,TRUE,FALSE,TRUE)
[1] TRUE TRUE FALSE TRUE
 
Search WWH ::




Custom Search