user12
user12

Reputation: 91

Row matrix is acting weird when changing to data frame in R

I have a row matrix made from one row

    chr       start        end     clusterSize    strand   isCluster 
   "chr1"      "25"        "40"        "15"        "."     "TRUE"

When I change this into a dataframe using as.data.frame in R, I get the following result:

              res
chr          chr1
start          25
end            40
clusterSize    15
sites       2,1,2
strand          .
isCluster    TRUE

I expected to see the row matrix become one row as a data frame format.I can't understand this behavior, when I have a matrix made from more than one row it is converted perfectly to a data frame. Does anyone have an idea about this?

Upvotes: 1

Views: 45

Answers (1)

Onyambu
Onyambu

Reputation: 79208

You have a vector and not a row matrix:

a = c(chr ="chr1", start="25", end="40", clusterSize="15", strand=".", isCluster ="TRUE")

This is how a row matrix looks like:

> t(a)
     chr    start end  clusterSize strand isCluster
[1,] "chr1" "25"  "40" "15"        "."    "TRUE" 

but what you have is this

> a
        chr       start         end clusterSize      strand   isCluster 
     "chr1"        "25"        "40"        "15"         "."      "TRUE" 

so in order to get what you want, just do:

data.frame(t(as.matrix(a)))
   chr start end clusterSize strand isCluster
1 chr1    25  40          15      .      TRUE

or you can do directly:

data.frame(t(a))
   chr start end clusterSize strand isCluster
1 chr1    25  40          15      .      TRUE

Upvotes: 1

Related Questions