Re: [R] ideas about how to reduce RAM & improve speed in trying to use lapply(strsplit())

2011-05-29 Thread Ian Gow
Not a new approach, but some benchmark data (the perl=TRUE speeds up Jim's suggestion): > x <- c('18x.6','12x.9','302x.3') > y <- rep(x,10) > system.time(temp <- unlist(lapply(strsplit(y,".",fixed=TRUE),function(x) >x[1]))) user system elapsed 1.203 0.018 1.222 > system.time(temp2 <-

Re: [R] NaN, Inf to NA

2011-05-26 Thread Ian Gow
> df$a[is.infinite(df$a) | is.nan(df$a) ] <- NA > df a 1 NA 2 NA 3 NA 4 1 5 2 6 3 On 5/26/11 3:18 PM, "Albert-Jan Roskam" wrote: >Hi, > >I want to recode all Inf and NaN values to NA, but I;m surprised to see >the >result of the following code. Could anybody enlighten me about this? > >>

Re: [R] Importing fixed-width data

2011-05-25 Thread Ian Gow
Everything looks OK. Does this help? > test <- >data.frame(alpha=as.factor(c("A","A","B","B","C")),number=c(1,2,3,4,5)) > mode(test) [1] "list" > class(test) [1] "data.frame" > sapply(test, mode) alphanumber "numeric" "numeric" > sapply(test, class) alphanumber "factor" "numeric

Re: [R] how to eliminate first row in datafile created in do loop

2011-05-24 Thread Ian Gow
3975" >one.month "5" "30.894195075" "37.657271835" >one.month "6" "29.27843098" "37.59689852" >one.month "7" "27.5014142975" "34.36265367" >one.month "8" "26.4055425"

Re: [R] how to eliminate first row in datafile created in do loop

2011-05-24 Thread Ian Gow
Gregory: Would setting limit.list <- NULL at the start do the trick? See example below: Analogue of your example: lower <- 0 upper <- 0 limit.list<-data.frame(lower,upper) for(i in 1:12) { some.data <- rnorm(2) lower <- min(some.data) upper <- max(some.data) one.month <- data.frame(low

Re: [R] days between dates

2011-05-23 Thread Ian Gow
Geoffrey: There may be something for this in one of the packages dealing with dates. If not, here's one (incomplete) idea, based on something I used for a similar issue a little while ago. Essentially, make a data frame that ranks each weekday over a period in ascending order. This data frame

Re: [R] Downloading a csv from Dropbox using the shareable link

2011-05-23 Thread Ian Gow
ofile and then use Sys.getenv("DROPBOX_PATH") to access the path. It seems from looking at forums for Dropbox that there is no easily accessed environment variable that Dropbox sets for the path to the Dropbox folder. -- Ian Gow Accounting Information and Management Kellogg School of

Re: [R] Add a vector to the values in a specific dimension of an array

2011-05-18 Thread Ian Gow
Hi: Reordering the dimensions, then doing a vectorized addition, then reordering (back) again is faster, it seems. > m <- 20; n <- 30; p <- 40; q <- 30 > a <- NA > length(a) <- m * n * p * q > dim(a) <- c(m, n, p, q) > x <- 1:n > a[1:m,,1:p,1:q] <- 0 > b <- a > > # Approach 1 > system.time({ +

Re: [R] rbind with partially overlapping column names

2011-05-15 Thread Ian Gow
That approach relies on df1 and df2 not having overlapping values in b. Slight variation in df2 gives different results: > df1 <- data.frame(a=c("A","A"),b=c("B","B")) > df2 <- data.frame(b=c("B","B"),c=c("c","c")) > merge(df1,df2,all=TRUE) b a c 1 B A c 2 B A c 3 B A c 4 B A c On 5/15/11 11:1

Re: [R] rbind with partially overlapping column names

2011-05-15 Thread Ian Gow
Hi: This is a bit of a kluge, but works for your test case: > df2[,setdiff(names(df1),names(df2))] <- NA > df1[,setdiff(names(df2),names(df1))] <- NA > df3 <- rbind(df1,df2) > df3 a b c 1 A B 2 A B 3 b c 4 b c -Ian On 5/15/11 7:41 PM, "Jonathan Flowers" wrote: >Hello, > >I would like to

Re: [R] how to merge within range?

2011-05-14 Thread Ian Gow
t; df.2 > from to value > 1 99 303 1 > 2 500 702 3 > 3 799 950 5 > > what I want is: > time value > 1 1011 > 2 1991 > 3 3011 > 4 401NA > 5 5013 > 6 6013 > 7 7003 > 8 8005 > 9 9005

Re: [R] how to merge within range?

2011-05-14 Thread Ian Gow
If I assume that the third column in data.frame.2 is named "val" then in SQL terms it _seems_ you want SELECT a.time, b.val FROM data.frame.1 AS a LEFT JOIN data.frame.2 AS b ON a.time BETWEEN b.start AND b.end; Not sure how to do that elegantly using R subsetting/merge, but you might try a packa