【问题标题】:Is there a function in R to separate .txt files with no delimiter?R中是否有一个函数来分隔没有分隔符的.txt文件?
【发布时间】:2020-05-16 20:54:21
【问题描述】:

我获得了一个 .txt 格式的数据集,我需要将其导入 R 以进行研究分析。 .txt 文件有一列带有数字字符,没有标题,也没有分隔符。以下是 .txt 文件中的几个示例:

4878578572809275874037093859845083594859474905704627402739385785748756 0934893758795493758745846784678576857458708476968983984980985974687586 3989458476857609379087685796847586770493706759787398499485957658968590

我想知道如何根据变量在 R 中分隔这些字符。所以,我想将前 3 个数字用于变量 1,接下来的 5 个数字用于变量 2,接下来的 2 个数字用于变量 3,依此类推。

是否有我可以在 R 中使用的表达式,允许我根据每个变量所需的字符数将 .txt 文件中的观察结果分开?

我尝试过 read.fwf 但它不起作用。或者,也许我尝试不正确。

我们将非常感谢您对这个问题的帮助!

【问题讨论】:

标签: r delimiter read.fwf


【解决方案1】:

更好的解决方案

library(utils)
raw <- "4878578572809275874037093859845083594859474905704627402739385785748756
0934893758795493758745846784678576857458708476968983984980985974687586
3989458476857609379087685796847586770493706759787398499485957658968590"

# Put your data in a temporary file.  You shouldn't have to do this, you data
# is already sitting in a file.
ff <- tempfile()
cat(file = ff, raw)

现在用read.fwf读回它

answer <-  suppressWarnings(
  read.fwf(ff, widths = c(3, 5, 2)))
# Remember to clean up after ourselves.
unlink(ff)  # Again, you won't need to do this; your file isn't temporary.

answer    
   V1    V2 V3
1 487 85785 72
2  93 48937 58
3 398 94584 76

使用正则表达式的初始答案

您可以使用正则表达式(正则表达式)。我已经编码了您在帖子中所说的位置中断:

library(tidyverse)
library(readr)
byRegx <- function(raw){
  rawSpl <- str_match(raw[1], "(?x) (^\\d{3}) (\\d{5}) (\\d{2}) (.+)")[1,]
  tibble(apples = rawSpl[2], bananas = rawSpl[3], carrots = rawSpl[4], 
         therestofthem = rawSpl[5])
}

将您的输入读入表格,然后应用byRegex 函数

inputTbl<- tibble(
            raw = readr::read_lines("4878578572809275874037093859845083594859474905704627402739385785748756
                     0934893758795493758745846784678576857458708476968983984980985974687586
                     3989458476857609379087685796847586770493706759787398499485957658968590")) %>% 
  mutate(morecol = map(str_trim(raw), byRegx)) %>% 
  unnest() %>% 
  select(- raw)

inputTbl
# A tibble: 3 x 4
# apples bananas carrots therestofthem                                               
# <chr>  <chr>   <chr>   <chr>                                                       
# 1 487    85785   72      809275874037093859845083594859474905704627402739385785748756
# 2 093    48937   58      795493758745846784678576857458708476968983984980985974687586
# 3 398    94584   76      857609379087685796847586770493706759787398499485957658968590

【讨论】:

  • 你好大卫,谢谢你的帮助!我已经让它工作,直到输入表步骤。我收到一条错误消息,指出它找不到函数“read_lines”。有没有办法解决这个问题?还是我在某个地方犯了错误……?再次感谢!
  • 我编辑添加了readr 库。顺便说一句,正则表达式中的 (?x) 允许我输入空格,并使正则表达式更具可读性。
  • 我意识到有比正则表达式更好的答案。 utils 包中的 read.fwf(固定宽度文件)是更好的选择。注意,这种方法将片段作为数字引入,正则表达式将它们作为字符数据引入。
【解决方案2】:

你可以使用substring函数。

txt <- "487857857280927587403709385984508359485947490570462740273938578574875609348937587954937587458467846785768574587084769689839849809859746875863989458476857609379087685796847586770493706759787398499485957658968590"

select <-  c(3 , 5, 2)  # vector of number of words to be extracted

out <- as.numeric(substring(txt, 
                            cumsum(c(1, select[-length(select)])), 
                            cumsum(select)))
out
#[1]   487 85785    72

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多