An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110526/2886768f/attachment.pl>
matrix not working
5 messages · Dat Mai, Andy Zhu, David Winsemius +1 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110526/7cd29303/attachment.pl>
On May 26, 2011, at 1:53 PM, Andy Zhu wrote:
Dat: 1. you can use as.matrix to convert data.frame to matrix; 2. it is likely that the internal representation of your data.frame may not be numerical value; matrix can only take on numeric.
Not true. Can be any single mode, including "character", "list", and "logical".
david.
>
>
> --- On Thu, 5/26/11, Dat Mai <dat.d.mai at gmail.com> wrote:
>
> From: Dat Mai <dat.d.mai at gmail.com>
> Subject: [R] matrix not working
> To: r-help at r-project.org
> Date: Thursday, May 26, 2011, 12:24 PM
>
> Hello All,
>
> I'm trying to create a matrix from a dataframe (let's call it df):
> ......a......b.....c.....d
> a inputs output
> b inputs output
> c inputs output
> d inputs output
> e inputs output
>
> The inputs are represented by columns a and b
> The outputs are represented by columns c and d, but the only outputs
> are
> those from column d
> - some values from column d are NA
> - column d was created with the code:
>
> df$d=rank(df$c, na.last="keep")
>
> #----------R Code---------#
> item=unique(df$a)
> n=length(list)
>
> r=matrix(data=NA,nrow=n, ncol=n, dimnames=list(PRR1=item, PRR2=item))
>
> for(j in 2:ln)
> {
> for(i in 1:(j-1))
> {
> input1=rownames(r)[i]
> input2=colnames(r)[j]
>
> q=df[(df$a==input1 & df$b==input2), "d"]
>
> if(length(q)==0)
> {
> q=df[(df$a==input2 & df$b==input1), "d"]
> }
>
> if(length(q)==0)
> {
> q=NA
> }
>
> r[j,i]=q
> r[i,j]=q
> r[j,j]=q
> }
> }
>
> The result is a matrix with the appropriate dimensions, but
> everything is
> filled with NA instead of the rankings of the various combinations.
> I'd like
> for the matrix to be filled with the ranking values--what have I
> done wrong?
> --
> Best,
> Dat Mai
> PhD Rotation Student
> Albert Einstein College of Medicine
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110526/e65f8ba6/attachment.pl>
Please... ?"[" Online tutorial "An Introduction to R." I think you'll find everything you need in these. -- Bert
On Thu, May 26, 2011 at 12:39 PM, Dat Mai <dat.d.mai at gmail.com> wrote:
When I use the as.matrix, the data.frame does turn into a matrix, but I cannot change the dimensions of the matrix. I'd still want it to have that pseudo cartesian format (e.g. [a1,b1], [a2,b2]) On Thu, May 26, 2011 at 6:58 PM, David Winsemius <dwinsemius at comcast.net>wrote:
On May 26, 2011, at 1:53 PM, Andy Zhu wrote: ?Dat:
1. you can use as.matrix to convert data.frame to matrix; 2. it is likely that the internal representation of your data.frame may not be numerical value; matrix can only take on numeric.
Not true. Can be any single mode, including "character", "list", and "logical". -- david.
--- On Thu, 5/26/11, Dat Mai <dat.d.mai at gmail.com> wrote:
From: Dat Mai <dat.d.mai at gmail.com>
Subject: [R] ?matrix not working
To: r-help at r-project.org
Date: Thursday, May 26, 2011, 12:24 PM
Hello All,
I'm trying to create a matrix from a dataframe (let's call it df):
......a......b.....c.....d
a ? inputs ? ? ?output
b ? inputs ? ? ?output
c ? inputs ? ? ?output
d ? inputs ? ? ?output
e ? inputs ? ? ?output
The inputs are represented by columns a and b
The outputs are represented by columns c and d, but the only outputs are
those from column d
- some values from column d are NA
- column d was created with the code:
df$d=rank(df$c, na.last="keep")
#----------R Code---------#
item=unique(df$a)
n=length(list)
r=matrix(data=NA,nrow=n, ncol=n, dimnames=list(PRR1=item, PRR2=item))
for(j in 2:ln)
{
?for(i in 1:(j-1))
?{
? ?input1=rownames(r)[i]
? ?input2=colnames(r)[j]
? ?q=df[(df$a==input1 & df$b==input2), "d"]
? ?if(length(q)==0)
? ?{
? ? ?q=df[(df$a==input2 & df$b==input1), "d"]
? ?}
? ?if(length(q)==0)
? ?{
? ? ?q=NA
? ?}
? ?r[j,i]=q
? ?r[i,j]=q
? ?r[j,j]=q
?}
}
The result is a matrix with the appropriate dimensions, but everything is
filled with NA instead of the rankings of the various combinations. I'd
like
for the matrix to be filled with the ranking values--what have I done
wrong?
--
Best,
Dat Mai
PhD Rotation Student
Albert Einstein College of Medicine
? ?[[alternative HTML version deleted]]
______________________________________________ R-help at 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. ? ? ? ?[[alternative HTML version deleted]] ______________________________________________ R-help at 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
-- Best, Dat Mai PhD Rotation Student Albert Einstein College of Medicine ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at 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.
"Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics