Re: [R] convert matrix

2016-10-30 Thread Robert Baer



On 10/29/2016 11:19 AM, Elham - via R-help wrote:

Dear Madam / Sir,I saw this function for "Convert to matrix as it is that you wanted" 
> test2<-as.matrix(test1)

colnames(test2)<-NULL
genelist<-c("Fkh2","Swi5","Sic1")
rownames(test2)<-genelist
test2
#  [,1]  [,2]  [,3]
#Fkh2 0.141 0.242 0.342
#Swi5 0.224 0.342 0.334
#Sic1 0.652 0.682 0.182


what is function for large data?my data and genelist are 28031 rows,how can I convert? clear that I can not 
write 28031 genes like genelist<-c("Fkh2","Swi5","Sic1")
You can assign the names of your genes by any method convenient. The 
point is not necessarily to use the c() function.   If you have them in 
a .csv file somewhere, simply read them in to create the genelist vector.


If you do not know how to to this you should probably read, "An 
Introduction to R", 
https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf


?read.csv typed at the command prompt will give you some specifics

In the end, you will do something like:
genelist  <- read.csv("AfileOfMyGenes.csv", header = TRUE)

# assume your gene names are in the first column which is titled genename
genelist <- genelist$genename





Your attention would be really appreciated.Best Regards,Elham Dalalbshi Esfahani

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix

2016-10-30 Thread Jim Lemon
Hi Elham,
As you have asked this question a large number of times in quite a few
places, and have received reasonable answers, I assume that you
already know that the gene names and associated values are in another
format. What you probably want to do is to convert the first column of
the data that you have imported into rownames:

test2a<-(test2[,-1])
rownames(test2a)<-test2[,1]

Why do I guess this? Because the values in a matrix must all be of the
same data type and you probably have a data frame in which the first
column is the names. You want the numeric values in the subsequent
columns to become a matrix. Perhaps I have guessed correctly. Perhaps
not.

Jim


On Sun, Oct 30, 2016 at 3:01 AM, Elham Dallalbashi
 wrote:
>> Dear Madam / Sir,
>> I saw this function for "*Convert to matrix as it is that you wanted" *
>>
>> >* test2<-as.matrix(test1)
>> *>* colnames(test2)<-NULL
>> *>* genelist<-c("Fkh2","Swi5","Sic1")
>> *>* rownames(test2)<-genelist
>> *>* test2
>> *>* #  [,1]  [,2]  [,3]
>> *>* #Fkh2 0.141 0.242 0.342
>> *>* #Swi5 0.224 0.342 0.334
>> *>* #Sic1 0.652 0.682 0.182*
>>
>>
>>
>>
>> what is function for large data?my data and genelist are 28031 rows,how
>> can I convert? clear that I can not write 28031 genes like
>> *genelist<-c("Fkh2","Swi5","Sic1")*
>>
>>
>>
>> Your attention would be really appreciated.
>>
>> Best Regards,
>>
>> Elham Dalalbshi Esfahani
>>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix

2016-10-29 Thread Elham Dallalbashi
> Dear Madam / Sir,
> I saw this function for "*Convert to matrix as it is that you wanted" *
>
> >* test2<-as.matrix(test1)
> *>* colnames(test2)<-NULL
> *>* genelist<-c("Fkh2","Swi5","Sic1")
> *>* rownames(test2)<-genelist
> *>* test2
> *>* #  [,1]  [,2]  [,3]
> *>* #Fkh2 0.141 0.242 0.342
> *>* #Swi5 0.224 0.342 0.334
> *>* #Sic1 0.652 0.682 0.182*
>
>
>
>
> what is function for large data?my data and genelist are 28031 rows,how
> can I convert? clear that I can not write 28031 genes like
> *genelist<-c("Fkh2","Swi5","Sic1")*
>
>
>
> Your attention would be really appreciated.
>
> Best Regards,
>
> Elham Dalalbshi Esfahani
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix to numeric

2011-08-03 Thread Duncan Murdoch

On 03/08/2011 3:04 PM, Jeffrey Joh wrote:

I have a matrix that looks like this:


structure(c(0.0376673981759913, 0.111066500741386, 1, 1103,
18, OPEN, DEPR, 0.0404073656092023, 0.115186044704599,
1, 719, 18, OPEN, DEPR, 0.0665342096693433, 0.197570061769498,
1, 1103, 18, OPEN, DEPR, 0.119287147905722, 0.356427096010845,
1, 1103, 18, OPEN, DEPR), .Dim = c(7L, 4L), .Dimnames = list(
 c(Sn, SlnC, housenum, date, hour, flue, pressurization
 ), c(10019.BLO, 1002.BLO, 10020.BLO, 10021.BLO)))



How do I convert rows 1-5 to numeric?  I tried mode()- numeric but that 
doesn't change anything.


Every entry in a matrix has the same type, so you can't change just 
those rows other than by extracting them into a separate matrix and 
changing that.


Duncan Murdoch




I also tried converting this to a table then converting to numeric, but I got: 
(list) object cannot be coerced to type 'double'



Jeff
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix to numeric

2011-08-03 Thread Ken
How about
Matrix[1:5,]=as.numeric(Matrix[1:5,])
-Ken Hutchison

On Aug 3, 2554 BE, at 3:04 PM, Jeffrey Joh johjeff...@hotmail.com wrote:

 
 I have a matrix that looks like this:
 
 
 structure(c(0.0376673981759913, 0.111066500741386, 1, 1103, 
 18, OPEN, DEPR, 0.0404073656092023, 0.115186044704599, 
 1, 719, 18, OPEN, DEPR, 0.0665342096693433, 0.197570061769498, 
 1, 1103, 18, OPEN, DEPR, 0.119287147905722, 0.356427096010845, 
 1, 1103, 18, OPEN, DEPR), .Dim = c(7L, 4L), .Dimnames = list(
c(Sn, SlnC, housenum, date, hour, flue, pressurization
), c(10019.BLO, 1002.BLO, 10020.BLO, 10021.BLO)))
 
 
 
 How do I convert rows 1-5 to numeric?  I tried mode() - numeric but that 
 doesn't change anything.
 
 
 
 I also tried converting this to a table then converting to numeric, but I 
 got: (list) object cannot be coerced to type 'double'
 
 
 
 Jeff 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix to numeric

2011-08-03 Thread Sarah Goslee
Hi Jeffrey,

On Wed, Aug 3, 2011 at 3:04 PM, Jeffrey Joh johjeff...@hotmail.com wrote:

 I have a matrix that looks like this:


 structure(c(0.0376673981759913, 0.111066500741386, 1, 1103,
 18, OPEN, DEPR, 0.0404073656092023, 0.115186044704599,
 1, 719, 18, OPEN, DEPR, 0.0665342096693433, 0.197570061769498,
 1, 1103, 18, OPEN, DEPR, 0.119287147905722, 0.356427096010845,
 1, 1103, 18, OPEN, DEPR), .Dim = c(7L, 4L), .Dimnames = list(
    c(Sn, SlnC, housenum, date, hour, flue, pressurization
    ), c(10019.BLO, 1002.BLO, 10020.BLO, 10021.BLO)))

Thank you for providing a small working example.

 How do I convert rows 1-5 to numeric?  I tried mode() - numeric but that 
 doesn't change anything.

Two things are going on here. First, a matrix can only contain one kind of data.
For this example, since there are strings the whole thing has to be character.
A data frame is intended to hold different kinds of data, but each column has
to be a single type. So if you want those values to be numeric instead of
character, you'll need to transpose your matrix and convert it to a data frame.

