This is my data as "*.txt"
ID LBI RTI WDI FLA PSF FSF ZDF1 PROZD
ar ?,35.30,2.60, ?,42.4,24.2,47.1,69
arn 1.23,27.00,3.59,122, 0.0,40.0,40.0,30
be 1.24,26.50,2.90,121,16.0,20.7,29.7,72
bi1 1.07,29.10,3.10,114,44.0, 2.6,26.3,68
bi2 1.08,43.70,2.40,105,32.6, 5.8,10.7,42
bie 1.39,29.50,2.78,126,14.0, 0.0,50.0,78
bn 1.31,26.30,2.10,119,15.7,15.7,30.4,72
bo 1.27,27.60,3.50,116,16.8,23.0,35.2,69
by 1.11,32.60,2.90,113,15.8,15.8,15.0,57
Then, it failed when I command
r2 <- read.table('StoneFlakes.txt',header=TRUE,na.strings='?')
The error ;
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 9 elements
Anyone can help me?
Use
text <- readLines('StoneFlakes.txt')
text <- gsub(",", " ", text)
read.table(textConnection(text), header=TRUE, na.strings='?')
#insted of using textConnection you can also use
read.table(text=text, header=TRUE, na.strings='?')
Related
I am trying to import into R a text file, saved with TextWrangler as Unicode (UTF-8) and Unix(LF)
Here is the code I am using:
scan("Testi/PIRANDELLOsigira.txt", fileEncoding='UTF-8', what=character(), sep='\n')
I got the following warning:
Read 6 items
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
invalid input found on input connection 'Testi/PIRANDELLOsigira.txt'
and a vector that stops at the first accented character.
first change your locale from Italy to English
Sys.setlocale(category="LC_ALL", locale = "English_United States.1252")
Then you can read the data with italian encoding
df_ch <- read.table("test.utf8",
sep=",",
header=TRUE,
encoding=" Italian",
)
if you want to only read the data with UTF-8 encoding
you can simply use the following
yourdf <- read.table(" path to your data.utf8",
sep=",",
header=TRUE,
encoding="UTF-8",
)
i am appending 100 files in a folder with a delimiter "|". Below is the code used. I am getting an error whichi am not able to debug,
file_list <- list.files()
dataset <- ldply(file_list, read.table, header=TRUE, sep="|")
ERROR - Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 284 did not have 12 elements
Please help me on this who have experience around this.
I am trying to read.table data in clipboard to get around DRM-ed file. But I could not understand why it does not work as below. I added " comment.char="" " and it didn't help.
t1 = read.table( "clipboard", header=T, sep="\t" )
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 23938 did not have 23 elements
t1_0 = readLines( "clipboard" )
t2 = sapply( t1_0, function(x) strsplit(x, "\t") )
table( sapply(t2, length ) )
23
406799
I am trying to read a huge file (2GB in size) with this:
data1<-read.table("file1.txt", sep=",",header=F)
I get this error:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 513836 did not have 8 elements
Is there a way to skip lines where missing data or replace it with NA values?
This error is most commonly fixed by adding fill = TRUE to your read.table() call. In your case, it would be the following
data1 <- read.table("file1.txt", sep = ",", fill = TRUE)
Additionally, header = FALSE is the default setting for the header argument in read.table() and therefore unnecessary in your code.
I'm trying to read a text file into R using the below code:
d = read.table("test_data.txt")
It returned the following error message:
"Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 2 did not have 119 elements"
I tried this:
read.table("man_cohort9_check.txt", header=T, sep="\t")
but it gave this error:
"Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 43 did not have 116 elements"
I don't understand what's going wrong??
It's because your file has rows with different number of column. To start investigate you can run:
d = read.table("test_data.txt", fill=TRUE, header=TRUE, sep="\t")
The usual cause of this are unmatched quotes and or lurking octothorpes ("#"). I would investigate these by seeing which of these produces the most regular table:
table( countfields("test_data.txt", quote="", comment.char="") )
table( countfields("test_data.txt", quote="") )
table( countfields("test_data.txt", comment.char="") )