Re: [R] matrix not working

From: Andy Zhu <andyzhu35_at_yahoo.com>
Date: Thu, 26 May 2011 10:53:59 -0700 (PDT)


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.

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.

Received on Thu 26 May 2011 - 18:47:39 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 May 2011 - 19:20:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive