2017-09-18 13 views
0
prob.matrix <- function(tables){ 

tables <- addmargins(table(titanicData$Pclass, titanicData$Survived)) 
tables 



l <- matrix(data = 0, nrow = 4, ncol = 3, byrow = FALSE) 
l 
for(x in 1:3){ 



    l[1:3, x] <- tables[c(1:3), x]/tables[c(4), x] 
    l[4, x] <- tables[c(4), x]/tables[(4), 3] 


} 
l 
} 


Pred <- function(Class, prob.matrix){ 
    if ("FirstClass") 
    { Class <- prob.matrix[c(1),3]} 
    else if (class == "SecondClass") { Class <- prob.matrix[c(2),3]} 
    else if (class == "ThirdClass") {Class <- prob.matrix[c(3), 3]} 
    class 
} 

Pred("FirstClass") 
class 

Ich versuche, die zweite Funktion zu funktionieren. Ich möchte in der Lage sein, die Klasse des Passagiers nach Pred in die Klammern einzugeben und dafür die relevanten Daten aus meiner Matrix anzuzeigen. Ich kann es nicht zur Arbeit bringen.Wie kann ich diese Funktion in R arbeiten lassen?

Vielen Dank für Ihre Hilfe.

+0

welche Funktion? Es gibt zwei von ihnen? – PoGibas

+0

Die zweite Funktion @PoGibas – Bashir

Antwort

0

Try this:

Pred <- function(Class, prob.matrix){ 
    if (Class == "FirstClass") { 
     class <- prob.matrix[c(1),3] 
    } else if (Class == "SecondClass") { 
     class <- prob.matrix[c(2),3] 
    } else if (Class == "ThirdClass") { 
     class <- prob.matrix[c(3), 3] 
    } 
    return(class) 
} 

Sie haben return am Ende Ihrer Funktion nutzen Ergebnis zurückzukehren.

+0

Vielen Dank – Bashir

Verwandte Themen