If it helps, I'm running R 3.3.1 on a Macbook Pro OS El Captain...
I am trying to read in a folder of similar data files. I've checked the directory and the files are where they should be:
list.files('../data/')
[1] "B101.txt" "B101p2.txt" "B116.txt" "B6.txt" "B65.txt" "B67.txt" "B67p2.txt"
[8] "B70.txt" "B71.txt" "B71p2.txt" "B95.txt" "B95p2.txt" "B96.txt" "B96p2.txt"
[15] "B98.txt" "B98p2.txt" "B99.txt" "B99p2.txt"
The following is my code and error:
a = ldply(
.data = list.files(
path = '../data/'
)
, .fun = function(x){
to_return = read.table(
file = x
, skip = 20
, sep = '\t'
, fill = TRUE
)
return(to_return)
}
, .progress = 'text'
)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'B101.txt': No such file or directory
I do not know what the problem is as all searches for those errors suggest fixing the directory. I have also checked the data files and can read an individual file using:
read.table('../data/B101.txt', skip = 20, sep = '\t', fill=TRUE)
Could someone please help me to fix the problem of reading in the whole folder. I'm trying to sort out the script with a small number of files but will need it to run for a much larger number, so reading them in one by one isn't practical. Thanks.
By default, list.files returns only the filename itself, not including the leading (relative or absolute) path (if any). When dealing with files potentially in another directory, you need to include full.names = TRUE:
a = ldply(
.data = list.files(
path = '../data/',
full.names = TRUE
)
, .fun = function(x){
to_return = read.table(
file = x
, skip = 20
, sep = '\t'
, fill = TRUE
)
return(to_return)
}
, .progress = 'text'
)
Related
I want to extract column called X1 out of 168 different .csv files, called table3_2, table3_3, table3_4, table3_5..., table3_168, all held in one folder (folder1). Then, merge into one new df. Contents of the column is factor.
Trying this code but can't get it to work.
folder1 <- "folder1"
folder2 <- "folder2" # destination folder
write_to <- function(file.name) {
file.name <- paste0(tools::file_path_sans_ext(basename(file.name)), ".csv")
df <- read.csv(paste(folder1, file.name, sep = "/"), header = FALSE, sep = "/")[X1]
write.csv(df, file = past(folder2, file.name, sep= "/"))
}
files <- list.files(path = folder1, pattern = "*.csv")
lapply(X = paste(folder1, files, sep= "/"), write_to)
This comes up with the error:
Error in file(file, "rt") : cannot open the connection
In addition: warning message:
In file(file, "rt") :
cannot often file folder1/folder1.csv: No such file or directory
So, I am not calling in the correct names of the table, and maybe not directing R to the correct folder (I've set the wd to folder1).
Any suggestions would be greatly appreciated.
Many thanks
There are a few minor issues that stand out, e.g. you have a typo in file = past(folder2, file.name, sep= "/") (should be paste() not past()) but perhaps a simpler approach would suit, e.g. using vroom:
library(vroom)
files <- fs::dir_ls(glob = "table3_*csv")
data <- vroom(files, id = "ID", col_select = c(ID, X1))
data
vroom_write(data, file = "~/xx/folder2/new_df.csv")
# replace "xx" with your path
Does this approach solve your problem?
I'm a bit stuck with this code... The purpose is to read only text files from a folder with few different kind of files, take a column for each one and create a data frame with every extracted column (cbind.fill is a hand-made function that add a new column and fill the "empty" spaces with NA values). Here is the code:
setwd("...folderOfInterest/")
genes_data <- data.frame()
for(i in list.files(pattern = "^GO_.*txt", full.names = TRUE)){
print(i) #this works perfectly, it only prints desired files...
q <- read.table(i, header = TRUE, sep = "\t", quote = NULL)
genes_data <- cbind.fill(genes_data, q[,2])
}
As #Adam B suggests, here is the print(i) output and a screenshot of the folder (folder_screenshot):
[1] "./GO_ALPHA_AMINO_ACID_CATABOLIC_PROCESS.xls"
[1] "./GO_ALPHA_AMINO_ACID_METABOLIC_PROCESS.xls"
[1] "./GO_ALPHA_BETA_T_CELL_ACTIVATION.xls"
[1] "./GO_AMINO_ACID_BETAINE_METABOLIC_PROCESS.xls"
[1] "./GO_AMINO_ACID_IMPORT.xls"
[1] "./GO_AMINO_ACID_TRANSMEMBRANE_TRANSPORT.xls"
[1] "./GO_AMINO_ACID_TRANSPORT.xls"
[1] "./GO_AMINOGLYCAN_BIOSYNTHETIC_PROCESS.xls"
[1] "./GO_ANGIOGENESIS.xls"
[1] "./GO_ANION_TRANSPORT.xls"
[1] "./GO_ANTIGEN_PROCESSING_AND_PRESENTATION.xls"
[1] "./GO_ANTIGEN_PROCESSING_AND_PRESENTATION_OF_ENDOGENOUS_ANTIGEN.xls"
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file './GO_ANTIGEN_PROCESSING_AND_PRESENTATION_OF_ENDOGENOUS_ANTIGEN.xls': No such file or directory
(note: the files' extension is .xls, but really they are .txt files)
It propmts this message:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file './GO_ANTIGEN_PROCESSING_AND_PRESENTATION.txt': No such file or directory
Also running only q <- read.table(i, header = TRUE, sep = "\t", quote = NULL) appears this error message.
I think I'm in the correct folder (because print(i) works good), I've also changed full.names option and set list.files as a variable out the loop... but nothins seems to work. Please, if anybody has an idea it'll be welcome!
I've tried it on randomly generated files and it works. You probably do not need to cd into the directory with the data, just give the list.files a dir argument with the path to your data directory.
GOfls <- list.files("indata", pattern = "^GO_.*\\.txt", full.names = TRUE)
head(GOfls)
[1] "indata/GO_amswylfbgp.txt" "indata/GO_amswylfbgptxt" "indata/GO_apqqqktvir.txt"
[4] "indata/GO_arwudmbzsr.txt" "indata/GO_autljyljgn.txt" "indata/GO_beeqcmnayk.txt"
# lapply -> do.call for reading and binding the data is better approach
gene_data <- do.call('cbind', lapply(GOfls, function(path) read.delim(path)[,2]))
# have a look at the data
dim(gene_data)
[1] 100 100
I have tried to reproduce your problem this way (it's optional text):
dir.create("indata")
fls <- lapply(1:100, function(i) data.frame(matrix(rnorm(1000), ncol = 10)))
names(fls) <- replicate(100, paste0("./indata/", "GO_",
paste0(sample(letters, 10, replace = T),
collapse = ""), ".txt"
)
)
lapply(names(fls), function(x) write.table(fls[[x]], x, quote = F, sep = "\t"))
head(dir("indata"))
[1] "GO_acebruujkw.pdf" "GO_amswylfbgp.txt" "GO_amswylfbgptxt" "GO_apqqqktvir.txt"
[5] "GO_arwudmbzsr.txt" "GO_autljyljgn.txt"
# I have added some renamed .txt files (.pdf, .tiff, .gel) to the indata
rm(list = ls())
That's solved! It's a bit strange but copying the folder of interest into the desktop the code seems to work again.
A mate and I saw that hard disk's activity was collapsed, so we thought that maybe there could be a problem in the process of reading... so copying the folder was the (simple) solution!
Nevertheless, if anybody has an idea that explains this strange situation I'm sure it'll be useful! Thanks a lot!
EDIT
I've done some tests and maybe the problem is the name of the folder path, which it'd too long and crashes the loop.
I think it's because you're searching for .xls files, but then trying to open it at as a .txt file
In excel try saving the files as comma or tab delimited text files.
If you want to open excel files directly they have a few packages that can do that. Try readxl.
I have a bit of code used successfully before. It should import all files in a given directory into a single dataset. I have a new folder of data, and now I can't get it to work. The individual files will read in. List.files will also show all files in the folder. So I'm at a loss.
list.files('../data/')
[1] "B101-2.txt" "B101.txt" "B116.txt" "B6.txt" "B65.txt" "B67-2.txt" "B67.txt"
[8] "B70.txt" "B71-2.txt" "B71.txt" "B95-2.txt" "B95.txt" "B96-2.txt" "B96.txt"
[15] "B98-2.txt" "B98.txt" "B99-2.txt" "B99.txt"
a = ldply(
.data = list.files(
path = '../data/'
)
, .fun = function(x){
from_header = scan(x,n = 1,skip=1,quiet=T)
to_return = read.table(
file = x
, skip = 20
, sep = '\t'
, fill = TRUE
)
to_return$condition = from_header[1]
return(to_return)
}
, .progress = 'text'
)
Error in file(file, "r") : cannot open the connection
In addition: Warning message:
In file(file, "r") :
cannot open file 'B101-2.txt': No such file or directory
Specifying the full path name can be done in list.files directly.
list.files(path = '../data', full.names = TRUE)
Note the omission of / in the path specification. If left in, the files would be listed as ..data//B101-2.txt, which would fail.
TEST Simulating a file structure you note in Tim Biegeleisen's answer:
library(plyr)
dir.create("analysis")
dir.create("data")
write.table(matrix(c(1:57,1:6), ncol=3, byrow=T), file="data/test1.txt", sep="\t", row.names=F, quote=F)
write.table(matrix(c(2:58,7:12), ncol=3, byrow=T), file="data/test2.txt", sep="\t", row.names=F, quote=F)
write.table(matrix(c(3:59,13:18), ncol=3, byrow=T), file="data/test3.txt", sep="\t", row.names=F, quote=F)
We now run your code from within the analysis folder.
setwd("analysis")
a = ldply(
.data = list.files(path = '../data', full.names = TRUE)
, .fun = function(x){
from_header = scan(x,n = 1,skip=1,quiet=T)
to_return = read.table(file = x, skip = 20, sep = '\t', fill = TRUE)
to_return$condition = from_header[1]
return(to_return)
}
, .progress = 'text'
)
The code reads in all three files and outputs lines 21-22 for each.
a
V1 V2 V3 condition
1 1 2 3 1
2 4 5 6 1
3 7 8 9 2
4 10 11 12 2
5 13 14 15 3
6 16 17 18 3
list.files('../data/') is showing you output like B101-2.txt etc. for the files, but from your very call to list.files() you can see that the relative path is ../data/. So if you were to try to do, for example, read.csv(file="B101-2.txt") it would fail. Instead, the call should be read.csv(file="../data/B101-2.txt"). The solution to your problem is to use the full relative path necessary to address the files.
Use this as the first argument to ldply():
.data = paste0("../data/", list.files(path = '../data/'))
The key thing to take away from this is that list.files() returns a list of filenames (with extensions), not the full or relative path to those files.
I am having a folder where I have a lot of csv files in it.
Can I read all of them, for example as zoo object without knowing the file name?
UPDATE
I tried that:
files <- list.files( "C://Users//ramid//Desktop//Files//" );
(na.omit(files))
for( i in files ) {
filePath <- gsub(" ","", paste("C://Users//ramid//Desktop//Files//",files[i],".csv"), fixed=TRUE)
cat(filePath)
df <- read.csv(gsub(" ","", filePath, fixed=TRUE), header = TRUE, sep = ";",stringsAsFactors=FALSE)
}
However I am getting an error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C://Users//ramid//Desktop//Files//NA.csv': No such file or directory
I do not have any NA in my files list.
I'd use a combination of list.files and lapply:
list_of_files = list.files('.', pattern = '*csv', full.names = TRUE)
list_of_csv_contents = lapply(list_of_files, read.csv)
list_of_zoo = lapply(list_of_csv_contents, zoo)
Or wrap both the read.csv and zoo in one step:
read_into_zoo = function(path) {
contents = read.csv(path)
zoo_contents = zoo(contents)
return(zoo_contents)
}
list_of_zoo = lapply(list_of_files, read_into_zoo)
This strategy of storing things in lists/arrays/vectors/matrices and using apply style looping is a strategy that works very well in R.
I have a simple for loop to write the past 100 tweets of a few usernames to .csv files:
library(twitteR)
mclist <- read.table('usernames.txt')
for (mc in mclist)
{
tweets <- userTimeline(mc, n = 100)
df <- do.call("rbind", lapply(tweets, as.data.frame))
write.csv(df, file=paste("Desktop/", mc, ".csv", sep = ""), row.names = F)
}
I mostly followed what I've read on StackOverflow but I continue to get this error message:
Error in file(file, ifelse(append, "a", "w")) :
invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :
the condition has length > 1 and only the first element will be used
Where did I go wrong?
I just cleaned up the code a bit, and everything started working.
Step 1: Let's set the working directory and load the 'twitteR' package.
library(twitteR)
setwd("C:/Users/Dinre/Desktop") # Replace with your desired directory
Step 2: First, we need to load a list of user names from a flat text file. I'm assuming that each line in the text file has one username, like so:
[contents of usernames.txt]
edclef
notch
dkanaga
Let's load it using the 'scan' function to read each line into an array:
mclist <- scan("usernames.txt", what="", sep="\n")
Step 3: We'll loop through the usernames, just like you did before, but we're not going to refer to the directory, since we're going to use the same directory for output as input. The original code had a syntax error in attempting to referring to the desktop directory, and we're just going to sidestep that.
for (mc in mclist){
tweets <- userTimeline(mc, n = 100)
df <- do.call("rbind", lapply(tweets, as.data.frame))
write.csv(df, file=paste(mc, ".csv", sep = ""), row.names = F)
}
I end up with three files on the desktop, and all the data seems to be correct.
edclef.csv
notch.csv
dkanaga.csv
Update: If you really want to refer to different directories within your code, use the '.' character to refer to the parent directory. For instance, if your working directory is your Windows user profile, you would refer to the 'Desktop' folder like so:
setwd("C:/Users/Dinre")
...
write.csv(df, file=paste("./Desktop/". mc, ".csv", sep = ""), row.names = F)
There's a convenience function in the package twListToDF which will handle the conversion of the list of tweets to a data.frame.
Since your mclist is a data.frame, you can replace your for by apply
apply( mclist, 1,function(mc){
tweets <- userTimeline(mc, n = 100)
df <- do.call("rbind", lapply(tweets, as.data.frame))
write.csv(df, file=paste("Desktop/", mc, ".csv", sep = ""), ##!! Change Desktop to
## something like Desktop/tweets/
row.names = F)
})
PS :
The userTimeline function will only work if the user requested has a
public timeline, or you have previously registered a OAuth object
using registerTwitterOAuth