gpt4 book ai didi

用字符串替换矩阵中的数字

转载 作者:行者123 更新时间:2023-12-04 16:43:12 24 4
gpt4 key购买 nike

我有一个包含整数的矩阵和一个包含多列的数据框。

矩阵:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 6 1 NA NA
[2,] 2 3 NA NA NA NA
[3,] 3 4 5 6 2 1
[4,] 6 6 2 3 3 NA
[5,] 1 2 1 4 5 6
[6,] 4 NA NA NA NA NA

数据框:
   V1   V2           V3             
1 "5P" "Fox" "28639"
2 "5P" "Horse" "33844"
3 "5P" "Cat" "Bes86"
4 "5P" "Seal" "Bes259"
5 "5P" "Snake" "Bes260"
6 "5P" "Platypus" "NSA8631"

实际数据框比这大得多(10000+ 行)。

我想要的是用我的数据框中 V2 的相应行替换矩阵中的数字。因此,所有“1”条目最终都为“Fox”,“2”为“Horse”,依此类推。
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,] Fox Seal Platypus Fox NA NA
[2,] Horse Cat NA NA NA NA
[3,] Cat Seal Snake Platypus Horse Fox
[4,] Platypus Platypus Horse Cat Cat NA
[5,] Fox Horse Fox Seal Snake Platypus
[6,] Seal NA NA NA NA NA

感谢您的任何帮助!

最佳答案

这似乎可以解决问题:

#you convert the matrix to vector
#use it to index df2$V2
#and then reconstruct the matrix
matrix(df2$V2[as.vector(mat)], ncol=6)

#Or actually even better as @PierreLafortune messaged me
#you don't even need as.vector as this occurs automatically
matrix(df2$V2[mat], ncol=ncol(mat)) #result is the same

数据:
mat <- as.matrix(read.table(header=T,text='    [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 6 1 NA NA
[2,] 2 3 NA NA NA NA
[3,] 3 4 5 6 2 1
[4,] 6 6 2 3 3 NA
[5,] 1 2 1 4 5 6
[6,] 4 NA NA NA NA NA'))

df2 <- read.table(text='V1 V2 V3
1 "5P" "Fox" "28639"
2 "5P" "Horse" "33844"
3 "5P" "Cat" "Bes86"
4 "5P" "Seal" "Bes259"
5 "5P" "Snake" "Bes260"
6 "5P" "Platypus" "NSA8631" ')

输出:
    [,1]       [,2]       [,3]       [,4]       [,5]    [,6]      
[1,] "Fox" "Seal" "Platypus" "Fox" NA NA
[2,] "Horse" "Cat" NA NA NA NA
[3,] "Cat" "Seal" "Snake" "Platypus" "Horse" "Fox"
[4,] "Platypus" "Platypus" "Horse" "Cat" "Cat" NA
[5,] "Fox" "Horse" "Fox" "Seal" "Snake" "Platypus"
[6,] "Seal" NA NA NA NA NA

关于用字符串替换矩阵中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32950227/

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