Data Science Desktop Survival Guide
by Graham Williams |
|||||
Read Strings from a File |
20180604 There may be occasions where we would like to load a dataset from a file as strings, one line as a string, returning a vector of strings. We achieve this using the function base::readLines(). In the following example we access the system file weather.csv that is provided by the rattle package.
library(glue) # Format strings: glue().
dsname <- "weather" # Dataset name. ftype <- "csv" # Source dataset file type. fname <- glue("{dsname}.{ftype}") fname %T>% print() %>% system.file(ftype, ., package="rattle") %>% readLines() -> ds
Show the first few lines using utils::head():
|
head(ds)
Find those strings that contain a specific pattern using base::grep().
|
grep("ENE", ds)
grep("ENE", ds, value=TRUE)
|