dataset - Error in reading in data set in R -


when reading in data set in r follows:

dataset.df <- read.table("c:\\dataset.txt", header=t) 

i following error message:

error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :    line 1 did not have 145 elements 

what mean , can tell me how fix it?

this error pretty self-explanatory. there seem data missing in first line of data file (or second line, case may since you're using header = true).

here's mini example:

## create small dataset play cat("v1 v2\nfirst 1 2\nsecond 2\nthird 3 8\n", file="test.txt") 

r automatically detects should expect rownames plus 2 columns (3 elements), doesn't find 3 elements on line 2, error:

read.table("test.txt", header = true) # error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :  #   line 2 did not have 3 elements 

look @ data file , see if there indeed problem:

cat(readlines("test.txt"), sep = "\n") # v1 v2 # first 1 2 # second 2 # third 3 8 

manual correction might needed, or can assume value first value in "second" row line should in first column, , other values should na. if case, fill = true enough solve problem.

read.table("test.txt", header = true, fill = true) #        v1 v2 # first   1  2 # second  2 na # third   3  8 

r smart enough figure out how many elements needs if rownames missing:

cat("v1 v2\n1\n2 5\n3 8\n", file="test2.txt") cat(readlines("test2.txt"), sep = "\n") # v1 v2 # 1 # 2 5 # 3 8 read.table("test2.txt", header = true) # error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :  #   line 1 did not have 2 elements read.table("test2.txt", header = true, fill = true) #   v1 v2 # 1  1 na # 2  2  5 # 3  3  8 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -