Import text file as single character string
Here's a variant of the solution from @JoshuaUlrich that uses the correct size instead of a hard-coded size:
fileName <- 'foo.txt'
readChar(fileName, file.info(fileName)$size)
Note that readChar allocates space for the number of bytes you specify, so readChar(fileName, .Machine$integer.max)
does not work well...
how to import a text file into R as a character vector
Try strsplit
after removing the space with gsub
A <- strsplit(gsub('\\s+', '', lines),'')[[1]]
A
#[1] "H" "e" "l" "l" "o" "m" "y" "n" "a" "m" "e" "i" "s" "F" "a" "g" "u" "i" "C"
#[20] "u" "r" "t" "a" "i" "n"
Or
library(stringi)
stri_extract_all_regex(lines, '\\w')[[1]]
#[1] "H" "e" "l" "l" "o" "m" "y" "n" "a" "m" "e" "i" "s" "F" "a" "g" "u" "i" "C"
#[20] "u" "r" "t" "a" "i" "n"
Or if you are using linux, scan
and be piped with awk
scan(pipe("awk 'BEGIN{FS=\"\";OFS=\" \"}{$1=$1}1' file.txt"),
what='', quiet=TRUE)
#[1] "H" "e" "l" "l" "o" "m" "y" "n" "a" "m" "e" "i" "s" "F" "a" "g" "u" "i" "C"
#[20] "u" "r" "t" "a" "i" "n"
data
lines <- readLines('file.txt')
Trouble turning a text file with a character string into a column with one character per row in R
You can use strsplit
. Just be aware that strsplit
returns a list and hence you have to convert it into a character vector
string <- "jasdklnjabfial"
data.table::data.table(x = unlist(strsplit(string, "")))
#> x
#> 1: j
#> 2: a
#> 3: s
#> 4: d
#> 5: k
#> 6: l
#> 7: n
#> 8: j
#> 9: a
#> 10: b
#> 11: f
#> 12: i
#> 13: a
#> 14: l
Read text file in R and convert it to a character object
By default character columns are converted to factors. You can avoid this by setting as.is=TRUE
argument:
clusters <- read.delim("test", sep="\t", fill=TRUE, header=FALSE, as.is=TRUE)
If you only pass arguments from text file to character vector you could do something like:
x <- readLines("test")
xx <- strsplit(x,split="\t")
xx[[1]] # xx is a list
# [1] "248585_at" "250887_at" "245638_s_at" "AFFX-BioC-5_at"
Importing a single vector from a text file as a variable in R
if your text file has R syntax in it (c(...)
) then
x <- source("myfile.txt")[["value"]]
should work.
How to read a text file into a string variable and strip newlines?
You could use:
with open('data.txt', 'r') as file:
data = file.read().replace('\n', '')
Or if the file content is guaranteed to be one-line
with open('data.txt', 'r') as file:
data = file.read().rstrip()
Related Topics
How to Divide Each Row of a Matrix by Elements of a Vector in R
Multiple Plots in For Loop Ignoring Par
Intelligent Point Label Placement in R
How to Use Reference Variables by Character String in a Formula
Dplyr: Nonstandard Column Names (White Space, Punctuation, Starts With Numbers)
Number of Months Between Two Dates
Reshape Multiple Values At Once
Idiomatic R Code For Partitioning a Vector by an Index and Performing an Operation on That Partition
Why Does X[Y] Join of Data.Tables Not Allow a Full Outer Join, or a Left Join
How to Convert Long to Wide Format With Counts
Extract the First 2 Characters in a String