Skip to content

R: writing data from one matrix into another with keeping NA's

2 messages · Karl Weinmayer, Uwe Ligges

1 day later
#
On 18.11.2011 18:06, Karl Weinmayer wrote:
I think it is easier without using any extra package (such as qpcR or 
plyr), and a first shot that reproduces your code is given below:


data <- c(1, NA, 2, 3)
row <- 2
col <- 28
x <- matrix(data=data, nrow=row, ncol=col)
colnames(x) <- 1:28
x_sorted <- t(apply(x, 1, sort, decreasing=TRUE, na.last=TRUE))

###################
##DETERMINING LENGTH OF THE QUINTILES
q  <- rowSums(!is.na(x_sorted))
q_list <- matrix(q)
q_list <- q_list/5

##Function to check whether q is full number
check.integer <- function(N, tol = .Machine$double.eps^0.5) {
     abs(N - round(N)) < tol
}

##Round Quintiles to full integer
##Aggregate all q_list in one matrix
q_round <- ifelse(check.integer(q_list), q_list, ceiling(q_list))

#######################
##Obtaining 1st Quintile from data

p1 <- x_sorted[,1:max(q_round)]
for (i in 1:nrow(p1)) {
     if(q_round[i] < ncol(p1))
          is.na(p1[i,seq(q_round[i]+1, ncol(p1), by=1)]) <- TRUE
}



But if you simply want to calculate kinds of quantiles, see ?quantile 
with its 9 different types.


Uwe Ligges