tempdata - structure(c(0.0376673981759913, 0.111066500741386, 1, 1103,
18, OPEN, DEPR, 0.0404073656092023, 0.115186044704599,
1, 719, 18, OPEN, DEPR, 0.0665342096693433, 0.197570061769498,
1, 1103, 18, OPEN, DEPR, 0.119287147905722, 0.356427096010845,
1, 1103, 18, OPEN, DEPR), .Dim = c(7L, 4L), .Dimnames = list(
   c(Sn, SlnC, housenum, date, hour, flue, pressurization
   ), c(10019.BLO, 1002.BLO, 10020.BLO, 10021.BLO)))

tempdata - data.frame(t(tempdata), stringsAsFactors=FALSE)

Once you have the right kind of object, you can convert the five columns
of interest to numeric. This needs to be done a column at a time, I think:
tempdata[, 1:5] - apply(tempdata[,1:5], 2, as.numeric)

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix to numeric

2011-08-03 Thread Nordlund, Dan (DSHS/RDA)
 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Ken
 Sent: Wednesday, August 03, 2011 12:13 PM
 To: Jeffrey Joh
 Cc: r-help@r-project.org
 Subject: Re: [R] Convert matrix to numeric
 
 How about
 Matrix[1:5,]=as.numeric(Matrix[1:5,])
 -Ken Hutchison
 
 On Aug 3, 2554 BE, at 3:04 PM, Jeffrey Joh johjeff...@hotmail.com
 wrote:
 
 
  I have a matrix that looks like this:
 
 
  structure(c(0.0376673981759913, 0.111066500741386, 1, 1103,
  18, OPEN, DEPR, 0.0404073656092023, 0.115186044704599,
  1, 719, 18, OPEN, DEPR, 0.0665342096693433,
 0.197570061769498,
  1, 1103, 18, OPEN, DEPR, 0.119287147905722,
 0.356427096010845,
  1, 1103, 18, OPEN, DEPR), .Dim = c(7L, 4L), .Dimnames =
 list(
 c(Sn, SlnC, housenum, date, hour, flue,
 pressurization
 ), c(10019.BLO, 1002.BLO, 10020.BLO, 10021.BLO)))
 
 
 
  How do I convert rows 1-5 to numeric?  I tried mode() - numeric
 but that doesn't change anything.
 

Ken,

You can't store the numeric values back in the matrix, because rows 6 and 7 
contain character values.  Everything will just be converted back to character. 
 You need to create a new matrix for the numeric values.

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert matrix to numeric

2011-08-03 Thread David Winsemius

Here's what you _should_  do

1) transpose
2a) as.data.frame
3a) fix the stupid default stringsAsFactor behavior
4a) convert the first 5 columns to numeric

dfrm - as.data.frame( t( structure(.) ) )
dfrm[, 1:5] -lapply(dfrm[, 1:5], as.character)
dfrm[, 1:5] -lapply(dfrm[, 1:5], as.numeric)

Or:
1) transpose
2b) as.data.frame with stringsAsFactors= FALSE
3b) convert to numeric




On Aug 3, 2011, at 3:04 PM, Jeffrey Joh wrote:



I have a matrix that looks like this:


structure(c(0.0376673981759913, 0.111066500741386, 1, 1103,
18, OPEN, DEPR, 0.0404073656092023, 0.115186044704599,
1, 719, 18, OPEN, DEPR, 0.0665342096693433,  
0.197570061769498,
1, 1103, 18, OPEN, DEPR, 0.119287147905722,  
0.356427096010845,
1, 1103, 18, OPEN, DEPR), .Dim = c(7L, 4L), .Dimnames =  
list(
   c(Sn, SlnC, housenum, date, hour, flue,  
pressurization

   ), c(10019.BLO, 1002.BLO, 10020.BLO, 10021.BLO)))



How do I convert rows 1-5 to numeric?  I tried mode() - numeric  
but that doesn't change anything.




I also tried converting this to a table then converting to numeric,  
but I got: (list) object cannot be coerced to type 'double'




Jeff
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Prof Brian Ripley

