[R] RE : RE: using apply to loop [SEC=UNCLASSIFIED]

From: Louis Martin <louismartinbis_at_yahoo.fr>
Date: Fri, 21 Dec 2007 12:24:27 +0100 (CET)


Thanks Joe,

It works well. How can I expand the output of z to include all other classes from 1 to nclass

Right now I the output is:
> z

          truth
classified 4 5 8 10

        1   1  1  1  0
        4   6 10  8  0
        10  1  0  1  1

> Is it possible to have something like:

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0 0 0 1 1 0 0 1 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 6 10 0 0 8 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 0 0 0 0 0 0 0 0 0
[8,] 0 0 0 0 0 0 0 0 0 0 0 0
[9,] 0 0 0 0 0 0 0 0 0 0 0 0

[10,]    0    0    0    1    0    0    0    1    0     1     0     0
[11,]    0    0    0    0    0    0    0    0    0     0     0     0
[12,]    0    0    0    0    0    0    0    0    0     0     0     0

Thanks

"Crombie, Joe" <Joe.Crombie_at_brs.gov.au> a écrit : Hi Louis,

You could try this:

# find the index of the maximum value in each row of _data_, # disregarding the last column classified <- apply(data[,-(nclass+1)],1,which.max)

## or, if the maximum may be repeated:
classified <- apply(data[,-(nclass+1)], 1, FUN = function(x) which(x == max(x)))

# the variable _truth_ is just the last column of _data_ ? truth <- data[,nclass + 1]

?table
z <- table(classified, truth)

HTH Joe  

Joe Crombie  

Biosecurity and Information Sciences
Bureau of Rural Science
Canberra Australia  

e: joe.crombie_at_brs.gov.au  

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Louis Martin
Sent: Friday, 21 December 2007 11:37 AM
To: R-help_at_r-project.org
Subject: [R] using apply to loop

Hi,

I am running the following loop, but it takes hours to run as n is big. Is there any way "apply" can be used? Thanks. ### Start

    nclass <- dim(data)[[2]] - 1

    z <- matrix(0, ncol = nclass, nrow = nclass)
    n <- dim(data)[[1]]
    x <- c(1:nclass)

# loop starts
for(loop in 1:n) {
        r <- data[loop, 1:nclass]
        classified <- x[r == max(r)]
       
        truth <- data[loop, nclass + 1]
        z[classified, truth] <- z[classified, truth] + 1
    }
# loop ends              

[[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.

------IMPORTANT - This message has been issued by The Department of Agriculture, Fisheries and Forestry (DAFF). The information transmitted is for the use of the intended recipient only and may contain confidential and/or legally privileged material. It is your responsibility to check any attachments for viruses and defects before opening or sending them on.

Any reproduction, publication, communication, re-transmission, disclosure, dissemination or other use of the information contained in this e-mail by persons or entities other than the intended recipient is prohibited. The taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you have received this e-mail in error please notify the sender and delete all copies of this transmission together with any attachments. If you have received this e-mail as part of a valid mailing list and no longer want to receive a message such as this one advise the sender by return e-mail accordingly. Only e-mail correspondence which includes this footer, has been authorised by DAFF


             

        [[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 Fri 21 Dec 2007 - 11:29:51 GMT

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 Fri 21 Dec 2007 - 15:30:20 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.