简体   繁体   中英

Failed to create matrix in R

I read tsv file using:

> dt <- read.table("gene.tsv", fill=TRUE, header=TRUE, quote="", sep="\t")

and then I check my data using >head(dt) and I got this:

 Gene Healthy_A Healthy_B Infected_A Infected_B
1  zwf        79        57         99        100
2 yyaP        99        99         99         99
3 xylB        74        45         78         99
4  hup        77        61         86         99
5  pgi        67        41         66         99
6  lon        98        76         99         99

I set row names using:

> rownames(dt) <- dt$gene

and than make a matrix from my data using:

> dt_matrix <- as.matrix(dt[2:5])

but when I checked the matrix using head(dt_matrix) , my data looks like this:

Healthy_A Healthy_B Infected_A Infected_B
[1,]        79        57         99        100
[2,]        99        99         99         99
[3,]        74        45         78         99
[4,]        77        61         86         99
[5,]        67        41         66         99
[6,]        98        76         99         99

I want my matrix looks like bellow for creating heatmap:

 Gene       Healthy_A Healthy_B Infected_A Infected_B
  zwf        "79"        "57"         "99"        "100"
 yyaP        "99"        "99"         "99"         "99"
 xylB        "74"        "45"         "78"         "99"
  hup        "77"        "61"         "86"         "99"
  pgi        "67"        "41"         "66"         "99"
  lon        "98"        "76"         "99"         "99"

How I can get what I want?

Set rownames after creating the matrix like so

dt <- fread(' Gene Healthy_A Healthy_B Infected_A Infected_B
  zwf        79        57         99        100
 yyaP        99        99         99         99
 xylB        74        45         78         99
  hup        77        61         86         99
  pgi        67        41         66         99
  lon        98        76         99         99')

dt_rownames <- dt$Gene

dt_matrix <- as.matrix(dt[,-1])
rownames(dt_matrix) <- dt_rownames
head(dt_matrix)

     Healthy_A Healthy_B Infected_A Infected_B
zwf         79        57         99        100
yyaP        99        99         99         99
xylB        74        45         78         99
hup         77        61         86         99
pgi         67        41         66         99
lon         98        76         99         99

You actually could do this in one step.

m <- `rownames<-`(as.matrix(dt[-1]), dt[,1])
m
#      Healthy_A Healthy_B Infected_A Infected_B
# zwf         79        57         99        100
# yyaP        99        99         99         99
# xylB        74        45         78         99
# hup         77        61         86         99
# pgi         67        41         66         99
# lon         98        76         99         99

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM