Reputation: 2897
I have a text file name "A.txt" . In there , I have the following data:
1 3 4
2 3 4
5 6 7
I want to read it and save as A vector in R software . How can I do that ?
Update :
I have tried by the following code :
R> dat <- as.numeric(readLines('D:/Simplex/SimplexInitialTheoryWithRsoftware/src/A.txt'))
R> dat.matrix <- matrix(dat, nrow=??)
But I have the following error .
Eror: unexpected ')' in "dat.matrix <- matrix(dat, nrow=??)"
I am very new in this software . Please help me
Upvotes: 2
Views: 40523
Reputation: 161
The post is a bit old, but the answers are valid until today.
aa <- as.matrix(read.table("A.txt", sep=" "))
Upvotes: 3
Reputation: 868
Do it like this Define matrix with column header:
Col1 Col2 Col3
1 3 4
2 3 4
5 6 7
d<-read.table("/home/shad/Desktop/A.txt",header=TRUE,sep=" ")
then access it like:
d[1,1]
d[1,2]
For numeric conversion see documentation Hope it helps
Upvotes: 3
Reputation: 23758
From the way you've started out it looks like you're searching for the scan
function.
mat <- scan('A.txt')
mat <- matrix(mat, ncol = 3, byrow = TRUE)
Upvotes: 12