gpt4 book ai didi

arrays - R:整数字符串矩阵到整数计数数组

转载 作者:行者123 更新时间:2023-12-04 02:40:21 25 4
gpt4 key购买 nike

我有一个由逗号分隔的整数字符串组成的字符矩阵:

> mat<-matrix(c(NA,"1",NA,"2,1","3","1,3,3"),nrow=2)
> mat
[,1] [,2] [,3]
[1,] NA NA "3"
[2,] "1" "2,1" "1,3,3"

我希望得到一个数字数组的输出,其中 z 索引表示矩阵中整数的计数:

, , 1

[,1] [,2] [,3]
[1,] NA NA NA
[2,] 1 1 1

, , 2

[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA 1 NA

, , 3

[,1] [,2] [,3]
[1,] NA NA 1
[2,] NA NA 2

我怎样才能做到这一点?

要了解数据的规模,最终数组的尺寸约为 20,000 x 2,000 x 200,矩阵将是数组的前两个维度 (20,000 x 2,000)。

最佳答案

这使用循环,可能不是最有效的解决方案:

mat<-matrix(c(NA,"1",NA,"2,1","3","1,3,3"),nrow=2)

#split the strings
temp <- strsplit(mat, ",", fixed=TRUE)

#unique values
levels <- na.omit(unique(do.call(c, temp)))

#convert to factors and use table
temp <- t(sapply(temp, function(x) table(factor(x, levels=levels))))

#make it an array
array(temp, c(nrow(mat), ncol(mat), length(levels)))
# , , 1
#
# [,1] [,2] [,3]
# [1,] 0 0 0
# [2,] 1 1 1
#
# , , 2
#
# [,1] [,2] [,3]
# [1,] 0 0 0
# [2,] 0 1 0
#
# , , 3
#
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 0 0 2

编辑:

这避免了在循环中应用 tablefactor 并且应该更快:

temp <- strsplit(mat, ",", fixed=TRUE)

id <- rep(seq_along(temp), sapply(temp, length))
temp <- factor(do.call(c, temp))
array(t(table(temp, id)), c(nrow(mat), ncol(mat), length(levels(temp))))

关于arrays - R:整数字符串矩阵到整数计数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20047270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com