2013-10-23 10 views
21

Ich versuche, ein Dataset vor dem Plotten zu erstellen. Ich entschied Funktion Fabrik gammaplot.ff() und die erste Version meines Codes zu verwenden sieht wie folgt aus:Vermeiden Sie rbind()/cbind() Konvertierung von numerisch zu Faktor

PowerUtility1d <- function(x, delta = 4) { 
    return(((x+1)^(1 - delta))/(1 - delta)) 
} 
PowerUtility1d <- Vectorize(PowerUtility1d, "x") 

# function factory allows multiparametrization of PowerUtility1d() 
gammaplot.ff <- function(type, gamma) { 
    ff <- switch(type, 
       original = function(x) PowerUtility1d(x/10, gamma), 
       pnorm_wrong = function(x) PowerUtility1d(2*pnorm(x)-1, gamma), 
       pnorm_right = function(x) PowerUtility1d(2*pnorm(x/3)-1, gamma) 
      ) 
    ff 
} 

gammaplot.df <- data.frame(type=numeric(), gamma=numeric(), 
          x=numeric(), y=numeric()) 
gammaplot.gamma <- c(1.1, 1.3, 1.5, 2:7) 
gammaplot.pts <- (-1e4:1e4)/1e3 

# building the data set 
for (gm in gammaplot.gamma) { 
    for (tp in c("original", "pnorm_wrong", "pnorm_right")) { 
    fpts <- gammaplot.ff(tp, gm)(gammaplot.pts)  
    dataChunk <- cbind(tp, gm, gammaplot.pts, fpts) 
    colnames(dataChunk) <- names(gammaplot.df) 
    gammaplot.df <- rbind(gammaplot.df, dataChunk) 
    } 
} 

# rbind()/cbind() cast all data to character, but x and y are numeric 
gammaplot.df$x <- as.numeric(as.character(gammaplot.df$x)) 
gammaplot.df$y <- as.numeric(as.character(gammaplot.df$y)) 

Es stellte sich heraus, das gesamte Datenrahmen Zeichendaten enthält, also muss ich es manuell konvertieren zurück (dauerte eine Weile, bis entdecke das an erster Stelle!). SO Suche indicates, dass dies passiert, weil Typ Variable ist Zeichen. Um dies zu vermeiden (! Sie können Performance-Probleme auf Zeichendaten vorstellen, während der Datensatz Aufbau) änderte ich den Code ein bisschen:

gammaplot.ff <- function(type, gamma) { 
    ff <- switch(type, 
       function(x) PowerUtility1d(x/10, gamma), 
       function(x) PowerUtility1d(2*pnorm(x)-1, gamma), 
       function(x) PowerUtility1d(2*pnorm(x/3)-1, gamma) 
      ) 
    ff 
} 

for (gm in gammaplot.gamma) { 
    for (tp in 1:3) { 
    fpts <- gammaplot.ff(tp, gm)(gammaplot.pts)  
    dataChunk <- cbind(tp, gm, gammaplot.pts, fpts) 
    colnames(dataChunk) <- names(gammaplot.df) 
    gammaplot.df <- rbind(gammaplot.df, dataChunk) 
    } 
} 

Dies funktioniert gut für mich, aber ich verlor eine selbsterklärende Zeichenparameter, die ist ein Nachteil. Gibt es eine Möglichkeit, die erste Version der Funktion Factory ohne eine implizite Konvertierung aller Daten in Zeichen zu behalten?

Wenn es eine andere Möglichkeit gibt, das gleiche Ergebnis zu erzielen, würde ich es gerne ausprobieren.

+0

@Thomas, deine kurze Antwort ist eindeutig falsch; Siehe akzeptierte Antwort. Auch die Aussage, dass man nichts ohne eine Alternative tun sollte, ist nicht konstruktiv. –

Antwort

48

Sie können rbind.data.frame und cbind.data.frame anstelle von rbind und cbind verwenden.

+0

Es ist sogar genug, nur 'cbind.data.frame' zu ​​verwenden, da' rbind' korrekt funktioniert (Datenrahmen wird als eines seiner Argumente erkannt). Dachte nie, es wäre so einfach! – tonytonov

+2

Vorsicht: Wenn Sie 'cbind.data.frame()' in Kombination mit Names-Listen verwenden, erstellen Sie eine n x m-Matrix, die Sie nicht wollten. Falls dies nicht beabsichtigt ist, "liste()" die benannte Liste auf, bevor "cbind.data.frame()' anstelle von "cbind()' verwendet wird. – BurninLeo

+6

'cbind.data.frame (tp, gm, gammaplot.pts, fpts, stringsAsFactors = FALSCH)' Wenn Sie keine StringsAsFactors = F haben, können Sie immer noch Faktoren haben. – mtelesha

Verwandte Themen