Hi,
I am looking to build even quintiles for a set of data. I managed to get it
done, but I would like to know if there is a more direct way to write the
data from my loop output x in the bottom of the code into the "empty" matrix
p1, which I filled with NA's. The way I am doing it at the moment is, more
or less adding the matrix x after p1 and then deleting in a second step the
unnecessary rows. The empty matrix p1 has always the same number of rows, as
the original data. So the final output will have the double number of rows,
which I have to cut back then again. I couldn't come up with any different
way of writing the data without having R either give me an error for
different dimensions or overwriting the unmatched NA's and fill the whole
matrix by repeating the row data.
I am using the qpcR package and rbind.na. Is there a different way of
achieving the same but more directly? I know it's just one additional line,
but it's still bugging me and it will definitely come in handy at some time.
Best regards,
Karl
##Loading Packages
library(qpcR)
data = c(1,NA, 2, 3)
row=2
col=28
x <- matrix(data=data, nrow=row, ncol=col)
colnames(x) <- c(1:28)
x_sorted <- t(apply(x, 1, sort, decreasing=T, na.last=T))
q_list <- matrix(data = 0, nrow= nrow(x), ncol=1)
###################
##DETERMINING LENGTH OF THE QUINTILES
q <- apply((!is.na(x_sorted)),1, sum)
q_list[q_list==0] <- q
q_list = q_list/5
##Function to check wether q is full number
check.integer <- function(N){
!length(grep("[^[:digit:]]", as.character(N)))
}
##Round Quintiles to full integer
q_round <- matrix(data = 0, nrow= nrow(q_list), ncol = ncol(q_list))
##Aggregate all q_list in one matrix
for (i in 1:nrow(q_round)) {
if(check.integer(q_list[i]) == TRUE) q_round[i] = q_list[i] else q_round[i]
= floor(q_list[i]) + 1
}
#######################
##Obtaining 1st Quintile from data
p1 <- matrix(nrow=nrow(q_round), ncol=max(q_round))
for (i in 1:nrow(p1)) {
x <- t(as.matrix(x_sorted[i, 1:q_round[i]]))
rbind.fill.matrix(x)
print(x)
p1 <- rbind.na(p1, x)
}
######################
##Removing unnecessary rows
p1 <- p1[-(1:nrow(q_round)),]
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.