##------------------------------------------------------------------------------# ## Statistical Analysis of the PHALANX Project. # ## # ## MAS in Medizinphysik # ## Blockkurs "Computer in der Medizin" # ##------------------------------------------------------------------------------# ##------- Short version of ./PHALANX_statistics.R ----------------------- ##------------------------------------------------------------------------------# ## Directories and filenames # ##------------------------------------------------------------------------------# dir.base <- "D:/Martin/Documents/NDS/Computerkurs_2007/Statistik/R" ## MM: dir.base <- "/u/maechler/R/Meetings-Kurse-etc/2007-Biomechanik/R" dir.csv <- file.path(dir.base, "csv") ## Students: dir.base <- "http://stat.ethz.ch/Teaching/maechler/Phalanx" dir.csv <- dir.base if(FALSE) { ## Students can ``bootstrap'' themselves {get the data} via FF <- tempfile() download.file("http://stat.ethz.ch/Teaching/maechler/Phalanx/read-etc.R", FF) source(FF) } ##------------------------------------------------------------------------------# ## Read and merge data. # ##------------------------------------------------------------------------------# ## Simple utility function: readTabPhalanx <- function(filename) { head1 <- read.table(filename, nrows = 1, sep = ";", as.is = TRUE) dat <- read.table(filename, skip = 2, sep = ";", col.names = head1) stopifnot(names(dat)[1] == "SPECIMEN") ## use "SPECIMEN" as row names and drop as variable : rownames(dat) <- as.character(dat[,"SPECIMEN"]) # and not factor ## return dat[, -1] # '-1' : leave away first variable i.e. 'SPECIMEN' } ### -------------- Now read the 4 files ---------------------------------- d.anthro <- readTabPhalanx(file.path(dir.csv, "PHALANX_Anthropometric.csv")) d.dist <- readTabPhalanx(file.path(dir.csv, "PHALANX_Distal_25percent.csv")) d.mech <- readTabPhalanx(file.path(dir.csv, "PHALANX_Mechanical_Testing.csv")) d.spher <- readTabPhalanx(file.path(dir.csv, "PHALANX_Spherical_VOI.csv")) ## merge rnMerge <- function(d1,d2, ...) { ## Purpose: merge by row.names -- and make the "row.names" variable to row.names ## ---------------------------------------------------------------------- ## Arguments: d1, d2: two data frames with common row names ## ---------------------------------------------------------------------- ## Author: Martin Maechler, Date: 13 Mar 2007 d <- merge(d1, d2, by = "row.names", ...) ## set row names and omit them as variable: rownames(d) <- d[, 1] d[, -1] } d.A.M <- rnMerge(d.anthro, d.mech) ## Martin Stauber's : d.distal <- rnMerge(d.A.M, d.dist) d.spherical <- rnMerge(d.A.M, d.spher) ## All of them: d.All <- rnMerge(d.distal, d.spher, suffixes= c("_d", "_s")) ## Variabel-Namen vereinfachen: ---------- ## Use a simple function : mySub <- function(nms) { sub("Nr.of.", "Nr_", # shorten sub("^Ap", "AP", # <<-- Fix typo in file gsub("\\.Diameter$", ".dm", # shorten.. gsub("\\.$", "", # omit trailing "." gsub("\\.([._])", "\\1", # substitute ".." by "." and "._" by "_" nms))))) } names(d.All) <- mySub(names(d.All)) ## All variables but the "mechanical": d.X <- rnMerge(d.anthro, rnMerge(d.dist, d.spher, suffixes= c("_d", "_s"))) names(d.X) <- mySub(names(d.X))