Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 非NA细胞在基质中的位置_R_Na - Fatal编程技术网

R 非NA细胞在基质中的位置

R 非NA细胞在基质中的位置,r,na,R,Na,考虑以下矩阵 m <- matrix(letters[c(1,2,NA,3,NA,4,5,6,7,8)], 2, byrow=TRUE) ## [,1] [,2] [,3] [,4] [,5] ## [1,] "a" "b" NA "c" NA ## [2,] "d" "e" "f" "g" "h" m非钠元素的列(和行)标记可以通过 which(!is.na(m), TRUE) 完整答案: 由于您希望按行工作,但R按列处理向量,因此使用m的转置更容

考虑以下矩阵

m <- matrix(letters[c(1,2,NA,3,NA,4,5,6,7,8)], 2, byrow=TRUE)
##      [,1] [,2] [,3] [,4] [,5]
## [1,] "a"  "b"  NA   "c"  NA  
## [2,] "d"  "e"  "f"  "g"  "h" 
m非钠元素的列(和行)标记可以通过

which(!is.na(m), TRUE)

完整答案:

由于您希望按行工作,但R按列处理向量,因此使用
m
的转置更容易

t_m <- t(m)
n_cols <- ncol(m)
非钠元素的列(和行)标记可通过以下方式获得:

which(!is.na(m), TRUE)

完整答案:

由于您希望按行工作,但R按列处理向量,因此使用
m
的转置更容易

t_m <- t(m)
n_cols <- ncol(m)

这将使您接近:

    cols <- col(m)
    cbind(cols[which(is.na(m))-1],cols[is.na(m)])
         [,1] [,2]
    [1,]    2    3
    [2,]    4    5

cols这会让你接近:

    cols <- col(m)
    cbind(cols[which(is.na(m))-1],cols[is.na(m)])
         [,1] [,2]
    [1,]    2    3
    [2,]    4    5

cols至少对我来说,
结果中的值来自哪里并不明显。您能详细说明一下吗?这两个NAs分别用“b”和“c”分组。所有其他元素只有一个对应的列索引。至少对我来说,
结果中的值来自哪里并不明显。您能详细说明一下吗?这两个NAs分别用“b”和“c”分组。所有其他元素只有一个对应的列索引。
    cols <- col(m)
    cbind(cols[which(is.na(m))-1],cols[is.na(m)])
         [,1] [,2]
    [1,]    2    3
    [2,]    4    5