The as.numeric(d.df$pH) should have been an as.numeric(as.character(d.df$pH)).
Sorry for the confusion. Greg On 1/5/10 12:33 AM, Greg Hirson wrote: > Dieter, > > I'd approach this by first making a matrix, then converting to a data > frame with appropriate types. I'm sure there is a way to do it with > structure in one step, but here it is: > > a <- function() { > len <- 100000 > d <- replicate(len, list(pH = 3, marker = TRUE, position = "A"), FALSE) > d.matrix <- matrix(unlist(d), ncol = 3, byrow = TRUE) > d.df <- as.data.frame(d) > names(d.df) <- c('pH', 'marker', 'position') > > #d.df$pH <- as.numeric(d.df$pH) #incorrect d.df$pH <- as.numeric(as.character(d.df$pH)) #correct > d.df$marker <- as.logical(d.df$marker) > return(d.df) > } > > system.time(a) > > > On 1/4/10 11:43 PM, Dieter Menne wrote: >> I have very large data sets given in a format similar to d below. Converting >> these to a data frame is a bottleneck in my application. My fastest version >> is given below, but it look clumsy to me. >> >> Any ideas? >> >> Dieter >> >> # ----------------------- >> len = 100000 >> d = replicate(len, list(pH = 3,marker = TRUE,position = "A"),FALSE) >> # Data are given as d >> >> # preallocate vectors >> pH =rep(0,len) >> marker =rep(0,len) >> position =rep(0,len) >> >> system.time( >> { >> for (i in 1:len) >> { >> d1 = d[[i]] >> #Assign to vectors >> pH[i] = d1[[1]] >> marker[i] = d1[[2]] >> position[i] = d1[[3]] >> } >> # combine vectors >> pHAll = data.frame(pH,marker,position) >> } >> ) >> >> >> > > -- > Greg Hirson > ghir...@ucdavis.edu > > Graduate Student > Agricultural and Environmental Chemistry > > 1106 Robert Mondavi Institute North > One Shields Avenue > Davis, CA 95616 > -- Greg Hirson ghir...@ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616 [[alternative HTML version deleted]] ______________________________________________ R-help@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.