[R] readBin into a data frame

2013-08-01 Thread Zhang Weiwu
Hello. readBin is designed to read a batch of data with the same spec, e.g. read 1 floats into a vector. In practise I read into data frame, not vector. For each data frame, I need to read a integer and a float. for (i in 1:1000) { dataframe$int[i] <- readBin(con, integer(), size

[R] use Vectorized function as range of for statement

2013-08-01 Thread Zhang Weiwu
I guess this has been discussed before, but I don't know the name of this problem, thus had to ask again. Consider this scenario: fun <- function(x) { print(x)} for (i in Vectorize(fun, "x")(1:3)) print("OK") [1] 1 [1] 2 [1] 3 [1] "OK" [1] "OK" [1] "OK" The optimal behaviour is: fun <- f

Re: [R] use Vectorized function as range of for statement

2013-08-01 Thread Zhang Weiwu
On Thu, 1 Aug 2013, Jeff Newmiller wrote: The Vectorize function is essentially a wrapped up for loop, so you are really executing two successive for loops. Note that the Vectorize function is not itself vectorised, so there is no particular advantage to using it in this way. You might as we

[R] why Vectorize conjures a list, not a vector?

2013-08-14 Thread Zhang Weiwu
The manual seems to suggest, with the SIMPLIFY = TRUE default option, Vectorize would conjure a vector if possible. Quote: SIMPLIFY: logical or character string; attempt to reduce the result to a vector, matrix or higher dimensional array; see the ‘simplify’ argument of ‘sappl

Re: [R] why Vectorize conjures a list, not a vector?

2013-08-15 Thread Zhang Weiwu
On Wed, 14 Aug 2013, Hervé Pagès wrote: Hi Zhang, First note that a list is a vector (try is.vector(list())). The documentation for sapply() and Vectorize() should say *atomic* vector instead of vector in the desccription of the 'simplify' and 'SIMPLIFY' arguments. So in order for sapply() t

[R] how to retain dimension when selecting one row from a matrix?

2013-08-15 Thread Zhang Weiwu
When you select a single row from a matrix, the dimsion is lost: n <- matrix(nrow = 3, ncol = 5) dim(n) [1] 3 5 dim(n[1,]) NULL dim(n[2,]) NULL This doesn't happen if you select more than one row: dim(n[1:2,]) [1] 2 5 This is causing trouble. SCENARIO: when I filter out unqualified sam

[R] to match samples by minute

2013-08-15 Thread Zhang Weiwu
Perhaps this is simple and common, but it took me quite a while to admit I cannot solve it in a simple way. The data frame `df` has the following columns: unixtime, value, factor Now I need a matrix of: unixtime, value-difference-between-factor1-and-factor2 The naive solution is:

[R] on how to make a skip-table

2013-09-12 Thread Zhang Weiwu
I've got two data frames, as shown below: (NR means Number of Record) record.lenths NR length 1 100 2 130 3 150 4 148 5 100 683 760 valida.records NR factor

Re: [R] on how to make a skip-table

2013-09-12 Thread Zhang Weiwu
It is a nice surprise to wake up receiving three answers, all producing correct results. Many thanks to all of you. Jim Holtman solved it with amazing clarity. Gang Peng using a traditioanl C-like pointer style and Arun with awesome tight code thanks to diff(). I am embrassed to see my mis-

[R] how to get values within a threshold

2013-09-13 Thread Zhang Weiwu
input: > values [1] 0.854400 1.648465 1.829830 1.874704 7.670915 7.673585 7.722619 > thresholds [1] 1 3 5 7 9 expected output: [1] 1 4 4 4 7 That is, need a vector of indexes of the maximum value below the threshold. e.g. First element is "1", becaus

Re: [R] how to get values within a threshold

2013-09-13 Thread Zhang Weiwu
On Fri, 13 Sep 2013, William Dunlap wrote: findInterval(thresholds, values) [1] 1 4 4 4 7 Thanks a lot! But now I have a new problem, a typical R issue perhaps. First, let's look at a successful case: > thresholds <- c(1,3,5,7,9) > values <- c(0.854, 1.648, 1.829, 1.874,

Re: [R] how to get values within a threshold

2013-09-13 Thread Zhang Weiwu
On Fri, 13 Sep 2013, William Dunlap wrote: You may want to append -Inf (or 0 if you know the data cannot be negative) to the start of your 'values' vector so you don't have to write code to catch the cases when a threshold is below the range of the values. > findInterval(thresholds, c(0,valu

[R] the problem of buying and selling

2013-09-13 Thread Zhang Weiwu
I own a lot to the folks on r-help list, especially arun who answered every of my question and was never wrong. I am disinclined to once again ask this question, since it is more arithmatic than technical. But, having worked 2 days on it, I realized my brain is just not juicy enough Here

Re: [R] the problem of buying and selling

2013-09-14 Thread Zhang Weiwu
On Sat, 14 Sep 2013, Zhang Weiwu wrote: I own a lot to the folks on r-help list, especially arun who answered every of my question and was never wrong. I am disinclined to once again ask this question, since it is more arithmatic than technical. But, having worked 2 days on it, I realized

Re: [R] Instructions for upgrading R on ubuntu

2013-09-16 Thread Zhang Weiwu
On Sun, 15 Sep 2013, Andrew Crane-Droesch wrote: The c2d4u PPA is the main search result when googling "upgrade R 3.0.1 ubuntu". And it should be, because it is more likely that a PPA re-distribution works better for Ubuntu than a general distribution, even if it is an exceptional case with