Learn R Programming

reader (version 1.0.6)

force.vec: returns a vector if 'unknown.data' can in anyway relate to such:

Description

if the name of a file with a vector or vector, then reads the file, if a matrix or dataframe, then preferentially return rownames, otherwise return first column - designed to search for IDs. Using this within functions allows flexibility in the specification of a datasource for vectors

Usage

force.vec(unknown.data, most.unique = TRUE, dir = NULL, warn = FALSE)

Arguments

unknown.data
something that is or can refer to a 2d dataset
most.unique
if TRUE, select most unique column if a unknown.data is a matrix, else select the first column
dir
if unknown.data is a file name, specifies directory(s) to look for the file
warn
whether to display a warning if unknown.data is a matrix

Value

returns a vector regardless of the original object type

See Also

force.frame

Examples

Run this code
# create a matrix, binary file, and simple vector
my.ids <- paste("ID",1:4,sep="")
my.dat <- sample(2,4,replace=TRUE)
test.files <- c("temp.rda")
mymat <- cbind(my.ids,my.dat)
save(mymat,file=test.files[1])
test.vecs <- list(myvec = my.ids,
 myrda = test.files[1],mymat=mymat)
# show dimensions of each test object
sapply(test.vecs,function(x) {  if(is.null(dim(x))){ length(x)} else {dim(x)}})
# run the function on each, reporting specs of the object returned
for (cc in 1:3) {
  the.vec <- force.vec(test.vecs[[cc]])
  cat(names(test.vecs)[cc],": length() => ",
      length(the.vec),"; is() => ",is(the.vec)[1],"\n",sep="")
}
unlink(test.files)

Run the code above in your browser using DataLab