r - more columns than column name on txt file -


i have txt file , read in r command:

read.table("c:/users/vatlidak/my documents/documents/untitled.txt", header=true) 

r returns me following command:

"more columns column name"

txt file:

height shoesize gender location 1   181 44   male      city center 4   170 43   female    city center 5   172 43   female    city center 13  175 42   male      out of city 14  181 44   male      out of city 15  180 43   male      out of city 16  177 43   female    out of city 17  133 41   male      out of city 

if myfile contains path/filename replace each of first 4 stretches of whitespace on every line comma , re-read using read.csv. no packages used.

l <- readlines(myfile) ## for(i in 1:4) l <- sub("\\s+", ",", l) df <- read.csv(text = l) 

giving:

> df    height shoesize gender    location 1     181       44   male city center 4     170       43 female city center 5     172       43 female city center 13    175       42   male out of city 14    181       44   male out of city 15    180       43   male out of city 16    177       43 female out of city 17    133       41   male out of city 

note: purposes of testing can use in place of line marked ## above. (note can introduce spaces @ beginnings of lines remove them.)

lines <- " height shoesize gender location 1   181 44   male      city center 4   170 43   female    city center 5   172 43   female    city center 13  175 42   male      out of city 14  181 44   male      out of city 15  180 43   male      out of city 16  177 43   female    out of city 17  133 41   male      out of city"  l <- readlines(textconnection(lines)) l[-1] <- sub("^\\s+", "", l[-1]) 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -