[R] how to draw random numbers from many categorical distributions quickly?

2011-12-14 Thread Sean Zhang
Dear R helpers, I have a question about drawing random numbers from many categorical distributions. Consider n individuals, each follows a categorical distribution defined over k categories. Consider a simple case in which n=4, k=3 as below catDisMat <- rbind(c(0.1,0.2,0.7),c(0.2,0.2,0.6),c(0.1,

[R] how to efficiently extract elements of a list?

2011-02-07 Thread Sean Zhang
Dear R helper, I wonder whether there is a quick way to extract some elements for a list. for a vector we can do the following vec <- seq(3) names(vec) <- LETTERS[1:3] vec[c(1,3)] vec[c('A','C')] But for a list, test.l <- list(c(1,3),array(NA,c(1,2)),array(0,c(2,3))) names(test.l)<-LETTERS[1:

[R] using character vector as input argument to setkey (data.table pakcage)

2011-02-06 Thread Sean Zhang
Dear R helpers, I wonder how to use a character vector as an input argument to setkey (data.table package). The following works: library(data.table) test.dt <- data.table(expand.grid(a=1:30,b=LETTERS),c=seq(30*26)) setkey(test.dt,a,b) I like a similar function, but can accept c('a','b') as an in

[R] how to cut a multidimensional array along a chosen dimension and store each piece into a list

2011-01-17 Thread Sean Zhang
Dear R-Helpers, I wonder whether there is a function which cuts a multiple dimensional array along a chosen dimension and then store each piece (still an array of one dimension less) into a list. For example, arr <- array(seq(1*2*3*4),dim=c(1,2,3,4)) # I made a point to set the length of the fir

[R] question about deparse(substitute(...))

2011-01-13 Thread Sean Zhang
Dear R helpers: I like to apply deparse(substitute()) on multiple arguments to collect the names of the arguments into a character vector. I used function test.fun as below. it works when there is only one input argument. but it does not work for multiple arguements. can someone kindly help? test

Re: [R] how to replace a single backward slash with a double backward slash?

2009-12-13 Thread Sean Zhang
Correct me, if I misunderstand and there is no such a limitation. Thanks again. -Sean On Sun, Dec 13, 2009 at 5:26 PM, David Winsemius wrote: > > On Dec 13, 2009, at 5:11 PM, Sean Zhang wrote: > > Dear R-helpers: >> >> Hours ago, I asked how to replace a single

[R] how to replace a single backward slash with a double backward slash?

2009-12-13 Thread Sean Zhang
Dear R-helpers: Hours ago, I asked how to replace a single forward slash with a double backward slash and recieved great help. Thanks again for all the repliers. In the meantime, I wonder how to replace a single backward slash with a double backward slash? e.g., I want change "c:\test" into "c:\

[R] how to replace a single forward slash with a double backward slash in a string?

2009-12-13 Thread Sean Zhang
Dear R-helpers. Can someone kindly tell me how to replace a single forward slash with double backward slash in a string? i.e., from "a/b" to "a\\b" Many thanks in advance. -Sean [[alternative HTML version deleted]] __ R-help@r-project.org

Re: [R] how to convert character string with only month and year into date

2009-09-22 Thread Sean Zhang
t 3:03 PM, Sean Zhang wrote: > > Dear R helpers. >> >> I am new to plotting time data using R. >> wonder how to convert character time info into date in R. >> I searched over the web but did not find answer. >> >> the input character string is something li

[R] how to convert character string with only month and year into date

2009-09-22 Thread Sean Zhang
Dear R helpers. I am new to plotting time data using R. wonder how to convert character time info into date in R. I searched over the web but did not find answer. the input character string is something like 03_1993 or 03-1993, so the precision is at month level. I tried the following but failed

[R] why need a new database to store results generated from another database in filehash?

2009-08-19 Thread Sean Zhang
Dear R-Helpers: I am trying filehash and would like to know whether I have to create a new database to store results generated from another database. Example code is presented below to show the question. #R code below library(filehash) dbCreate("myDB1") db1 <- dbInit("myDB1") dbDelete

[R] how to pass more than one argument to the function called by lapply?

2009-08-18 Thread Sean Zhang
Dear R helpers: I wonder how to pass more than one argument to the function called by lapply. For example, #R code below --- indf <- data.frame(id=I(c('a','b')),y=c(1,10)) #I want to add an addition argument cutoff into the function called by lapply. outside.fun <- functi

[R] how to use do.call together with cbind and get inside a function

2009-07-26 Thread Sean Zhang
Dear R-helpers: I have a question related to using do.call to call cbind and get. #the following works vec1 <- c(1,2) vec2 <- c(3,4) ColNameVec <- c('vec1','vec2') mat <- do.call("cbind",lapply(ColNameVec,get)) mat #put code above into a function then it does not work #before doing so, first remo

[R] Problem with storing a sequence of lmer() model fit into a list

2009-06-22 Thread Sean Zhang
Dear R-helpers: May I ask a question related to storing a number of lmer model fit into a list. Basically, I have a for-loop (see towards the bottom of this email) in the loop, I am very sure that the i-th model fit (i.e.,fit_i) is successfully generated and the character string (i.e., tmp_i) is c

[R] how to apply the dummy coding rule in a dataframe with complete factor levels to another dataframe with incomplete factor levels?

2009-06-19 Thread Sean Zhang
Dear R helpers: Sorry to bother for a basic question about model.matrix. Basically, I want to apply the dummy coding rule in a dataframe with complete factor levels to another dataframe with incomplete factor levels. I used model.matrix, but could not get what I want. The following is an example.

[R] Sean / Re: question related to fitting overdispersion count data using lmer quasipoisson

2009-04-11 Thread Sean Zhang
Hey Buddy, Hope you have been doing well since last contact. If you have the answer to the following question, please let me know. If you have chance to travel up north. let me know. best, -Sean -- Forwarded message -- From: Sean Zhang Date: Sat, Apr 11, 2009 at 12:12 PM

[R] question related to fitting overdispersion count data using lmer quasipoisson

2009-04-11 Thread Sean Zhang
Dear R-helpers: I have a question related to fitting overdispersed count data using lmer. Basically, I simulate an overdispsed data set by adding an observation-level normal random shock into exp(+rnorm()). Then I fit a lmer quasipoisson model. The estimation results are very off (see model out

[R] How to handle tabular form data in lmer without expanding the data into binary outcome form?

2009-04-10 Thread Sean Zhang
Dear R-gurus: I have a question about lmer. Basically, I have a dataset, in which each observation records number of trials (N) and number of events (Y) given a covariate combination(X) and group id (grp_id). So, my dataset is in tabular form. (in case my explanation of tabular form is unclear, pl

Re: [R] What is the best package for large data cleaning (not statistical analysis)?

2009-03-15 Thread Sean Zhang
stored as binary > objects in filehash, it was pretty fast to retrieve them, pick out the > data I needed from each month and create a subset of just the data I > needed that would now fit in memory. > > So it all depends ... > > On Sat, Mar 14, 2009 at 8:46 PM, Se

[R] What is the best package for large data cleaning (not statistical analysis)?

2009-03-14 Thread Sean Zhang
Dear R helpers: I am a newbie to R and have a question related to cleaning large data frames in R. So far, I have been using SAS for data cleaning because my data sets are relatively large (handling multiple files, each could be as large as 5-10 G). I am not a fan of SAS at all and am eager to mo

Re: [R] Is there any difference between <- and =

2009-03-12 Thread Sean Zhang
ng from "environment" perspective validly exists). Thank you all again very much! -Sean Zhang #My little example is listed below f1<-function(a=1,b=2) {print(a); print(b); print(a-b) } f1() #get 3, makes sense f1(2,) #get 0, makes sense a <- 10 b <- 20 f1(a=a+1,b=a) a #get 10 a

[R] Is there any difference between <- and =

2009-03-11 Thread Sean Zhang
Dear R-helpers: I have a question related to <- and =. I saw very experienced R programmers use = rather than <- quite consistently. However, I heard from others that do not use = but always stick to <- when assigning valuese. I personally like = because I was using Matabl, But, would like to re

Re: [R] How to write a function that accepts unlimited number of input arguments?

2009-03-09 Thread Sean Zhang
Big thanks for your help and suggestion for email communication. -s On Mon, Mar 9, 2009 at 12:18 PM, baptiste auguie wrote: > > On 9 Mar 2009, at 16:04, Sean Zhang wrote: > > Dear Baptiste: > > Many thanks for your help! > > Using the Reduce way, it works almost perf

[R] How to write a function that accepts unlimited number of input arguments?

2009-03-09 Thread Sean Zhang
Dear R-helpers: I am an R newbie and have a question related to writing functions that accept unlimited number of input arguments. (I tried to peek into functions such as paste and cbind, but failed, I cannot see their codes..) Can someone kindly show me through a summation example? Say, we have i

[R] How to make warning message colorful (or have sound)?

2009-02-25 Thread Sean Zhang
Dear R-helpers: I am new to R and wonder how to make a warning message colorful (if possible, having sound is also welcome). I did some research and failed to see options to allow this functionality. Is this a techical limitation so far, or I miss some information. Many thanks in advance. -sean

[R] how to NULL multiple variables of a df efficiently?

2009-02-24 Thread Sean Zhang
Dear R-helpers: I am an R novice and would appreciate answer to the following question. Want to delete many variables in a dataframe. Am able to delete one variable by assigning it as NULL Have a large number of variables and would like to delete them without using a for loop. Is there a command

[R] How to transfer a list of space delimited character elements into a char vector?

2009-02-20 Thread Sean Zhang
My dear R-helpers: I am a novice in R and have the following text string manipulation question. Is there a function that performs the job described below? Say, wanted_output <- c("ab", "cd", "ef") #the function_wanted can generate c("ab", "cd", "ef") using ab cd ef as the single input argument w

[R] How to apply table() on subdata and stack outputs

2009-02-11 Thread Sean Zhang
Dear R helpers: I am a R novice and have a question about using table() to extract frequences over many sub-datasets. A small example input dataframe and wanted output dataframe are provided below. The real data is very large so a for loop is what I try to avoid. Can someone englithen me how to u

[R] 3d scatter plot with both error bars and a flexibly fitted surface

2009-01-23 Thread Sean Zhang
Dear R-helpers: I, an entry level R user, wonder how make a 3d scatter plot with both error bars and a flexibly fitted surface. Can anyone eligthen me? Many Thanks in advance. -Sean [[alternative HTML version deleted]] __ R-help@r-project

[R] misalignment of x-axis when overlaying two plots using latticeExtra

2009-01-14 Thread Sean Zhang
Dear R-helpers: I am an entry-level R user and have a question related to overlaying a barchart and and a xyplot using latticeExtra. My problem is that when I overlay them I fail to align their x-axes. I show my problem below through an example. #the example data frame is provided below ve

[R] Object name vectcor as function input argument?

2008-12-27 Thread Sean Zhang
large number of dfs loop.fun <- function (all.dfs) { for (i in 1:length(all.dfs) ) ifelse ( i==1, output <- get(all.dfs[i]), output <- rbind(output,get(all.dfs[i])) ) return(output) } output <- loop.fun(all.dfs) #Your help is highly appreciated. Many thanks in advance. -S

[R] How to skip re-installing CRAN packages when updating R?

2008-12-24 Thread Sean Zhang
Dear R-helpers: I am new to R and would like to seek your expert opinion on installation tip. Many thanks in advance. I want to update my R to the newest version and wonder the following two questions: Question 1: How can I install R and its contributed packages in a way so when updating R in t

[R] Extract values based on indexes without looping?

2008-12-23 Thread Sean Zhang
Dear R-Helpers: I am a entry level user of R. Have the following question. Many thanks in advance. # value.vec stores values value.vec <- c('a','b','c') # which.vec stores the locations/indexs of values in value.vec. which.vec <- c(3, 2, 2, 1) # How can I obtain the following vector based on t

[R] quotation problem/dataframe names as function input argument.

2008-12-23 Thread Sean Zhang
Dear R friends: Can someone help me with the following problem? Many thanks in advance. # Problem Description: # I want to write functions which take a (character) vector of dataframe names as input argument. # For example, I want to extract the number of observations from a number of dataframes.