Karl Weinmayer
2011-Nov-18 17:06 UTC
[R] R: writing data from one matrix into another with keeping NA's
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]]
Uwe Ligges
2011-Nov-19 19:11 UTC
[R] R: writing data from one matrix into another with keeping NA's
On 18.11.2011 18:06, Karl Weinmayer wrote:> 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.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> > > 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]] > > ______________________________________________ > R-help at r-project.org 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.