1

This seems so elementary that I am almost embarrassed to ask the question but I just can't get this to work. What would be the equivalent apply for this. This is a contrived example as I want to access the data for each cell with its row and column index but it represents the issue well.

ndat<-matrix(c(1:100), ncol=10)
for (i in 1:nrow(ndat)) {
    for (j in 1:ncol(ndat)) {
        cat(ndat[i,j]," ")
    }  
    cat("\n")
}

Here is the actual problem. I am trying to plot the matrix on a grid. I know that textplot can plot matrices but I need more control on the grid. The location of the text depends on the row and column indices. The code is as follows:

plot.new()
tlx <- 0.04
tly <- 0.96
label_shift <- 0.04
text(tlx + label_shift, tly, "Columns", cex=1, adj=c(0,0))
text(tlx, tly - label_shift, "Rows", cex=1, adj=c(0,1), srt=-90)
ndat<-matrix(c(1:100), ncol=10)
dimnames(ndat) <- list(paste("R",1:10,sep=""), paste("C",1:10,sep=""))
row_name_space <- 0.1
col_name_space <- 0.1
data_width <- 0.075
data_height <- 0.075
x1 <- 1.5 * tlx 
x2 <- x1 + row_name_space + ncol(ndat) * data_width
y1 <- tly - 0.5*tlx
y2 <- y1 - col_name_space - nrow(ndat) * data_height
lines(c(x1,x1), c(y1,y2))
lines(c(x1,x2), c(y1,y1))
vertical_lseg <- function(n) {
        lx <- x1 + row_name_space + (n - 1) * data_width
        xv <- c(lx, lx)
        yv <- c(y1, y2)
        lines (xv, yv)
}
sapply(1:(ncol(ndat)+1), vertical_lseg)
horizontal_lseg <- function(n) {
        ly <- y1 - col_name_space - (n - 1) * data_height
        xv <- c(x1, x2)
        yv <- c(ly, ly)
        lines (xv, yv)
}
sapply(1:(nrow(ndat)+1), horizontal_lseg)
sapply(1:nrow(ndat), function(x) text(1.5 * tlx + row_name_space / 2, tly - 0.5*tlx - col_name_space - (x - 0.5) * data_height, rownames(ndat)[x], adj=c(0.5,0.5), cex=1))
sapply(1:ncol(ndat), function(x) text(1.5*tlx + col_name_space + (x - 0.5) * data_width, tly - 0.5*tlx - col_name_space /2, colnames(ndat)[x], adj=c(0.5,0.5), cex=1))
# This is where the text would be plotted. The calculation of x and y is right but the number of elements that outer produces seems to be 10,000
# rather than 100
outer(1:nrow(ndat), 1:ncol(ndat), FUN=function(r,c) text(1.5*tlx + col_name_space + (c - 0.5) * data_width, 
                                                         tly - 0.5*tlx - col_name_space - (r - 0.5) * data_height, 
                                                         toString(ndat[r,c]), adj=c(0.5,0.5), cex=1))
4

3 回答 3

4

来自?apply

MARGIN:一个向量,给出函数将被应用的下标。例如,对于矩阵,“1”表示行,“2”表示列,“c(1, 2)”表示行和列。'X' 已命名为 dimnames,它可以是选择维度名称的字符向量。

所以你要:

ndat<-matrix(c(1:100), ncol=10)
apply(ndat, 1:2, cat)
于 2013-06-18T22:18:33.790 回答
4

不知道你想做什么就很难说,但矩阵是有维度的向量。所以矢量化函数通常可以直接正常工作:

ndat<-matrix(c(1:100), ncol=10)
ndat^2

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1  121  441  961 1681 2601 3721 5041 6561  8281
 [2,]    4  144  484 1024 1764 2704 3844 5184 6724  8464
 [3,]    9  169  529 1089 1849 2809 3969 5329 6889  8649
 [4,]   16  196  576 1156 1936 2916 4096 5476 7056  8836
 [5,]   25  225  625 1225 2025 3025 4225 5625 7225  9025
 [6,]   36  256  676 1296 2116 3136 4356 5776 7396  9216
 [7,]   49  289  729 1369 2209 3249 4489 5929 7569  9409
 [8,]   64  324  784 1444 2304 3364 4624 6084 7744  9604
 [9,]   81  361  841 1521 2401 3481 4761 6241 7921  9801
[10,]  100  400  900 1600 2500 3600 4900 6400 8100 10000

我提到这一点的原因是,与apply..

于 2013-06-18T22:26:47.507 回答
2

您正在做的事情可以这样模拟:

apply(ndat, 1, function (k) cat(k, "\n"))
于 2013-06-18T22:22:12.153 回答