Re: [R] Three values that add to the same number by 0.01 steps

2014-01-28 Thread Richard Kwock
An alternative using runif. x <- round(runif(1, 0, 1), 2) y <- round(runif(1, 0, 1-x), 2) z <- round(1-x-y, 2) sum1 <- cbind(x, y, z) any(!(sum1[,1] + sum1[,2] + sum1[,3])) Richard On Tue, Jan 28, 2014 at 9:36 AM, S Ellison wrote: > > I'd like to create a matrix with three columns

Re: [R] How to detect a sequence of NA values?

2014-01-08 Thread Richard Kwock
rle(is.na(a)) > max(rl$lengths[rl$values]) > #[1] 4 > > A.K. > > > > > On Wednesday, January 8, 2014 12:56 PM, Richard Kwock > wrote: > Another way: > > a = c(1,NA,NA,4,3,NA,NA,NA,NA,5) > > # find position of the non-NAs in the vector > pos = which(

Re: [R] How to detect a sequence of NA values?

2014-01-08 Thread Richard Kwock
Another way: a = c(1,NA,NA,4,3,NA,NA,NA,NA,5) # find position of the non-NAs in the vector pos = which(!is.na(a)) # this calculates the length by taking the differences between the non-NA positions. diff(pos)-1 #get the max max(diff(pos)-1) Richard On Tue, Jan 7, 2014 at 7:36 PM, arun wrote:

Re: [R] plot() function: color transparency

2013-12-18 Thread Richard Kwock
As Duncan suggested, this will probably get you what you want. You can set the transparency using alpha.f in adjustcolor(). x <- c(1:5) color <- c(2,2,3,4,5) color_transparent <- adjustcolor(color, alpha.f = 0.3) plot(x, col = color, pch = 20, cex = 4) plot(x, col = color_transparent, pch = 20,

Re: [R] how do I separete coloumns by comma?

2013-12-13 Thread Richard Kwock
Hi Elham, >From the (\t), It looks like the data in your data.txt file is tab-delimited. To take care of that, set the separator string to be: "sep = '\t' " instead of "," when you read in the file. text <- read.table(file = "data.txt", sep = "\t") text write.table(text, "textfile.csv", row.names

Re: [R] Lattice Legend/Key by row instead of by column

2013-11-01 Thread Richard Kwock
2, col = 4) > foo <- placeGrob(foo, > textGrob(lab = "The Beatles"), > row = 1, col = 6) > > xyplot(1 ~ 1, legend = list(top = list(fun = foo))) > > In my case I changed "strwidth" to "cm" for the text as I

[R] Lattice Legend/Key by row instead of by column

2013-10-31 Thread Richard Kwock
Hi All, I am having some trouble getting lattice to display the legend names by row instead of by column (default). Example: library(lattice) set.seed(456846) data <- matrix(c(1:10) + runif(50), ncol = 5, nrow = 10) dataset <- data.frame(data = as.vector(data), group = rep(1:5, each = 10), time

Re: [R] foreign package problem with stata 13

2013-10-02 Thread Richard Kwock
Use "saveold" instead of "save" when you save your dataset in stata. I don't think foreign library in R supports stata 13 yet. Richard On Wed, Oct 2, 2013 at 3:11 PM, Pablo Menese Camargo wrote: > foreign package does not support dataset saved at stata 13. > anyone knows any wayt to make it wor

Re: [R] capturing warnings within loops, so I know the iterations where warnings occurred?

2013-09-23 Thread Richard Kwock
Hi, Check out: https://stat.ethz.ch/pipermail/r-help/2010-December/262626.html > demo(error.catching) > tryCatch.W.E mylist <- list(NULL) mylist_warns <- list(NULL) old.warn <- options(warn=1) x <- c(1:5) for (i in 1:2) { assign("last.warning", NULL, envir = baseenv()) temp <- tryCatch.W.E

Re: [R] two questions about xyplot

2013-09-23 Thread Richard Kwock
..." in the panel function. Hope that helps. Richard On Mon, Sep 23, 2013 at 11:23 AM, Richard Kwock wrote: > Hi, > > To answer your second question you can do something like this: > > p<-xyplot(dvy ~ sessidx | case, group = numph, data=d66df, col = c(1:4), > layout=

Re: [R] two questions about xyplot

2013-09-23 Thread Richard Kwock
Hi, To answer your second question you can do something like this: p<-xyplot(dvy ~ sessidx | case, group = numph, data=d66df, col = c(1:4), layout=c(1, 3), xlab= "Sessions", ylab = "Number of Seconds", type="l") update(p, panel=function(...){ panel.xyplot(...) panel.a

Re: [R] search species with all absence in a presence-absence matrix

2013-09-20 Thread Richard Kwock
Hi, I believe the function you are looking for is: which("yourdata" == 0, arr.ind = T) The "arr.ind" parameter in the "which" function will return you a matrix with row, column indices for where there are 0's in your dataset. set.seed(6584) data <- matrix(sample(c(0,1), 36, replace = T), nc = 6

Re: [R] (no subject)

2013-09-19 Thread Richard Kwock
Hi Tony, The dimnames parameter is only in the matrix() function, not in the as.matrix() function. #So you can do: A <- matrix(rbind(Fert,M), nrow = nrow(rbind(Fert,M))) A #This for example will allow you to name your row and colums. B <- matrix(rbind(Fert,M), nrow = nrow(rbind(Fert,M)), dimname

Re: [R] Randomly drop a percent of data from a data.frame

2013-08-16 Thread Richard Kwock
Try this: data <- data.frame(x1=rnorm(5),x2=rnorm(5),x3=rnorm(5),x4=rnorm(5)) data <- round(data,digits=3) #get the total counts n = prod(dim(data)) #set up a dummy array/matrix dummy <- rep(F, n/2) dummy[sample(1:(n/2), n*.2)] <- T # 5x2 dummy matrix with T and F matrix(dummy, nc = 2) #subset