Question: converting integer to numeric
0
gravatar for Angel
3.3 years ago by
Angel40
Berlin
Angel40 wrote:

hi

i have a matrix but im facing this error

mycounts <- read.table("newMat.txt", header = T, sep = "\t")
head(mycounts[,1:4])
        IDs AT1G01060 AT1G01170 AT1G01180
1 AT1G01060         0         0         0
2 AT1G01170         0         0         0
3 AT1G01180         0         0         0
4 AT1G01183         0         0         0
5 AT1G01260         0         0         0
6 AT1G01380         0         0         0

list object cannot be coerced to type integer

how i can convert an integer to numeric please?

thank you

software error • 3.0k views
ADD COMMENTlink modified 3.3 years ago • written 3.3 years ago by Angel40
Answer: converting integer to numeric
2
gravatar for Nicolas Delhomme
3.3 years ago by
Sweden
Nicolas Delhomme320 wrote:
Hej Fereshteh Izadi! At the moment, your mycounts object is a data.frame mixing both characters (the first column) and integer/numeric values (your second to fourth columns). Depending on what you want to do with your object, e.g. coerce it as a matrix using as.matrix(mycounts) you will get the error you see, because of the different datatypes in your data.frame. As you have not included all the commands you ran, including the one generating the error in your post, it is hard to guess what might have triggered it. To get a matrix out of your example, do this instead: mycounts <- read.table("newMat.txt", header = T, sep = "\t",row.names=1) mymat <- as.matrix(mycounts) The only difference to your code is the "row.names" argument that will use the first column as row.names, so you will get something like: AT1G01060 AT1G01170 AT1G01180 AT1G01060 0 0 0 AT1G01170 0 0 0 ... And since your mycounts object will have only one type, you can coerce it as a matrix. Check ?read.table for more details. HTH, Nico --------------------------------------------------------------- Nicolas Delhomme, PhD Acting Manager UPSC bioinformatics core facility Umeå Plant Science Center, Swedish University for Agricultural Sciences (SLU) and Umeå University Tel: +46 90 786 5478 Email: nicolas.delhomme@umu.se SLU - Umeå universitet Umeå S-901 87 Sweden --------------------------------------------------------------- > On 30 Dec 2015, at 22:39, Fereshteh.Izadi@mpikg.mpg.de [bioc] <noreply@bioconductor.org> wrote: > > Activity on a post you are following on support.bioconductor.org > User Fereshteh.Izadi@mpikg.mpg.de wrote Question: converting integer to numeric: > > > hi > > i have a matrix but im facing this error > > mycounts <- read.table("newMat.txt", header = T, sep = "\t") > head(mycounts[,1:4]) > IDs AT1G01060 AT1G01170 AT1G01180 > 1 AT1G01060 0 0 0 > 2 AT1G01170 0 0 0 > 3 AT1G01180 0 0 0 > 4 AT1G01183 0 0 0 > 5 AT1G01260 0 0 0 > 6 AT1G01380 0 0 0 > > list object cannot be coerced to type integer > > how i can convert an integer to numeric please? > > thank you > > > Post tags: software error > > You may reply via email or visit converting integer to numeric >
ADD COMMENTlink written 3.3 years ago by Nicolas Delhomme320
Answer: converting integer to numeric
2
gravatar for arfranco
3.3 years ago by
arfranco130
European Union
arfranco130 wrote:

Add that an integer is also a numeric value, just lacking decimals

You need to add as row.names the values contained in the first column, and then, erase the first column

You have code showing the way of do it

ADD COMMENTlink written 3.3 years ago by arfranco130
Answer: converting integer to numeric
2
gravatar for Hervé Pagès
3.3 years ago by
Hervé Pagès ♦♦ 13k
United States
Hervé Pagès ♦♦ 13k wrote:
  1. This is not a Bioconductor question. It's about basic manipulations in R. This kind of questions is better asked on the R-help mailing list (https://stat.ethz.ch/mailman/listinfo/r-help).
  2. You're showing us code but you omitted the most important part which is the command that actually caused the error.
  3. The question you ask ("how i can convert an integer to numeric") doesn't seem directly related to the code you show. To convert an integer vector to a numeric vector, just call as.numeric() on it. However, your mycounts object is a data.frame, not an integer vector.
  4. It could be that you got the error message you show us by calling as.integer() on the mycounts data.frame. A data.frame is a special kind of list hence the error message you got. Note that calling as.integer() on a list only works in the very particular case where all the list elements have a length <= 1.
  5. Calling as.matrix() on your mycounts data.frame should work except that it will turn it into a character matrix, not an integer matrix. As Nico said, that's because one of the columns (the 1st one) in your data.frame is of type character (or factor). To get an integer matrix, you first need to get rid of this column. You can do this either by subsetting mycounts with as.matrix(mycounts[ , -1]), or by tweaking your call to read.table() as showed by Nico.

Cheers,

H.

ADD COMMENTlink modified 3.3 years ago • written 3.3 years ago by Hervé Pagès ♦♦ 13k
Answer: converting integer to numeric
0
gravatar for Angel
3.3 years ago by
Angel40
Berlin
Angel40 wrote:

thank you all

ADD COMMENTlink written 3.3 years ago by Angel40
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 106 users visited in the last hour