No row names when reading a file in R

Jogianni picture Jogianni · Oct 5, 2016 · Viewed 14.2k times · Source

I have a ".txt" file that does not contain row names, but when I use read.table with row.names = NULL it still takes in the first 2 rows as row names.

test <- read.table('C:\\somefolder\\myfile.txt', header = FALSE, row.names = NULL)
head(test)

#                         V1  V2  V3
#1  1002345017,1598773715,56 ,23 ,29
#2  2000310429,1134645573,68 ,12 ,36
#3  3003044126,1403951625,147 ,53 ,28
#4  4045601426,1003975400,38 ,18  ,0
#5  4500450126,1016051119,30 ,15  ,0
#6  6049000126,1013902600,29 ,19  ,2

I get the same results without using row.names specification as well.

Answer

Bulat picture Bulat · Oct 5, 2016

You are missing sep parameter:

res <- read.table(text = "1002345017,1598773715,56 ,23 ,29
2000310429,1134645573,68 ,12 ,36
3003044126,1403951625,147 ,53 ,28
4045601426,1003975400,38 ,18  ,0
4500450126,1016051119,30 ,15  ,0
6049000126,1013902600,29 ,19  ,2", header = FALSE, row.names = NULL, sep= ",")

As you source has a mix of spaces and commas you get it right half of the time.