Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

r - Avoid rbind()/cbind() conversion from numeric to factor

I'm trying to build a dataset before plotting it. I decided to use function factory gammaplot.ff() and the first version of my code looks like this:

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))

Turns out, the whole data frame contains character data, so I have to convert it back manually (took me a while to discover that in the first place!). SO search indicates that this happens because type variable is character. To avoid this (you can imagine performance issues on character data while building the data set!) I changed the code a bit:

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)
  }
}

This works fine for me, but I lost a self-explanatory character parameter, which is a downside. Is there a way to keep the first version of function factory without an implicit conversion of all data to character?

If there's another way of achieving the same result, I'd be happy to try it out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use rbind.data.frame and cbind.data.frame instead of rbind and cbind.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...