1

I want to read all files of the same format in the .txt format folder and get 1 data.frame. I know how to do this for .CSV (example below), but not for .TXT. Thanks to everyone who tells me)

temp <- list.files(pattern="*.csv")
Inst_test_Jule <- lapply(temp, read_csv) %>%
  rbind.fill(Inst_test)

For read once file in txt format I use

Ffact <- read.table("T:/test/work 0217.asc")

and I have

V1
<dbl>
V2
<dbl>
V3
<dbl>
V4
<dbl>
87406   2041    6779    20743   
87407   2014    6778    21553   
87408   2041    6780    20743   
87409   2041    6781    20743   
87410   2041    6782    20743   
87411   2014    6778    21553   

I load my file in cloud too...below in the comment

  • 2
    Don't worry about reading in a lot of files yet, worry about a single file first. Once you have that figured out, then wrap it with `lapply` and you should be off to the races. For instance, if it's a "simple" text file, you might be able to do `lapply(temp, read.table, header = TRUE)` (and an rbind-like operator). Is the file format straight-forward for reading in one file? – r2evans May 09 '20 at 22:16
  • @r2evans thanks for your answer. Yes, all files are the same in headings and number of columns, the only differences are in the number of lines and the numerical information in these lines. But, I did not quite understand how, after all, I should create 1 data.frame of the many .txt files immediately at the loading stage. – Иван Остащенко May 09 '20 at 23:02
  • 1
    Can you read in just one of the files? Stop thinking about doing it many times, just do it once. Does `read.table(temp[1], header=TRUE)` work? Once you read them in, combining multiple frames is identical: `rbind.fill` should work just fine, regardless of the `read.*` function used to read each individual file. – r2evans May 09 '20 at 23:15
  • 1
    If you're having trouble using `read.table` or `read_delim` or whatever, then it would help if you [edit] your question and include the first few lines of the text file (in a code block). – r2evans May 09 '20 at 23:18
  • @r2evans of course...all time I use read.table if need read txt file...I edit my question like you write now...thanks) – Иван Остащенко May 09 '20 at 23:32
  • 1
    If that is really what each text file looks like (literal ``? looks like `tibble` metadata), then you need to do a bit more work. If you can discard the poorly-referenced headers (and potentially assign names manually later), then perhaps `read.table(temp[1], skip=8, header=FALSE)` would work. – r2evans May 09 '20 at 23:39
  • @r2evans what I see "Error in file(file, "rt") : cannot open the connection" and code temp <- list.files("T:/testtew", pattern = "*.asc") Test_XT <- read.table(temp[1], skip=8, header=FALSE) – Иван Остащенко May 09 '20 at 23:57
  • 1
    More my point: if you cannot read one file at a time, don't try to read all files at once. That error has nothing to do with parsing the file, that's about finding the file, nothing to do with `read.table` or `lapply` or `map`. – r2evans May 10 '20 at 00:02

2 Answers2

1

Look at the recent post: Import several files from different directory (but similar structure in every case)

Similarly,

## NOT RUN, because I don't have a directory of delimited .txt files handy.
library(tidyverse)
myConcat <- 
  list.files("some/directory", recursive = FALSE, pattern = 
            "(?i)*.txt", full.names=TRUE) %>% 
  map_df( ~ read.delim(.x, header = TRUE, sep = "\t"))

You haven't said how your .txt files are delimited. I assume they're tab-delimited and have headers. The (?i) in the pattern is in case some of the files are suffixed as .TXT, .Txt etc.

David T
  • 1,993
  • 10
  • 18
  • problem with loading 1 file r2evans right because all time I use read.table("T:/0217.asc") for loading txt file. Your code good work i think, but loading not like I want...for example I send in cloud my file..[link] https://yadi.sk/d/Ub0VoY2PYIrn3Q – Иван Остащенко May 09 '20 at 23:21
1

You have two problems here: (1) how to read that file, once; (2) how to read many files that look like that.

files <- list.files("~/StackOverflow/13426927", pattern = "asc$", full.names = TRUE)
files
# [1] "C:\\Users\\r2/StackOverflow/13426927/61704300.R.asc"  
# [2] "C:\\Users\\r2/StackOverflow/13426927/61704300_2.R.asc"

read.table(files[1], header = FALSE, skip = 8)
#      V1   V2   V3    V4
# 1 87406 2041 6779 20743
# 2 87407 2014 6778 21553
# 3 87408 2041 6780 20743
# 4 87409 2041 6781 20743
# 5 87410 2041 6782 20743
# 6 87411 2014 6778 21553

Now that we know how to read one at a time, now all at once:

alldat <- lapply(files, read.table, header = FALSE, skip = 8)
out <- do.call(rbind.data.frame, alldat)
out
#       V1   V2   V3    V4
# 1  87406 2041 6779 20743
# 2  87407 2014 6778 21553
# 3  87408 2041 6780 20743
# 4  87409 2041 6781 20743
# 5  87410 2041 6782 20743
# 6  87411 2014 6778 21553
# 7  87406 2041 6779 20743
# 8  87407 2014 6778 21553
# 9  87408 2041 6780 20743
# 10 87409 2041 6781 20743
# 11 87410 2041 6782 20743
# 12 87411 2014 6778 21553

(Rename as needed.)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    As a side note, that data format looks a bit nonstandard (that's always a relative term, I admit). If there is reasonable control of the format of the data that is exported, that may facilitate things like: preserve the column names. – r2evans May 10 '20 at 22:35
  • 1
    I can export in my work program only one format .asc. but the column names not problem , I can use rename function after combine) – Иван Остащенко May 11 '20 at 17:07