On Mon, 20 Oct 2008, [EMAIL PROTECTED] wrote:


I have a matrix x with repeating row names.



zz-matrix(0,4,4)

rownames(zz)=c(a,a,b,b)

data.frame(zz) (?)


The row names on a data frame should be unique.  You can try
as.data.frame(xx, row.names=FALSE) to convert zz to be a data frame.  If
you need the row name information, add it as a column in the data frame,
e.g. mydataframe$rnames - rownames(zz).  (Note to R-Core: the
documentation for as.data.frame doesn't mention the usage of
row.names=FALSE to ignore row names, but it seems to work consistently.
Does the help page for as.data.frame need updating?)


No.  row.names=FALSE is not intended to work, and did you check every 
single as.data.frame() method?


It just so happens that for the matrix method invalid input for 
'row.names' results in setting default row names.  Other methods may 
differ.





lm(as.formula(paste(final_dat[,5]~,paste(colnames(x),collapse=+))),x
)

this gives me a error



Error in model.frame.default(formula =
as.formula(paste(final_dat[,5]~,  :

  'data' must be a data.frame, not a matrix or an array


I suspect that if you try class(x), it will be a matrix, not the requisite
data frame.

Regards,
Richie.

Mathematical Sciences Unit
HSL



ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Richard . Cotton
  The row names on a data frame should be unique.  You can try
  as.data.frame(xx, row.names=FALSE) to convert zz to be a data frame. 
If
  you need the row name information, add it as a column in the data 
frame,
  e.g. mydataframe$rnames - rownames(zz).  (Note to R-Core: the
  documentation for as.data.frame doesn't mention the usage of
  row.names=FALSE to ignore row names, but it seems to work 
consistently.
  Does the help page for as.data.frame need updating?)
 
 No.  row.names=FALSE is not intended to work, and did you check every 
 single as.data.frame() method?
 
 It just so happens that for the matrix method invalid input for 
 'row.names' results in setting default row names.  Other methods may 
 differ.

row.names=FALSE seems a natural way of supressing existing row names to 
me, since it corresponds nicely to using row.names=FALSE in write.csv. 
Currently it seems that if a matrix has duplicate row names, then 
converting it to be a data frame requires

rnames - rownames(mymatrix)
rownames(mymatrix) - NULL
as.data.frame(mymatrix)
rownames(mymatrix) - rnames 

Ideally, three of these lines of code shouldn't really need to be there.

If you disagree that allowing row.names=FALSE is a good idea, or you don't 
want to change the function interface, then perhaps having as.data.frame 
check for duplicates and throwing a warning (rather than an error) would 
be preferable behaviour.  I do realise that there are dozens of 
as.data.frame methods, and the documentation does state that Few of the 
methods check for duplicated row names, but it would be beneficial from a 
user standpoint.

Regards,
Richie.

Mathematical Sciences Unit
HSL



ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Richard . Cotton
 I have a matrix x with repeating row names. 

 zz-matrix(0,4,4)
 
 rownames(zz)=c(a,a,b,b)
 
 data.frame(zz) (?)

The row names on a data frame should be unique.  You can try 
as.data.frame(xx, row.names=FALSE) to convert zz to be a data frame.  If 
you need the row name information, add it as a column in the data frame, 
e.g. mydataframe$rnames - rownames(zz).  (Note to R-Core: the 
documentation for as.data.frame doesn't mention the usage of 
row.names=FALSE to ignore row names, but it seems to work consistently. 
Does the help page for as.data.frame need updating?)

 lm(as.formula(paste(final_dat[,5]~,paste(colnames(x),collapse=+))),x
 )
 
 this gives me a error

 Error in model.frame.default(formula =
 as.formula(paste(final_dat[,5]~,  : 
 
   'data' must be a data.frame, not a matrix or an array

I suspect that if you try class(x), it will be a matrix, not the requisite 
data frame.

Regards,
Richie.

Mathematical Sciences Unit
HSL



ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.