【问题标题】:Reading in a matrix to R将矩阵读入 R
【发布时间】:2014-10-05 04:02:16
【问题描述】:

我在一个文件中有一个矩阵,格式如下:

         V1        V2        V3        V4        V5       V6        V7
[1,] 17.67787 12.375978 12.007860 16.089949 24.864464 37.64243 42.711561
 ...
[10,] 42.89655 21.535867  7.975470  6.580414 10.326551 11.06297 11.201733
        V8       V9      V10       V11       V12       V13       V14
[1,] 30.41993 35.46864 16.97427 10.992030 11.408483 17.417670 33.815149
 ...
[10,]

10 行和 N 列向量以 7 列为一组。

如何将其作为矩阵读入 R 中?

扫描抛出错误:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  scan() expected 'a real', got 'V1'

如果我尝试调用“as.matrix(read.table(..))”

我明白了:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 11 did not have 8 elements

【问题讨论】:

  • 我认为发布相关文件的实际head -11 会很有帮助。话虽如此,如果文件中确实散布了其他列标题(如V9-V16),那么您需要使用readLines,卷起袖子并进行一些后期处理。
  • 编辑我的帖子以包含我需要阅读的文件的实际摘录。
  • @akun 我得到:read.table 中的错误(“centroid0.txt”,fill = TRUE,header = TRUE):不允许重复的 'row.names'
  • @raf 请在下面查看我的解决方案。我刚刚从 pastebin 复制了数据并使用 readLines(textConnection( 运行它,或者您可以通过 readLines("name.txt") 从文件中读取它

标签: r matrix vector


【解决方案1】:

你可以试试:

 lines <- readLines(textConnection("V1 .... #... all the data you showed in `pastebin`
                 ...   33.21421"))

如果您是从file阅读它

 lines <- readLines("raf.txt")
 lines1 <- gsub("\\[.*]","",lines) #remove the `[,number]` part in the beginning
 library(stringr)
 lines2 <- str_trim(lines1) #remove the trailing/leading spaces

在这里,我使用grepcumsum 创建的索引将lines2 分成list 中的组,以便每组新组都有一个标题和数据。然后使用lapplyread.table读取list中的文件

  lst1 <- lapply(split(lines2, cumsum(grepl("^V", lines2))), 
                  function(x) read.table(text=x, header=TRUE))
  names(lst1) <- NULL
  res <- do.call(`cbind`, lst1)

如果你想把它转换成矩阵

 m1 <- as.matrix(res)
 dim(res)
 #[1]  10 128

 res[1:3,1:3]
 #       V1       V2       V3
 #1 17.67787 12.37598 12.00786
 #2 29.44688 19.44888 15.06014
 #3 30.49377 19.64495 11.15946

【讨论】:

    【解决方案2】:

    我构建了一个文本对象'txt',它符合您的原始描述。

    dput(txt)
    "         V1    V2    V3    V4    V5    V6    V7    V8\n [1] 10074 10146 10079 10091 10040 10066 10009 10152\n [2] 10137 10136 10032 10139 10038 10122 10123 10095\n [3] 10046 10120 10062 10061 10149 10029 10030 10059\n [4] 10003 10028 10148 10050 10057 10100 10144 10084\n [5] 10076 10012 10114 10073 10026 10135 10130 10083\n [6] 10007 10119 10063 10078 10086 10160 10125 10087\n [7] 10031 10090 10021 10092 10093 10067 10106 10129\n [8] 10004 10102 10113 10134 10042 10064 10037 10140\n [9] 10101 10156 10060 10121 10097 10002 10109 10033\n[10] 10075 10096 10024 10089 10115 10147 10036 10103\n         V9   V10   V11   V12   V13   V14   V15   V16\n [1] 10153 10107 10049 10143 10047 10126 10039 10018\n [2] 10065 10127 10048 10133 10108 10124 10117 10077\n [3] 10105 10051 10131 10069 10098 10058 10088 10006\n [4] 10132 10104 10112 10138 10128 10027 10043 10145\n [5] 10010 10072 10151 10111 10110 10052 10020 10082\n [6] 10023 10016 10044 10158 10159 10041 10155 10019\n [7] 10099 10008 10094 10142 10045 10068 10070 10015\n [8] 10013 10080 10053 10071 10085 10014 10056 10034\n [9] 10022 10011 10150 10054 10154 10035 10081 10118\n[10] 10116 10055 10017 10005 10025 10157 10141 10001"
    
     tcon <- textConnection(txt) # the first description did not have commas
    

    代替txt,您可以替换为file() 调用;原则是您可以增量阅读连接:

     cbind( read.table(text= readLines(tcon,n=11), header=TRUE),  # first 11 lines
            read.table(text= readLines(tcon,n=11), header=TRUE))  # second 11
    
            V1    V2    V3    V4    V5    V6    V7    V8
    [1]  10074 10146 10079 10091 10040 10066 10009 10152
    [2]  10137 10136 10032 10139 10038 10122 10123 10095
    [3]  10046 10120 10062 10061 10149 10029 10030 10059
    [4]  10003 10028 10148 10050 10057 10100 10144 10084
    [5]  10076 10012 10114 10073 10026 10135 10130 10083
    [6]  10007 10119 10063 10078 10086 10160 10125 10087
    [7]  10031 10090 10021 10092 10093 10067 10106 10129
    [8]  10004 10102 10113 10134 10042 10064 10037 10140
    [9]  10101 10156 10060 10121 10097 10002 10109 10033
    [10] 10075 10096 10024 10089 10115 10147 10036 10103
            V9   V10   V11   V12   V13   V14   V15   V16
    [1]  10153 10107 10049 10143 10047 10126 10039 10018
    [2]  10065 10127 10048 10133 10108 10124 10117 10077
    [3]  10105 10051 10131 10069 10098 10058 10088 10006
    [4]  10132 10104 10112 10138 10128 10027 10043 10145
    [5]  10010 10072 10151 10111 10110 10052 10020 10082
    [6]  10023 10016 10044 10158 10159 10041 10155 10019
    [7]  10099 10008 10094 10142 10045 10068 10070 10015
    [8]  10013 10080 10053 10071 10085 10014 10056 10034
    [9]  10022 10011 10150 10054 10154 10035 10081 10118
    [10] 10116 10055 10017 10005 10025 10157 10141 10001
    

    这对较长的文件执行相同的操作。并且转换为矩阵仍然是微不足道的:

    txt <-readLines(file("~/Downloads/YjwpsANG.txt"))
    tcon <-textConnection(txt)
    X <- cbind(  read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE),
     read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE), 
    read.table(text= readLines(tcon,n=11), header=TRUE),
    read.table(text= readLines(tcon,n=11), header=TRUE) )
    

    【讨论】:

    • 我可能解释得不好。我希望将矩阵作为 R 中的 10xN 矩阵读入,这样我就可以对其进行矩阵运算。
    • 我给了你一个 10 X N 的数据框。只需使用data.matrix 函数转换为矩阵即可。
    • 实际上这似乎不起作用。我只是一遍又一遍地得到相同的 11 行......
    • 将文本文件或链接发布到公共位置。
    • 您说“它不起作用”,但是当我通过从 textConnection 对象读取来执行此操作时,我不会重复读取文件顶部。当没有得到预期的结果时,您需要显示您的代码。短语“它不工作”中的歧义可能是失败的方式,也可能是“它”的含义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2021-02-11
    • 2013-11-10
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多