【问题标题】:Reading data in R from a single column text file从单列文本文件中读取 R 中的数据
【发布时间】:2014-01-25 19:59:57
【问题描述】:

我的名为 myfile.txt 的文件设置如下:

Column1
Column2
Column3
...
Column10
Row1
1
2
3
4
Row2
5
6
7
8
...

行数最终达到 100 并且我在使用 read.table 命令时遇到了问题。我不是一个有经验的 R 用户,所以我只需要弄清楚这一点并完成它。

我认为 col.names 看起来像:

read.table("myfile.txt", col.names = 1:10)

但这没有用

【问题讨论】:

  • 我会考虑更改创建此文件的任何内容。这是一种可怕的格式。
  • 虽然:将其全部折叠成一个字符串(使用 '\n' 或其他已知的虚拟字符串),然后在 Row\\d+\\n 上拆分

标签: r import read.table


【解决方案1】:

一个例子myfile.txt

Column1
Column2
Column3
Column4
Row1
1
2
3
4
Row2
5
6
7
8

读取文件并创建矩阵:

lin <- scan("myfile.txt", "") # read lines

lin2 <- grep("Column|Row", lin, value = TRUE, invert = TRUE) # values

matrix(as.numeric(lin2), ncol = sum(grepl("Column", lin)), byrow = TRUE)
  # create matrix

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

如果第一行没有命名Column...,但包含实际的列名,您可以使用以下方法:

lin <- scan("myfile.txt", "") # read lines

idx <- grep("^Row", lin) # index of lines containing 'Row'

lin2 <- lin[-(c(seq(1, idx[1] - 1), idx))] # actual values

matrix(as.numeric(lin2), nrow = length(idx), 
       dimnames = list(NULL, lin[seq(1, idx[1] - 1)]), byrow = TRUE)

      Column1 Column2 Column3 Column4
 [1,]       1       2       3       4
 [2,]       5       6       7       8

【讨论】:

  • 嗨,我喜欢这个答案,尽管结果不正确。第 1 行应该是 1 2 3 4。不是 1 3 5 7
  • @user1083079 哦,对不起!我忘记了matrix 函数的参数byrow = TRUE。现在,它起作用了。查看更新。
【解决方案2】:

Ricardo 给出了一个提示,这里有一个让它起作用的方法:

x <- read.table(text="Column1
Column2
Column3
Column10
Row1
1
2
3
4
Row2
5
6
7
8")

现在插入换行符:

(combined <- paste(x[[1]], collapse='\n'))
[1] "Column1\nColumn2\nColumn3\nColumn10\nRow1\n1\n2\n3\n4\nRow2\n5\n6\n7\n8"

按行拆分\d+\n:

(comb.split <- strsplit(combined, 'Row\\d+\\n'))
[[1]]
[1] "Column1\nColumn2\nColumn3\nColumn10\n" "1\n2\n3\n4\n"                          "5\n6\n7\n8"                           

在换行符上分割这些元素:

(split.list <- strsplit(comb.split[[1]], '\\n'))
[[1]]
[1] "Column1"  "Column2"  "Column3"  "Column10"

[[2]]
[1] "1" "2" "3" "4"

[[3]]
[1] "5" "6" "7" "8"

强制数字(如果适用):

(numeric.list <- lapply(split.list[-1], as.numeric))
[[1]]
[1] 1 2 3 4

[[2]]
[1] 5 6 7 8

创建数据框:

dat <- do.call(rbind, numeric.list)
colnames(dat) <- split.list[[1]]
dat
     Column1 Column2 Column3 Column10
[1,]       1       2       3        4
[2,]       5       6       7        8

您确实会丢失此处的行名。如果您知道它们是什么,可以使用rownames(dat)&lt;- names 添加它们。

【讨论】:

    【解决方案3】:

    1。样本数据

    X <- read.table(text=
    "Column1
    Column2
    Column3
    Column10
    Row1
    1
    2
    3
    4
    Row2
    5
    6
    7
    8
    Row99
    1
    2
    3
    4
    Row100
    5
    6
    7
    8", stringsAsFactors=FALSE)
    

    2。读入并处理

    # Some string that does not appear naturally in your data
    dummyCollapse <- "\n"  # eg:  "zQz5Nsddfdfjjj"
    ## Make sure to properly escape the escape char.
    dummyFind <- "\\n"  
    
    flat  <- paste(unlist(X), collapse=dummyCollapse)
    splat <- strsplit(flat, paste0("Row\\d+", dummyFind))
    
    ## strsplit returns a list.  You likely want just the first element
    splat <- splat[[1]]
    
    ## weed out colnames 
    cnms <- splat[[1]]  # now, the first element is the coloumn names from the weird data structure
    # split them, also on dummyFind
    cnms <- strsplit(cnms, dummyFind) 
    # again, only want the first element
    cnms <- cnms[[1]]  
    
    ## Weed out the rows
    rows <- tail(splat, -1)
    
    # split on dummy find
    rows <- strsplit(rows, dummyFind)
    ## NOTE: This time, do NOT take just the first element from strsplit. You want them all
    
    ## Combine the data into a matrix
    MyData <- do.call(cbind, rows)
    ## Coerce to data.frame, if you'd like
    MyData <- as.data.frame(MyData)
    ## Add in Column names
    colnames(MyData) <- cnms
    

    3。结果

    > MyData
      Column1 Column2 Column3 Column10
    1       1       5       1        5
    2       2       6       2        6
    3       3       7       3        7
    4       4       8       4        8
    

    4。向以这种格式向您发送数据的人发送电子邮件并对其进行打击

    This should help

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多