Reputation: 3082
I want to read a matrix (all values, no null or empty column) from a tab-separated text file of integers and name the columns automatically (based on the titles in the first line):
a b c
9 2 3
2 9 6
3 2 4
5 3 3
I have tried read.csv(), read.table() and scan() methods and read the file, but I want something that:
1- Automatically identifies the column names (no need to mention the names one by one).
2- I would be able to treat them as matrix of integers; run rcorr(data) and quantile(data$a, 0.9) instead of rcorr(as.matrix(data)) and quantile(as.matrix(data$a), 0.9) any time.
Any ideas on the simplest (yet efficient) way?
Upvotes: 0
Views: 269
Reputation: 3082
data <-- as.matrix(read.table("c:\\temp\\inFile.tsv", header=TRUE))
Note that I got the following error when there was special characters (#) in the header line:
Error in read.table("..."), : more columns than column names
So there shouldn't be special characters in the header line. Also it automatically detects the separator ("\t").
Upvotes: 0
Reputation: 3364
How about read.table
?
read.table(text="a b c
9 2 3
2 9 6
3 2 4
5 3 3", header=TRUE)
> a b c
1 9 2 3
2 2 9 6
3 3 2 4
4 5 3 3
it also has options to input file, declare the separator, etc.. see help(read.table)
Upvotes: 1