Read Columnwise Matrix in R

554 views Asked by At

I've spended lots of time trying but somehow nothing works - and I guess this is easy for advanced R users. I got an Dataformat where each element occurs linewise. First the label as an String followed by 1000 numeric features, all splited with whitespace:

"label1" 1 0 1 0 0 0 ...
"label2" 0 0 0 0 1 0 ...
"label2" 0 0 1 0 1 0 ...
"label2" 1 1 1 1 0 0 ...
...

The problem I have are the labels when reading the matrix (first row or always first column). I want to apply this matrix to tsne (dimension reduction) but the label cause problems. So I need the matrix without the labels but I'd like to store the labels later, so I can print them with their new dimensions. What I got so far is the following (rather pseudocode):

Data <- read.table("File.txt", header=False, row.names=1)
Labels <- Data[1]    # I somehow need the labels
Data[1] <- NULL # this should remove the first row (labels ?)
tsne = tsne(Data, initial_config = NULL, k = 2, initial_dims = 30,...)# function that reduces dimension

Here I need something that prints the new 2-Dimensional matrix togehter with each label, such as label[x] + tsne[xDimension] + tsne[yDimension]

I hope someone of you could help me, thanks in advance and best regards.

3

There are 3 answers

0
Oleg Sklyar On
data <- read.table("File.txt", header=False, row.names=1)
dlabels <- data[,1]

res = tsne(as.matrix(data[,-1]), ...)

What you do with labels afterwards is completely up to you as it is not clear from your question what you want to achive with those

0
Sven Hohenstein On

Try the following command:

cbind(Labels, tsne)
0
Richie Cotton On

Read the data in using read.table, as before.

data_as_data.frame <- read.table(
  text = '"label1" 1 0 1 0 0 0 
"label2" 0 0 0 0 1 0 
"label2" 0 0 1 0 1 0 
"label2" 1 1 1 1 0 0'
)

Create a matrix from all but the first column.

data_as_a_matrix <- as.matrix(data_as_data.frame[, -1])

Use that first column as rownames in the matrix.

rownames(data_as_a_matrix) <- data_as_data.frame[, 1]
data_as_a_matrix
##        V2 V3 V4 V5 V6 V7
## label1  1  0  1  0  0  0
## label2  0  0  0  0  1  0
## label2  0  0  1  0  1  0
## label2  1  1  1  1  0  0