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="") )
Related
I imported a dataset in R, but once I did that the value I got were only NAs, and all the numeric values from my csv file got "cancelled". I thought that it was a problem with factorisation and I tried to use stringasfactor= F, as well as read.csv(file="habits.csv",header=TRUE,colClasses=c("numeric","numeric","numeric","numeric"))
But for this last code I get this error:
> datagpa <- read.csv(file="habits.csv",header=TRUE,colClasses=c("integer","integer","integer","integer","integer","integer","integer","integer","integer","integer","integer"))
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
scan() expected 'an integer', got '2018/10/192:32:52PMEET;3.5;8;3;50;53;0;0;8;8;10;10'
> getwd()
[1] "/Users/rachelepesce/Desktop/QRM/R"
> datagpa <- read.csv(file="habits.csv",header=TRUE,colClasses=c("integer","integer","integer","integer","integer","integer","integer","integer","integer","integer","integer"))
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
scan() expected 'an integer', got '2018/10/192:32:52PMEET;3.5;8;3;50;53;0;0;8;8;10;10'
> datagpa <- read.csv(file="habits.csv",header=TRUE,colClasses=c("numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
scan() expected 'a real', got '2018/10/192:32:52PMEET;3.5;8;3;50;53;0;0;8;8;10;10'
Does any of you know how to solve it ?
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
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='?')
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.