Retrieve index of newly added row - for loop in R -
i trying retrieve index of newly-added row, added via loop.
starting beginning, have list of matrices of p-values, each variable number of rows , columns. because not groups have adequate number of treated individuals run t-tests. following prints console when access sample list:
$group1 normal treatment 1 treatment 2 treatment 1 1 na na treatment 2 1 1 na treatment 3 1 1 1 $group2 normal treatment 2 treatment 2 1 na treatment 4 1 1
i every group have same number of rows , columns, in correct order, missing values filled in nas. sample of like:
$group1 normal treatment 1 treatment 2 treatment 3 treatment 1 1 na na na treatment 2 1 1 na na treatment 3 1 1 1 na treatment 4 na na na na $group2 normal treatment 1 treatment 2 treatment 3 treatment 1 na na na na treatment 2 1 na na na treatment 3 na na na na treatment 4 1 1 na na
here code have far:
fix.results.row <- function(x, factors) { results.matrix <- x num <- 1 (i in factors){ if (!i %in% rownames(results.matrix)) { results.matrix <- rbind(results.matrix, na) rownames(results.matrix)[num] <- } num <- num + 1 } rownames(results.matrix) <- results.matrix[rownames(factors),,drop=false] return(results.matrix) }
in function above, x list of matrices, , factors list of factors in order want them. have similar function adding columns.
my problem, see it, in group 2. if sees i'm missing treatment 1, replace rowname treatment 2 rowname treatment 1, data treatment 2 mislabeled treatment 1. reorders variables way want them, data mislabeled!
if access index of newly-added row, changes group group, change specific row name. suggestions? please let me know if there's more information need provide. tried cover i'm not sure if there's else need.
this isn't elegant, might work better using 2 functions fill in rows , columns separately.
here, x
list of matrices; factor
optional list of desired row , column names
fix_rc <- function(x, factors) { f <- function(x) factor(ul <- unique(unlist(x)), levels = sort(ul)) if (missing(factors)) factors <- list(f(sapply(x, rownames)), f(sapply(x, colnames))) template <- matrix(na, length(factors[[1]]), length(factors[[2]]), dimnames = factors) lapply(x, function(xx) { ## original # xx <- rbind(xx, template[, colnames(xx)]) # xx <- cbind(xx, template[rownames(xx), ]) # xx[rownames(template), colnames(template)] ## better http://stackoverflow.com/questions/31050787/r-how-to-match-join-2-matrices-of-different-dimensions-nrow-ncol/31051218#31051218 xx <- as.data.frame.table(xx) template[as.matrix(xx[, 1:2])] <- xx$freq template }) }
here data using
l <- list(group1 = matrix(c(1,1,1,na,1,1,na,na,1), 3, 3, dimnames = list(paste('treatment', 1:3), c('normal', paste('treatment', 1:2)))), group2 = matrix(c(1,1,na,1), 2, 2, dimnames = list(paste('treatment', c(2,4)), c('normal','treatment 2')))) # $group1 # normal treatment 1 treatment 2 # treatment 1 1 na na # treatment 2 1 1 na # treatment 3 1 1 1 # # $group2 # normal treatment 2 # treatment 2 1 na # treatment 4 1 1
and can use this. note when don't supply factors
, function row , column names list of matrices
fix_rc(l) # $group1 # normal treatment 1 treatment 2 # treatment 1 1 na na # treatment 2 1 1 na # treatment 3 1 1 1 # treatment 4 na na na # # $group2 # normal treatment 1 treatment 2 # treatment 1 na na na # treatment 2 1 na na # treatment 3 na na na # treatment 4 1 na 1
i'm not sure treatment 3 in columns in desired output came from, can here if want so
fix_rc(l, factors = list(paste('treatment', 1:6), c('normal', paste('treatment', 1:3)))) # $group1 # normal treatment 1 treatment 2 treatment 3 # treatment 1 1 na na na # treatment 2 1 1 na na # treatment 3 1 1 1 na # treatment 4 na na na na # treatment 5 na na na na # treatment 6 na na na na # # $group2 # normal treatment 1 treatment 2 treatment 3 # treatment 1 na na na na # treatment 2 1 na na na # treatment 3 na na na na # treatment 4 1 na 1 na # treatment 5 na na na na # treatment 6 na na na na
Comments
Post a Comment