converting integer to numeric
4
0
Entering edit mode
Angel ▴ 40
@angel-7981
Last seen 7.7 years ago
Berlin

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 • 10k views
ADD COMMENT
2
Entering edit mode
@nicolas-delhomme-6252
Last seen 6.1 years ago
Sweden
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 COMMENT
2
Entering edit mode
arfranco ▴ 130
@arfranco-8341
Last seen 7 months ago
European Union

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 COMMENT
2
Entering edit mode
@herve-pages-1542
Last seen 3 hours ago
Seattle, WA, United States
  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 COMMENT
0
Entering edit mode
Angel ▴ 40
@angel-7981
Last seen 7.7 years ago
Berlin

thank you all

ADD COMMENT

Login before adding your answer.

Traffic: 605 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6