Reputation: 23
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
Upvotes: 1
Views: 209
Reputation: 160407
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.)
Upvotes: 1
Reputation: 2143
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.
Upvotes: 1