Domanda

I'm new to R and I'm having trouble understanding what all the parameters in read.table() do. I have a text file with a header, and roughly 50 rows. Columns are separated by tabs. I did the following.

data <- read.table("/Accounts/changy/Desktop/GreekProject/outputWithoutQuantity.txt",header=TRUE,sep="\t", quote = "")

Now, I want to create a matrix, but omit the header (first row). Also, read.table generates numbered rows for each of my already numbered rows, and I don't want my matrix to be numbered at all, so I would need to omit the first two columns as well. Can anyone point me to the right direction? I know

matrixData <- as.data.frame.matrix(data)

does it, but it doesn't format the rows and columns as I would like. Thanks for any help in advance, from a complete beginner to R!

here's a snapshot of my data set, upon request.

http://postimg.org/image/b7h97rd7d/

È stato utile?

Soluzione

Here is a suggestion. Does it work as you wish?

 ## Test dataset
 data = data.frame(col1 = c(1,2,3,4), col2 = c(0,0, 1, 0), col3 = c(1,0,0,1))
 write.table(data, "data.txt", row.names = FALSE)
 data.txt = read.table("data.txt", header = TRUE)

 data.mat = as.matrix(data.txt[,-1])  # gets rid of the leftmost column(as you requested)
 dimnames(data.mat) <-list(rep("", dim(data.mat)[1]), rep("", dim(data.mat)[2]))

This would be the output

0 1
0 0
1 0
0 1

instead of:

1 0 1
2 0 0
3 1 0
4 0 1
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top