Re: [R] Using R to Calculate Coefficients of Relatedness and Kinship for Family Members

2015-03-06 Thread JS Huang
] == Input$Mother_ID[i + 1] Input$Father_ID[i] != Input$Father_ID[i + 1]) { result[i] - 0.125 } } } } } return(result) } assignKinScore(pedigree.data) [1] 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.5 - JS Huang -- View this message in context

Re: [R] Want to convert list with vectore of dis similar lengths to data frame

2015-03-05 Thread JS Huang
Hi, The following works by appending NA to make up the maximum length of the list. test4 $Row1 [1] a b c d e $Row2 [1] a b c d e f g $Row3 [1] h i j k l m n as.data.frame(t(sapply(1:length(test4),

Re: [R] help on output my own function result in ddply

2015-03-05 Thread JS Huang
Hi, There is a lot of if-else statement. r syntax rejects them. Here is an example of if-else example. In r we can code *ifelse(x2,y - 1,ifelse(x1,y - 0.5, y - 0))* for the following. if (x 2) { y = 1 } else { if (x 1) { y = 0.5 } else { y = 0 } } - JS

Re: [R] problem with function that adds rows to dataframe based on conditional statement

2015-03-05 Thread JS Huang
Hi Curtis, Maybe you forgot to tell us how the function seq_len is defined. - JS Huang -- View this message in context: http://r.789695.n4.nabble.com/problem-with-function-that-adds-rows-to-dataframe-based-on-conditional-statement-tp4704228p4704237.html Sent from the R help mailing list

Re: [R] sampling dataframe based upon number of record occurrences

2015-03-04 Thread JS Huang
Here is an implementation with function named getSample. Some modification to the data was made so that it can be read as a table. fitting.set IDbyYear SiteID Year 1 42.24 A-Airport 2006 2 42.24 A-Airport 2006 3 42.24 A-Airport 2006 4

Re: [R] sampling dataframe based upon number of record occurrences

2015-03-04 Thread JS Huang
Since you indicated there are six more columns in the data.frame, getSample modified below to take care of it. getSample function(x) { sites - unique(x$SiteID) years - unique(x$Year) result - data.frame() x$ID - seq(1,nrow(x)) for (i in 1:length(sites)) { for (j in

Re: [R] remove repeated string in list

2015-03-04 Thread JS Huang
Hi, Here is one for preserving the first strings. as.numeric in the previous posting is not necessary. temp $set1 [1] a b d x $set2 [1] b c q m $set3 [1] b f e k q h sapply(1:length(temp),function(x){c - list(); for (j in 1:x){c - c(c,temp[[j]])};

Re: [R] remove repeated string in list

2015-03-04 Thread JS Huang
Hi, To avoid hardcoded 1:3, here is some revision. temp $set1 [1] a b d x $set2 [1] b c q m $set3 [1] b f e k q h sapply(1:*length(temp)*,function(x){temp[[x]][as.numeric(table(unlist(temp))[temp[[x]]])==1]}) [[1]] [1] a d x [[2]] [1] c m [[3]] [1] f e k h -- View this message in

Re: [R] remove repeated string in list

2015-03-04 Thread JS Huang
Hi, Here is one for removing repeated strings. temp $set1 [1] a b d x $set2 [1] b c q m $set3 [1] b f e k q h sapply(1:3,function(x){temp[[x]][as.numeric(table(unlist(temp))[temp[[x]]])==1]}) [[1]] [1] a d x [[2]] [1] c m [[3]] [1] f e k h -- View this message in context:

Re: [R] Sum upto every twelth cell in a column

2015-03-02 Thread JS Huang
Here is an implementation. (x - c(23,35,22,11,10,1,14,15,13,15,17,16,154,13,24,25,25,25,25,25,22,11,15,15)) [1] 23 35 22 11 10 1 14 15 13 15 17 16 154 13 24 25 25 25 25 25 22 11 15 15 (y - c(0,cumsum(x))) [1] 0 23 58 80 91 101 102 116 131 144 159 176 192 346 359

Re: [R] Sorting data frame by prepared order

2015-03-02 Thread JS Huang
Here is an implementation. t - data.frame(x=c(1,1,1,1,1,2,2,2,2,2),y=c(a,a,a,b,b,a,a,b,b,b)) t x y 1 1 a 2 1 a 3 1 a 4 1 b 5 1 b 6 2 a 7 2 a 8 2 b 9 2 b 10 2 b assignSeq function(test) { temp - test[order(test$x),] InC - numeric(length(test)) inD - unique(test$x) countAll

Re: [R] evaluate logical expressions

2015-02-28 Thread JS Huang
Hi, I assume input y to wrapper - function(y) {function(x) {(y)}} is a function. In the statement to assign gfunc[[i]]- gsub((Gene)([[:digit:]]), x[\\2], func[[i]]) the mode of gsub((Gene)([[:digit:]]), x[\\2], func[[i]]) is character. Is this the issue? -- View this message in context:

Re: [R] integrate with vector arguments

2015-02-26 Thread JS Huang
Hi, The following works. f2 function(z) { f1 - function(t) { z*t + z*t^2 } return(f1) } sapply(1:5,function(x)integrate(f2(x),0,1)$value) [1] 0.83 1.67 2.50 3.33 4.17 -- View this message in context:

Re: [R] covert entire dataset to numeric while persuing percentage values

2015-02-26 Thread JS Huang
The following data.frame x as one column named Percent. x Percent 1 10% 2 20% 3 30% as.numeric(substr(x$Percent,1,nchar(x$Percent)-1)) [1] 10 20 30 -- View this message in context:

Re: [R] format selected columns in dataframe as character

2015-02-26 Thread JS Huang
Try as.character like the following shows. dfx - data.frame( + group = c(rep('A', 8), rep('B', 15), rep('C', 6)), + sex = sample(c(M, F), size = 29, replace = TRUE), + age = runif(n = 29, min = 18, max = 54)) dfx group sex age 1 A M 41.35554346 2 A F

Re: [R] How many digits are there in left of dot of 0.0001 ?

2015-02-26 Thread JS Huang
Hi, Some modification to work for both positive and negative number: nchar(format(*abs*(a),scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)+1) -1. -- View this message in context:

Re: [R] How many digits are there in left of dot of 0.0001 ?

2015-02-26 Thread JS Huang
Hi, I assume you want to know the digit count to the left of decimal point. If this is the case, then you may use trunc(log10(max(1,trunc(abs(a)+1 for a numerical variable a. Count 0.12 as having one digit to the left of decimal point. trunc(log10(max(1,trunc(abs(-10.99)+1 [1]

Re: [R] Processing key_column, begin_date, end_date in R

2015-02-26 Thread JS Huang
Hi, Here is an implemenation: date key_column begin_dateend_date 1 123456 2013-01-01 2014-01-01 2 123456 2013-07-01 2014-07-01 3 789102 2012-03-01 2014-03-01 4 789102 2015-02-01 2016-02-01 5 789102 2015-02-06 2016-02-06 y -

Re: [R] Processing key_column, begin_date, end_date in R

2015-02-26 Thread JS Huang
Hi, It's not as easy as I originally thought. Here is a revision with the function beginEnd to get it done. date key_column begin_dateend_date 1 123456 2013-01-01 2014-01-01 2 123456 2013-07-01 2014-07-01 3 789102 2012-03-01 2014-03-01 4 789102 2015-02-01 2016-02-01

Re: [R] How many digits are there in left of dot of 0.0001 ?

2015-02-26 Thread JS Huang
Hi, To get the number of digits to the right of decimal point: nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)+1) -1. The part (trunc(log10(max(1,trunc(abs(a)+1) is the number of digits to the left of decimal. At the end, subtract 1 for the decimal point.

Re: [R] Replace the value with 1 and 0

2015-02-25 Thread JS Huang
Hi, Here is an implementation. More data are added. An extra column hasRain is added instead of replacing column Amount. rain Year Month Day Amount 1 1950 1 10.0 2 1950 1 2 35.5 3 1950 1 3 17.8 4 1950 1 4 24.5 5 1950 1 5 12.3 6 1950 1

Re: [R] Simple Histogram

2015-02-20 Thread JS Huang
Hi, Your data may look like the following and named speed.txt in working directory. Then the cod follows the data. The graph is attached as speed.pdf. Speed 50 52 55 57 58 59 60 61 62 63 64 65 65 65 67 68 68 68 68 69 69 70 71 72 72 72 73 73 73 73 75 76 77 78 79 speed -

Re: [R] irregular sequence of events

2015-02-20 Thread JS Huang
Hi, Herr is one implementation with function named eventList. start [1] 5 13 21 start [1] 5 13 21 end [1] 10 16 27 x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 eventList function(start, end, x) { result - character(0) for (i in

Re: [R] How do I access a specific element of a multi-dimensional list

2015-02-20 Thread JS Huang
Hi, Jim's answer is neat. There is an issue on the result. All are characters even though some are numeric or logic. The following implementation retains the variable type. x [[1]] [1] 2 3 5 [[2]] [1] aa bb cc [[3]] [1] TRUE FALSE TRUE getFirst function(aList) { result - list()

Re: [R] About Read in .csv Files with Chinese Characters

2015-02-19 Thread JS Huang
Hi Nicholas, I am not sure how much the following link can help but at least it is related to your question. http://r.789695.n4.nabble.com/Reading-Chinese-Language-GB2312-Input-td4647581.html Good luck! JS -- View this message in context:

Re: [R] About Read in .csv Files with Chinese Characters

2015-02-19 Thread JS Huang
Hi, I tried your file in my Windows7 pc. It worked fine except the display of the variable name. R version 3.1.2 (2014-10-31) -- Pumpkin Helmet Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) Hello - read.csv(Hello.csv);Hello

Re: [R] Averaging column scores when participants vary in number of observations

2015-02-19 Thread JS Huang
Hi, Another implication: data1 Observation Participant.ID Video.Coder Score 1 A 1 Donald 4 2 B 1 Tracy 5 3 C 2 Donald 6 4 D 3 Sam 2 5 E

Re: [R] Numerical stability of eigenvalue and hessian matrix in R

2015-02-18 Thread JS Huang
Hi, Since all entries in your hessian matrix and grad vector are integers, I suggest you execute the following for mat assignment. mat - round(h_x(x),digits=0)*round(hess.h,digits=0) - round(grad(h_x, x),digits=0) %o% round(grad(h_x, x),digits=0) mat [,1] [,2] [,3]

Re: [R] Substituting elements in vector

2015-02-17 Thread JS Huang
Hi, Here is an implementation. my.vector [1] A B C D E F G vec1 [1] p q r s t vec2 [1] x y z final - as.character(unlist(sapply(my.vector,function(x) if(x==B){vec1}else{if(x==E){vec2}else{x}}))) final [1] A p q r s t C D x y z F G -- View this message in context:

Re: [R] Error when I attempt to create a list or a data frame

2015-02-17 Thread JS Huang
Hi, It appears that you used left double quotation marks and right double quotation marks in your vector for characters. The first lst assignment is copied from your post and indicates issues. I retyped with doulbe quotation mark and went through fine with the second lst assignment. Unicode

Re: [R] Re categorizing components of varaibles

2015-02-17 Thread JS Huang
Hi, Here I use the data.frame b as an example to show how it can be accomplished. The sixth column of b has values 6 through 996 by an increment of 10. The statement sapply(b[,6],function(x)if(x==6){0}else{if(x==996){2}else{1}}) assigns 0 to 6, 2 to 996 and 1 to the rest in column 6. In your

Re: [R] sd, mean with a frequency distribution matrix

2015-02-17 Thread JS Huang
Hi, Just learned another way to calculate sd for a frequency distribution matrix: p - matrix(c(10,3,20,4,30,5),ncol=2,byrow=TRUE) p [,1] [,2] [1,] 103 [2,] 204 [3,] 305 rep(p[,1],p[,2]) [1] 10 10 10 20 20 20 20 30 30 30 30 30 sd(rep(p[,1],p[,2])) [1] 8.348471 --

Re: [R] compute values by condition in DF by rownames

2015-02-17 Thread JS Huang
Hi, I like the aggregate version. Here is an implementation with sapply and apply: data X2 gbm_tcga lusc_tcga ucec_tcga_pub 1 gbm_tcga 1.0 0.14053719 -0.102847164 2 gbm_tcga 1.0 0.04413434 0.013568055 3 gbm_tcga 1.0

Re: [R] sd, mean with a frequency distribution matrix

2015-02-16 Thread JS Huang
Hi, For the second one, sqrt((sum(p[,1]^2*p[,2])-(sum(p[,1]*p[,2]))^2/sum(p[,2]))/(sum(p[,2])-1)), please refer to the following link for an example to explain how it works. http://www.lboro.ac.uk/media/wwwlboroacuk/content/mlsc/downloads/var_stand_deviat_group.pdf For the first one:

Re: [R] compute values by condition in DF by rownames

2015-02-16 Thread JS Huang
Hi, I hope that someone can provide a better way to implement it. This is my implementation. data X2 gbm_tcga lusc_tcga ucec_tcga_pub 1 gbm_tcga 1.0 0.14053719 -0.102847164 2 gbm_tcga 1.0 0.04413434 0.013568055 3 gbm_tcga

[R] split dataframe to several dataframes in R

2015-02-16 Thread js . huang
quote author='R help mailing list-2' Hi All,I have a dataframe called 'means' as shown below:iris1.csv - iris iris2.csv - iris names - c(iris1.csv, iris2.csv) dat - mget(names) lst4 - lapply(dat, function(x) apply(x[,-5], 2, mean)) # Build the new data frame means - as.data.frame(do.call(rbind,

Re: [R] Coverage probability for a Poisson parameter

2015-02-14 Thread JS Huang
Hi, In your function cover, lambda1 and lambda2 are used but not in the argument of the function. I suppose that you need to have lambda1 and lambda2 in the argument of the function cover, like function(lambda1, lambda2, n, significance.level). Give it a try. cover - function(lambda, n,

Re: [R] sd, mean with a frequency distribution matrix

2015-02-14 Thread JS Huang
Or if you want to perform the calculation without using sd: sqrt((sum(p[,1]^2*p[,2])-(sum(p[,1]*p[,2]))^2/sum(p[,2]))/(sum(p[,2])-1)) -- View this message in context: http://r.789695.n4.nabble.com/sd-mean-with-a-frequency-distribution-matrix-tp4703218p4703231.html Sent from the R help

Re: [R] Coverage probability for a Poisson parameter

2015-02-14 Thread JS Huang
Hi, Given the function cover, it's very likely that you will get 0 for both s1 and s1 with small value of lambda1 and lambda2. In that case the sum s will be 0. With s being 0, you will have issue with the expression in pi - s2/s and root - ((s2/s)*(1-s2/s)+k/(4*s))^(1/2). You need to take

Re: [R] Genrating Ordinal Responses in R

2015-02-14 Thread JS Huang
Hi, Here assume there are four elements in the ordinal set y and take a random sample of size 10 according to the cumulative distribution given, or probability distribution p below. y - c(levels = c(First, Second, Third, Fourth)) y levels1 levels2 levels3 levels4 First Second Third

Re: [R] Caleberating weights

2015-02-14 Thread JS Huang
Hi, It's not clear what wizi is in the constraint Sum(wizi) = Z. If you can provide some data, it may make the problem easier to understand. -- View this message in context: http://r.789695.n4.nabble.com/Caleberating-weights-tp4703226p4703245.html Sent from the R help mailing list archive

Re: [R] Coverage probability for a Poisson parameter

2015-02-14 Thread JS Huang
Hi, Some suggestion about the arguments of the function defined below. Since theta is calculated with the value of lambda1 and lambda2, there is no need to include theta in the argument. Or, your function can be defined as function(lambda1, lambda2, significance.level) cover -

Re: [R] lme: Can not find groupData object in a function could this be a scoping problem?

2015-02-14 Thread JS Huang
Hi, Unless you defined SS somewhere before you execute data - data.frame(group=c(rep(Cont,SS),rep(Exp,SS)), pre=pre,post=post), SS is not assigned. Maybe it is TS you intended? doit- function(TS,rho,premean,presd,RxEffect) { . . . # Prepare data frames for regression analyses. data -

Re: [R] sd, mean with a frequency distribution matrix

2015-02-13 Thread JS Huang
Hi, Try this. sd(unlist(sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2] -- View this message in context: http://r.789695.n4.nabble.com/sd-mean-with-a-frequency-distribution-matrix-tp4703218p4703220.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] SIMPLE question

2015-02-12 Thread JS Huang
Hi, Maybe the following link helps. http://r.789695.n4.nabble.com/box-cox-td4363944.html -- View this message in context: http://r.789695.n4.nabble.com/SIMPLE-question-tp4703176p4703180.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] plotting this data

2015-02-11 Thread JS Huang
Hi, Here is an implementation. The image is uploaded as Rplot02.png. gen - read.table(geno.txt,header=TRUE) gen Genotype E1 E2E3 E4 E5 E6 E7 E8 E9 E10 E11 E12 E13 E14 E15 E16 E17 E18 1 G1 0.79 2.11 6.21 0.56 4.06 2.13 5.61 0.20 3.32 3.01 5.12 0.77 0.78

Re: [R] factor levels numeric values

2015-02-11 Thread JS Huang
Hi, Suppose your data frame is called data and the name of the factor column is named tobeConverted. I have tried this and it worked. Hope this helps. as.numeric(as.character(data$tobeConverted)) -- View this message in context:

Re: [R] $ operator is invalid for atomic vectors

2015-02-11 Thread JS Huang
Hi, If x is a data frame, then x$getmean will try to get the vector named getmean in x. You put () after x$getmean. I think r is confused about it. It appears that you want to call a function named getmean(). -- View this message in context:

Re: [R] Nonlinear integer programming question

2015-02-11 Thread JS Huang
Hi Rebecca, It will be very helpful if you can provide a set of specific functions g(x), h(x) and m(x). -- View this message in context: http://r.789695.n4.nabble.com/Nonlinear-integer-programming-question-tp4703122p4703128.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] function that calculates using preceding records

2015-02-10 Thread JS Huang
Hi, Here is an implementation: data - read.table(tree.txt,header=TRUE,sep=,,stringsAsFactors=FALSE) data treecode yearrw d 1 TC149 2014NA8 2 TC149 2013 0.080 NA 3 TC149 2012 0.125 NA 4 TC149 2011 0.120 NA 5 TC149 2010 0.125 NA 6 TC148

Re: [R] How to solve this complex equation

2015-02-10 Thread JS Huang
Hi, The solution x to the equation is the parmater lambda = x/2 of a Poisson distribution with probability of 0.05 for the number of occurrence 2 or fewer. -- View this message in context: http://r.789695.n4.nabble.com/How-to-solve-this-complex-equation-tp4702997p4703070.html Sent from the

Re: [R] assignment of categorical variables to matrix/table

2015-02-10 Thread JS Huang
Hi, Try this with three category variables. A [1] Baby Kid Teenager AdultMature B [1] Male Female C [1] PST MST CST EST expand.grid(Age=A, Sex=B, Zone=C) AgeSex Zone 1 Baby Male PST 2 Kid Male PST 3 Teenager Male PST 4 Adult Male PST

Re: [R] Making a histogram

2015-02-06 Thread JS Huang
Hi, For your error object 't1971' not found can be corrected by subsetting. t2001 can be done similarly with 2001 replacing 1971. t1971 - data[year==1971,] t1971 VOT year Consonant 1 67 1971 k 2 127 1971 k 3 79 1971 k 4 150 1971 k 5 53 1971

Re: [R] Coverage probability for a Poisson parameter

2015-02-06 Thread JS Huang
Hi, After some thought, I found the treatment of sample mean equal 0 was not appropriate. I modified the function likelihood.ratio.test.Poisson. resulting.matrix now has 0.0512 as the average of type I error. function(lambda, sample.size, significance.level) { reject - 0 sample.mean -

Re: [R] Plotting dataset

2015-02-06 Thread JS Huang
Hi, Here is my implementation. Modify the data as follows so that it can be read with read.table and save as rHelp_20151206.txt under working directory. female male vowel language 391 339 i W.Apache 561 512 e W.Apache 826 670 a W.Apache 453 427 o W.Apache 358 291 i CA.English 454 406 e

Re: [R] Plotting dataset

2015-02-06 Thread JS Huang
Hi, Forgot to mention that ggplot2 needs to be installed: install.packages(ggplot2) Warning in install.packages : downloaded length 227 != reported length 227 trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.1/ggplot2_1.0.0.zip' Content type 'application/zip' length 2675344 bytes

Re: [R] Rotate array by 90°

2015-02-05 Thread JS Huang
Hi, Here is an implementation. rot90 function(a,times=1) { row - dim(a)[1] col - dim(a)[2] dep - dim(a)[3] if (times %% 2 == 1){t - row; row - col; col - t} tempA - array(NA, c(row,col,dep)) for (i in 1:dep) { temp - a[,,i] for (j in 1:times) { temp -

Re: [R] Get 10%, 50% and 90% Quantile of data samples with probability

2015-02-04 Thread JS Huang
Hi, To complete the last part: dist - function(x) + { + return(c(rep(x[1],15),rep(x[2],15),rep(x[3],25),rep(x[4],20),rep(x[5],25))) + } x - dist(c(2,3,2,5.3,7.3)) x [1] 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 [29]

Re: [R] collapse a list of dataframes

2015-02-03 Thread JS Huang
Hi, The following worked. data.frame(rbind(as.matrix(data.frame(a=1:3,b=letters[1:3])),as.matrix(data.frame(x=1:5,b=LETTERS[1:5] a b 1 1 a 2 2 b 3 3 c 4 1 A 5 2 B 6 3 C 7 4 D 8 5 E -- View this message in context:

Re: [R] Get 10%, 50% and 90% Quantile of data samples with probability

2015-02-03 Thread JS Huang
Hi, For you first task and use your first row of data as an example. Define a function named dist to take care of the probability for each group. dist - function(x) + { + return(c(rep(x[1],15),rep(x[2],15),rep(x[3],25),rep(x[4],20),rep(x[5],25))) + } plot(ecdf(dist(c(2,3,2,5.3,7.3

Re: [R] help plotting my data

2015-02-02 Thread JS Huang
Hi, I hope the following works for you. The plot is: Rplot.png http://r.789695.n4.nabble.com/file/n4702679/Rplot.png data - read.table(rHelp_20150202.txt,header=TRUE) order.data - order(data$counts,decreasing=TRUE) order.data [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Re: [R] Dropping time series observations

2015-02-02 Thread JS Huang
Hi, I have a data frame named data and with the statement data[5:length(data[[1]]),] I can get row 5 to the end of the data. data role counts 1 Agent220 2 Theme169 3 Patient 67 4 Location 41 5 Destination 32 6

Re: [R] Coverage probability for a Poisson parameter

2015-01-30 Thread JS Huang
Hi, Roughly reading the code, I find this statement phat - x / m is probably incorrect since this will give you the set of 100 observed x values /100. I redefine the function cover with three inputs: lambda for the parameter of the poisson distribution, sample.size and

Re: [R] quetion about matrix compute

2015-01-30 Thread JS Huang
Hi, Here is my implementation. Hope this helps. b [1] 1 2 3 4 5 c [1] 1 2 1 3 5 4 sapply(b,function(x)ifelse(x==c,1,0)) [,1] [,2] [,3] [,4] [,5] [1,]10000 [2,]01000 [3,]10000 [4,]00100 [5,]000

Re: [R] Coverage probability for a Poisson parameter

2015-01-30 Thread JS Huang
Hi, My first question is what is your n when you say fixed n. I assume the lambda is the mean of the poisson distribution that you want to take sample from. Another question is about the sample size. It does not make too much sense to make a sample of size 1. Let's assume that you want