[R] counting characters starting point
I have three character strings represented below as seq1, seq2, and seq3. Each string has a reference character different from the other. Thus, for seq1, the reference character is U, seq2, S (3rd S from left where A is leftmost character) and for seq3 Y. seq1 = PQRTUWXYseq2 = AQSDSSDHRSseq3 = EEZYJKFFBHO I wish to generate a 3 by 26 matrix where 3 represent seq1, seq2, seq3 and 26 the letters of the alphabet in order. A matrix entry should correspond to the number of characters from the reference character to the said character. We would consider characters to the left of the reference character to have a negative value and characters to the right a positive value. In addition, if a character appears more than once, we would consider the lowest of the counts. The output for seq1, seq2, seq3 shown below where 99 indicates missing. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 -4 -3 -2 99 0 99 99 1 2 3 99 -5 99 99 -2 99 99 99 1 99 99 99 99 99 99 99 -4 99 2 0 99 99 99 99 99 99 99 99 5 99 99 2 3 99 6 99 1 2 99 99 99 7 99 99 99 99 99 99 99 99 99 0 -1 Could someone help me with a code on how to implement this.Thank you in advance for your helpJN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Plot numeric and categorical variable
My data consists of numeric (yy) and categorical (xx) variables, as shown below. > xx = c(rep("C", 5), rep("D",5))> xx [1] "C" "C" "C" "C" "C" "D" "D" "D" "D" > "D"> yy = rnorm(10, 0, 4)> yy [1] 2.7219346 -3.7142481 6.8716534 -0.9352463 > 0.4901249 3.8113247 [7] -2.6602041 1.7714471 4.7298233 0.8848188 > xx1 = as.integer(as.factor(xx))> plot(xx1, yy, ylim = c(-13.5, 4), col="blue") I have attached the plot above as plot1 I wish to generate a scatter plot of the data such that instead of 1 it prints C, and instead of 2, it prints D on the x- axis (see plot1 attached). How could I possibly go about this, let me know. Any help will be greatly appreciated. Thanks in advance for your help,JN __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] plot mixed variables
My data consists of a numeric (yy) variable and a categorical (xx) variable, as shown below. xx = c(rep("C", 5), rep("D",5)) yy = rnorm(10, 0, 4) xx1 = as.integer(as.factor(xx)) plot(xx1, yy, ylim = c(-13.5, 4), col="blue") I wish to generate a scatter plot of the data such that instead of 1 it prints C, and instead of 2, it prints D on the x- axis. Could someone help me with an R code on how to go about this. Thank you in advance for your help, JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Convert full matrix back to lower triangular matrix
Given a vector;> ab = seq(0.5,1, by=0.1)> ab[1] 0.5 0.6 0.7 0.8 0.9 1.0 The euclidean distance between the vector elements is given by the lower triangular matrix > dd1 = dist(ab,"euclidean")> dd1 1 2 3 4 52 0.1 3 0.2 0.1 4 0.3 0.2 0.1 5 0.4 0.3 0.2 0.1 6 0.5 0.4 0.3 0.2 0.1 Convert the lower triangular matrix to a full matrix> ddm = as.matrix(dd1)> ddm 1 2 3 4 5 61 0.0 0.1 0.2 0.3 0.4 0.52 0.1 0.0 0.1 0.2 0.3 0.43 0.2 0.1 0.0 0.1 0.2 0.34 0.3 0.2 0.1 0.0 0.1 0.25 0.4 0.3 0.2 0.1 0.0 0.16 0.5 0.4 0.3 0.2 0.1 0.0 I would be grateful if someone could provide me with a code to convert ddm to the lower triangular matrix as before i.e. dd1 above. Your help will be greatly appreciated.JN NB: I apologize for the poor output of the previous email. Thanks for your understanding. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Convert back to lower triangular matrix
Given a vector;> ab = seq(0.5,1, by=0.1)> ab[1] 0.5 0.6 0.7 0.8 0.9 1.0 The euclidean distance between the vector elements is given by the lower triangular matrix > dd1 = dist(ab,"euclidean")> dd1 1 2 3 4 52 0.1 3 0.2 0.1 4 0.3 0.2 0.1 5 0.4 0.3 0.2 0.1 6 0.5 0.4 0.3 0.2 0.1 Convert the lower triangular matrix to a full matrix> ddm = as.matrix(dd1)> ddm 1 2 3 4 5 61 0.0 0.1 0.2 0.3 0.4 0.52 0.1 0.0 0.1 0.2 0.3 0.43 0.2 0.1 0.0 0.1 0.2 0.34 0.3 0.2 0.1 0.0 0.1 0.25 0.4 0.3 0.2 0.1 0.0 0.16 0.5 0.4 0.3 0.2 0.1 0.0 I would be grateful if someone could provide me with a code to convert ddm to the lower triangular matrix as before i.e. dd1 above. Your help will be greatly appreciated.JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Chunk of code
MATLAB chunk of code for jj = 1:numOfAA curAAPosInSeqIndex = aaPosInSeqIndex{1,jj}; for kk = 1:K p_c_a_z_given_x_contribution(1,jj,kk) = sum(sum(p_c_a_z_given_m_x_permuted(:,:,kk) .* curAAPosInSeqIndex, 1),2); % 1 by numOfAA by K end end aaPosInSeqIndex is a list of size 24, with each element a vector. p_c_a_z_given_m_x_permuted is an array of size N=1655 by J=39 by K=151 and p_c_a_z_given_x_contribution is an array of size 1 by numOfAA = 24 by K=151 R equivalent:app = t(sapply(1:numOfAA, function(xx) (t(sapply(1:K, function(x) colSums(p_c_a_z_given_m_x_permuted[,,x]* aaPosInSeqIndex[[xx]])))[,3]))) p_c_a_z_given_x_contribution = app I would be very grateful if someone could look at the R code and let me know if there is something missing or if there is a better way of getting the exact MATLAB output uing R code i.e. could someone write a code that would do what the MATLAB chunk of code above is doing. The R code does not output the same MATLAB output. Thanks in advance for your help.JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Split a list
I have a list of dataframes i.e. each list element is a dataframe with three columns and differing number of rows. The third column takes on only two values. I wish to split the list into two sublists based on the value of the third column of the list element. Second issue with lists as well. I would like to reduce each of the sublist based on the range of the second column, i.e. if the range of the second column is greater than twenty for example keep the list element. Could someone help me with a code to implement these two issues. Thanks in advance for your help, JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] bootstrapping nonlinear mixed effects models
In order to bootstrap nonlinear regression, the following code works. library(nlme) data(Soybean) fm1.nls <- nls(weight ~ SSlogis(Time, a, b, c), data=Soybean) summary(fm1.nls) bstat <- function(A, indices) { mboot <- nls(weight ~ SSlogis(Time, a, b, c), data=Soybean[indices, ]) return(coef(mboot)) } require(boot) boot1 <- boot(Soybean, bstat, 2000) boot1 For nonlinear mixed effects models it doesn't work. > stat.nlme <- function(A, indices) { + boot.nlme <- nlme(Gdiff/0.5 ~ SSlogis(tmmean,a,b,c), + fixed=list(a+b+c~1), + random = list(a+b+c~1), data = Soybean[indices,]) + return(fixef(boot.nlme)) + } > > require(boot) > bb <- boot(Soybean, stat.nlme, 2000) Error in parse(text = paste("~", paste(nVal, collapse = "/"))) : unexpected end of input in "~ " Could someone help me with fixing the code, thanks in advance for the help. JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Moving non-zero matrix entries to different locations on the matrix
I have a matrix: C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 R1 0 0 0 0 0 5 0 0 0 0 0 0 R2 0 0 0 0 0 3 0 0 0 0 0 0 R3 0 0 0 0 0 0 4 0 0 0 0 0 R4 0 0 0 0 0 0 2 0 0 0 0 0 R5 1 0 0 0 0 0 0 0 0 0 0 0 R6 8 0 0 0 0 0 0 0 0 0 0 0 R7 0 15 6 0 3 0 0 0 0 0 0 0 R8 0 9 7 18 17 0 0 0 0 0 0 44 R9 0 0 0 23 0 0 0 0 0 0 0 52 R10 0 0 0 0 0 0 0 0 0 0 19 0 where most of the entries are zeros. I wish to move some of the non -zero entries to locations where there are not as many non-zero entries giving a matrix like the one below: C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 R1 0 0 0 0 0 0 0 0 0 0 0 0 R2 0 0 0 0 0 0 0 0 0 0 0 0 R3 0 0 0 0 0 0 0 0 0 0 0 0 R4 0 0 0 0 0 0 0 0 0 0 0 0 R5 0 0 0 0 0 0 0 0 0 0 0 0 R6 0 0 0 0 0 0 0 0 0 0 0 0 R7 1 15 6 18 3 5 4 0 0 0 0 44 R8 8 9 7 23 17 3 2 0 0 0 0 52 R9 0 0 0 0 0 0 0 0 0 0 0 0 R10 0 0 0 0 0 0 0 0 0 0 19 0 R7 and R8 had more non-zero entries than all the other rows so, the non-zero entries had to be moved to R7 and R8. Can any one write a simple function that can implement this idea. Thanks in advance for your help, JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to get "mypkg-manual.pdf"
I am building a package say mypkg. Five months ago, when I built the package I got the mypkg manual in pdf format. Today, after making updates, I build the same package, same name, and steps; unfortunately I do not get the manual in pdf format. Rather I get the following message: cd: can't cd to /cygdrive/c/Documents saving output to 'mypkg-manual.pdf' ...Done Could any one help me on how to get the "mypkg-manual.pdf". Thank you in advance for your help. JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Handling repeated values differently
mat is a matrix with X and Y values. > mat X Y [1,] 56 20 [2,] 56 21 [3,] 2 50 [4,] 3 46 [5,] 18 77 [6,] 57 12 [7,] 57 36 [8,] 95 45 [9,] 65 23 [10,] 33 25 [11,] 33 98 [12,] 63 96 [13,] 66 75 [14,] 99 54 [15,] 78 65 [16,] 75 69 [17,] 54 68 [18,] 54 67 [19,] 0 22 [20,] 14 74 [21,] 15 52 [22,] 46 10 [23,] 6 20 [24,] 6 30 As you can see, some of the X values repeat. I wish to maintain only one of the X values that is repeated, however, I would like to have the sum of the corresponding Y values. For example, the X value of 6 repeats, with values 20 and 30. In the output, I would like to have 6 as X value, with the corresponding Y value of 50 (20+30). The output from mat above should be: > mat1 X1Y1 [1,] 56 41.0 [2,] 2 50.0 [3,] 3 46.0 [4,] 18 77.0 [5,] 57 48.0 [6,] 95 45.0 [7,] 65 23.0 [8,] 33 123.0 [9,] 63 96.0 [10,] 66 75.0 [11,] 99 54.0 [12,] 78 65.0 [13,] 75 69.0 [14,] 54 67.5 [15,] 0 22.0 [16,] 14 74.0 [17,] 15 52.0 [18,] 46 10.0 [19,] 6 50.0 I have not been able to come up with a code, I was wondering if someone can help me with a code to handle this. Thank you in advance for your help, JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Selecting rows of a matrix based on some condition on the columns
The data set consists of two sets of matrices, as labelled by the columns, T's and C's. > xy xT1T2T3T4T5C1C2C3C4C5 [1,] 50 0.00 0.00 33.75 0.00 0.00 0.00 36.76 0.00 35.26 0.00 [2,] 13 34.41 0.00 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [3,] 14 35.85 0.00 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [4,] 33 34.56 0.00 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 [5,] 66 36.38 37.42 0.00 32.47 34.05 0.00 0.00 0.00 0.00 0.00 [6,] 22 0.00 0.00 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [7,] 25 0.00 0.00 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 [8,] 9 33.63 0.00 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 [9,] 87 35.22 0.00 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 [10,] 99 0.00 0.00 34.94 0.00 0.00 33.54 0.00 34.39 34.54 0.00 First, I wish to select for each row, all columns that have at least a T and a C. Based on the code below, I got exactly what I need. > t1all <- apply(xy,1,function(x) > any((x[2]>0|x[3]>0|x[4]>0|x[5]>0|x[6]>0)&(x[7]>0 |x[8]>0 > |x[9]>0|x[10]>0|x[11]>0))) > mat.t1all <- xy[t1all,] > mat.t1all xT1 T2T3T4T5C1C2C3C4C5 [1,] 50 0.00 0 33.75 0.00 0.00 0.00 36.76 0.00 35.26 0.00 [2,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [3,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [4,] 33 34.56 0 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 [5,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [6,] 25 0.00 0 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 [7,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 [8,] 87 35.22 0 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 [9,] 99 0.00 0 34.94 0.00 0.00 33.54 0.00 34.39 34.54 0.00 Then, I need the rows for which there are at least two T's and two C's. Using a similar code as above, I get the following output: > t2all <- apply(xy,1,function(x) any(((x[2]>0&x[3]>0)|(x[2]>0&x[4]>0)| + (x[2]>0&x[5]>0)|(x[2]>0&x[6]>0)|(x[3]>0&x[4]>0)|(x[3]>0&x[5]>0)| + (x[3]>0&x[6]>0)|(x[4]>0&x[5]>0)|(x[4]>0&x[6]>0)|(x[5]>0&x[6]>0)) + + &(( (x[7]>0&x[8]>0)|(x[7]>0&x[9]>0)|(x[7]>0&x[10]>0)|(x[7]>0&x[11]>0)| + (x[8]>0&x[9]>0)|(x[8]>0&x[10]>0)|(x[8]>0&x[11]>0)|(x[9]>0&x[10]>0)| + (x[9]>0&x[11]>0)|(x[10]>0&x[11]>0) > > mat.t2all <- xy[t2all,] > mat.t2all xT1 T2T3T4T5C1C2C3C4C5 [1,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [2,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [3,] 33 34.56 0 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 [4,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [5,] 25 0.00 0 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 [6,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 [7,] 87 35.22 0 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 For three T's and three C's, I got > t3all <- apply(xy,1,function(x) any(( (x[2]>0&x[3]>0&x[4]>0)| + (x[2]>0&x[3]>0&x[5]>0)|(x[2]>0&x[3]>0&x[6]>0)|(x[2]>0&x[4]>0&x[5]>0)| + (x[2]>0&x[4]>0&x[6])|(x[2]>0&x[5]>0&x[6]>0)| + (x[3]>0&x[4]>0&x[5]>0)|(x[3]>0&x[4]>0&x[6]>0)| + (x[4]>0&x[5]>0&x[6]>0) ) + + &( (x[7]>0&x[8]>0&x[9]>0)| + (x[7]>0&x[8]>0&x[10]>0)|(x[7]>0&x[8]>0&x[11]>0)|(x[7]>0&x[9]>0&x[10]>0)| + (x[7]>0&x[9]>0&x[11])|(x[7]>0&x[10]>0&x[11]>0)| + (x[8]>0&x[9]>0&x[10]>0)|(x[8]>0&x[9]>0&x[11]>0)| + (x[9]>0&x[10]>0&x[11]>0) ) )) > > mat.t3all <- xy[t3all,] > mat.t3all xT1 T2T3T4T5C1C2C3C4C5 [1,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [2,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [3,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [4,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 Can someone help me with a better, and more efficient code that will handle this, thank you in advance for your help. JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Reducing a matrix
I wish to rearrange the matrix, df, such that all there are not repeated x values. Particularly, for each value of x that is reated, the corresponded y value should fall under the appropriate column. For example, the x value 3 appears 4 times under the different columns of y, i.e. y1,y2,y3,y4. The output should be such that for the lone value of 3 selected for x, the corresponding row entries with be 7 under column y1, 16 under column y2, 12 under column y3 and 18 under column y4. This should work for the other rows of x with repeated values. df x y1 y2 y3 y4 1 3 7 NA NA NA 2 3 NA 16 NA NA 3 3 NA NA 12 NA 4 3 NA NA NA 18 5 6 8 NA NA NA 6 10 NA NA 2 NA 7 10 NA 11 NA NA 8 14 NA NA NA 8 9 14 NA 9 NA NA 10 15 NA NA NA 11 11 50 NA NA 13 NA 12 50 20 NA NA NA The output should be: x y1 y2 y3 y4 1 3 7 16 12 18 2 6 8 NA NA NA 3 10 NA 11 2 NA 4 14 NA 9 NA 8 5 15 NA NA NA 11 6 50 20 NA 13 NA Can any write for me a code that would produce these results. Thank you in advance for your help. JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Rearranging entries in a matrix
I have a matrix, called data. I used the code below to rearrange the data such that the first column remains the same, but the y value falls under either columns 2, 3 or 4, depending on the value of z. If z=1 for example, then the value of y will fall under column 2, if z=2, the value of y falls under column 3, and so on. data x y z [1,] 50 13 1 [2,] 14 8 2 [3,] 3 7 3 [4,] 4 16 1 [5,] 6 8 2 [6,] 10 2 3 [7,] 15 11 1 [8,] 14 9 3 > data1 <- data.frame(matrix(0, length(x), ncol(data)+1)) > colnames(data1) <- c("x","y1","y2","y3") > data1$x <- data[,1] > data1$y1 <- apply(data, 1, function(x) (ifelse(x[3]==1,x[2],NA))) > data1$y2 <- apply(data, 1, function(x) (ifelse(x[3]==2,x[2],NA))) > data1$y3 <- apply(data, 1, function(x) (ifelse(x[3]==3,x[2],NA))) > data1 x y1 y2 y3 1 50 13 NA NA 2 14 NA 8 NA 3 3 NA NA 7 4 4 16 NA NA 5 6 NA 8 NA 6 10 NA NA 2 7 15 11 NA NA 8 14 NA NA 9 I used the apply function (three times) to generate the output below, which is as I wish to have it. Can any one write for me a for loop that would produce the same results. Thanks in advance. JN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R CMD check in Vista
I used R CMD check in windows vista, and it gives me the message 'sh' is not recognized as an internal or external command. How do I get around this, your help would be highly appreciated. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Bootstrap pvalues
Hi, How do I generate bootstrap p-values for goodness of fit test. Thank you in advance for your help, Juliet [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.