Re: [R] problem with abline and lines
Hi! You forgot to invert the slope: a perpendicular of 1/sqrt(2) should be -sqrt(2). Also, you should add asp=1 in the plot command to lock the aspect ratio, otherwise the scale of both X and Y may be different according to the size of the window: plot(x=c(-1, 1), y=c(-1, 1),asp=1); abline(a=0, b=1/sqrt(2)); abline(a=0, b=-sqrt(2)) Miguel On Mon, Mar 22, 2010 at 11:13 AM, Martin Ivanov tra...@abv.bg wrote: Dear R users, I need to plot to perpendicular straight lines. However, although I set the coefficients so that the lines are perpendicular, they do not look to be so in the plot. Here is a minimal working example: plot(x=c(-1, 1), y=c(-1, 1)); abline(a=0, b=1/sqrt(2)); abline(a=0, b=-1/sqrt(2)) Please tell me if the same problem is valid by you. I am running R-2.10.1 on Linux. Is there a way out of this? Regards, Martin __ 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. [[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.
Re: [R] remove substring from each string vector component
Hi! It's just this easy: x=gsub(\t,,x) For more complex things, it's worth learning some regular expressions syntax. Miguel On Tue, Mar 16, 2010 at 2:50 PM, arnaud chozo arnaud.ch...@gmail.comwrote: Hi all, I have a string vector like that: x=c(1\t\t, 2, 3\t\t\t) I need to remove all the occurrences of \t, in order to obtain: x = 1 2 3 I'm trying to use the function substring2, and it works for each component, for example: substring2(x[1], \t) = gives x = 1 23\t\t\t I'd like to apply this function to each component and I tried the following: myfun = function(x, i) { substring2(x[i], \t) = } lapply(x, myfun) which gives x= I know that I'm wrong somewhere using lapply, but I can't fix it. Thanks in advance, Arnaud Chozo [[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. [[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.
Re: [R] for() loop
Hi, I usually use aggregate() for this: aggregate(datjan,list(datjan[,4]),sum) with the advantage that you can use any other aggregation function (mean, var and so on...). See help. Miguel On Sun, Mar 14, 2010 at 8:49 PM, Schmidt Martin m.schm...@students.unibe.ch wrote: Hello I'm working with R since a few month and have still many trivial questions - I guess! Here is what I want: I have this matrix: dim(datjan) [1] 899 4 The first 10 rows looks as follows: datjan[1:10,] V1 V2 V3 V4 1 1961 1 1 24 2 1961 1 2 24 3 1961 1 3 24 4 1961 1 4 24 5 1961 1 5 24 6 1961 1 6 27 7 1961 1 7 27 8 1961 1 8 27 9 1961 1 9 27 10 1961 1 10 27 I tried now to create a for() loop, which gives me the sum of the 30 different classes (1:30!) in [,4]. for(i in 1:30){ sum(datjan[,4]==i) } R is then actually calculating the sum of i which certainly doesn't exist and results in a 0 value t1-sum(datjan[,4]==1) t2-sum(datjan[,4]==2) .until '30' This way its working, but I won't find a end by doing all this by hand, because there are many other matrix waiting. So, how can I make work that loop?? thanks for helping me __ 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. [[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.
Re: [R] for() loop
Sorry, I missed the [,4] : aggregate(datjan[,4],by=list(datjan[,4]),sum) Miguel [[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.
Re: [R] storing matrix(variables) in loop
Just in case... b=array(NA,c(3,3,3,4))# that means b[matrix-row,matrix-col,i,j] for (i in 1:3){ for (j in 1:4){ b[,,i,j]=matrix(runif(1),3,3) } } b (I think there are better ways to do this anyway...) Miguel [[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.
Re: [R] storing matrix(variables) in loop
Yeah, that sounds inefficient to me also. I think you'd be better off using multidimensional arrays instead of lists, since all your values are numeric. See ?array. Miguel 2010/3/15 Márcio Resende mresende...@yahoo.com.br Hello R-helpers, I have the following code that works well, b -list() for (i in 1:3){ a - matrix(runif(1),3,3) b[[i]] - a } b however, I need to do something similar with two loops and I was looking for something that would look like b - list() for (i in 1:3){ for (j in 1:4){ a - matrix(runif(1),3,3) b[[i,j]] - a #but this doesn´t work } } Anyway, I wanted b to loop like [[i=1, j=1]] [[i=1, j=2]] (...) a[i=1, j=1] a[i=1,j=2] (...) [[i = 2, j=1]](...) a[i = 2, j = 1] (...) (...) Can anybody help me? Thanks -- View this message in context: http://n4.nabble.com/storing-matrix-variables-in-loop-tp1593461p1593461.html Sent from the R help mailing list archive at Nabble.com. __ 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. [[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.
Re: [R] Time in minutes
Hi! That should do it: yourtime$min+yourtime$hour*60 In case your object is of class POSIXlt. Otherwise, convert it with as.POSIXlt first. Miguel On Mon, Mar 15, 2010 at 3:57 PM, Carlos Nader tamanduabande...@gmail.comwrote: Hi there! I have some data in POSIXlt format: 2009-07-18 5:53:00 2008-11-23 7:27:00 2008-11-24 5:25:00 and would like to extract information about only the minutes of the day, like: 5:53 -- 353 minutes 7:27 -- 447 minutes can you help me? Carlos Nader [[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. [[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.
Re: [R] Creating named lists
Hi See if this function works for you (I didn't properly test it...): nlist=function(...) { a=list(...); names(a)=as.character(match.call()[2:(length(a)+1)]) return(a); } Ex: a=1:3 b=matrix(1:10,nc=2) nlist(a,b) $a [1] 1 2 3 $b [,1] [,2] [1,]16 [2,]27 [3,]38 [4,]49 [5,]5 10 [[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.
Re: [R] Return one value, print another
Hmm... do something like that, no need to change the global option (I used a named vector instead of a list, it's more convenient): eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - c(xbar, sdx) names(value) - c(Mean of X, SD of X) print(round(value,digits)); return(invisible(value))} On Fri, Mar 12, 2010 at 8:14 AM, Joshua Wiley jwiley.ps...@gmail.comwrote: Dear R users, I am stuck trying to figure out how to make a function return one value and print another. Here is an example function: ## eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - list(xbar, sdx) names(value) - c(Mean of X, SD of X) return(value)} ## My current solution has been to round the variables before putting them into the list. Since it can go up to 22 digits, this is fine for my basic needs. However, my goal is for assignments to have full precision, but the screen printout to be rounded to digits. I have looked through ?return ?cat ?print. Can anyone suggest where I can learn how to do this (help pages, books, etc.)? Thanks in advance, Josh -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ __ 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. [[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.
Re: [R] Return one value, print another
Yeah that's right; in that case implementing the print.myclass as you say would be the best option. Miguel On Fri, Mar 12, 2010 at 2:17 PM, Dieter Menne dieter.me...@menne-biomed.dewrote: Miguel Porto wrote: Hmm... do something like that, no need to change the global option (I used a named vector instead of a list, it's more convenient): eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - c(xbar, sdx) names(value) - c(Mean of X, SD of X) print(round(value,digits)); return(invisible(value))} Would be ok, but Josh explicitly had no print in the function, so one could assume that he wanted full control. Dieter -- View this message in context: http://n4.nabble.com/Return-one-value-print-another-tp1590248p1590563.html Sent from the R help mailing list archive at Nabble.com. __ 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. [[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.
Re: [R] Comparing matrices
Also look at ?any and ?all Very handy functions. Miguel On Thu, Mar 11, 2010 at 12:37 PM, Esmail esmail...@gmail.com wrote: Hello all, I have two matrices, pop and pop2, each the same number of rows and columns that I want to compare for equality. I am concerned about efficiency in this operation. I've tried a few things without success so far. Doing something simple like: if (pop==pop2) { cat('equal') } else { cat('NOT equal') } results in the warning: 1: In if (pop == pop2) { : the condition has length 1 and only the first element will be used so it seems to look only at the first element. print(pop==pop2) gives me a listing of TRUE/FALSE values for each element. I am really only looking for a single TRUE or FALSE value to tell me if the two populations (ie pop and pop2) are the same or different. In my application there will be about 200 columns and 50 rows and this comparison will happen very frequently (possibly a few thousand times), so I am concerned about efficiency. I am appending the code I used to generate my matrices and some things I tried (mentioned above) and also the output get so far. FWIW, Linux environment, R 2.10.1. Thanks, Esmail ps: Am I correct that if I do the assignment pop2 = pop I create a totally separate instance/(deep)copy of the data? I tried a few tests that seem to confirm this, but I'd rather be sure. --- code # create a binary vector of size len create_bin_Chromosome - function(len) { sample(0:1, len, replace=T) } # create popsize members, each of length len create_pop_2 - function(popsize, len) { datasize=len*popsize npop - matrix(0, popsize, len, byrow=T) for(i in 1:popsize) npop[i,] = create_bin_Chromosome(len) npop } POP_SIZE = 3 LEN = 8 pop = create_pop_2(POP_SIZE, LEN) pop2 = pop print(pop==pop2) if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') } print(pop) print(pop2) pop[2,5] = 99 pop2[3,3] = 77 print(pop==pop2) if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') } print(pop) print(pop2) -- output --- source('popc.R') [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE equal [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]01010110 [2,]01100000 [3,]01001011 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]01010110 [2,]01100000 [3,]01001011 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [3,] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE equal [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]01010110 [2,]0110 99000 [3,]01001011 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]01010110 [2,]01100000 [3,]01 7701011 Warning messages: 1: In if (pop == pop2) { : the condition has length 1 and only the first element will be used 2: In if (pop == pop2) { : the condition has length 1 and only the first element will be used __ 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. [[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.
Re: [R] Can't convert list to matrix properly
Hi, Is this what you want? matrix(unlist(myList),nr=1) Miguel On Thu, Mar 11, 2010 at 4:03 PM, anna lippelann...@hotmail.com wrote: Hi guys, here is a list of names that I have: MyList: myList-list(A, B,C,D) myList [[1]] [1] A [[2]] [1] B [[3]] [1] C [[4]] [1] D I want to turn this list into a matrix of 1 row and 4 columns with those four components (A, B, C, D) so here is what I do: myDataFrame - data.frame(myList) and here is how myDataFrame looks like: myDataFrame X.A. X.B. X.C. X.D. 1ABCD Until that point everything is ok because if I want to retrieve one element I make myDataFrame[[1]] [1] A Levels: A But when I try to convert into matrix by doing this: myMatrix - data.matrix(myDataFrame) I get this: myMatrix X.A. X.B. X.C. X.D. [1,]1111 He just keeps the names and delete the components...What am I doing wrong? Thanks in advance! - Anna Lippel -- View this message in context: http://n4.nabble.com/Can-t-convert-list-to-matrix-properly-tp1589187p1589187.html Sent from the R help mailing list archive at Nabble.com. __ 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. [[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.
Re: [R] Can't convert list to matrix properly
see for yourself - AFAIK it'll just concatenate eveverything which is atomic into a vector, thus losing all the structure associated. Miguel On Thu, Mar 11, 2010 at 4:14 PM, anna lippelann...@hotmail.com wrote: Yes, definitely! so unlist() turns the list components into a vector? What if the component are vectors, matrices etc? Thank you Miguel! - Anna Lippel -- View this message in context: http://n4.nabble.com/Can-t-convert-list-to-matrix-properly-tp1589187p1589209.html Sent from the R help mailing list archive at Nabble.com. __ 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. [[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.
Re: [R] Joining elements of an array into a single element
Hello, If you do this after the for loop, you'll get what you want: paste(tre,collapse=) (you can use whatever separator you want in the collapse argument) But you don't even need the for loop, just do this instead of the for loop: paste(con(,p, == ,c,, ,zest,, ,sep=,collapse=) Best, Miguel On Wed, Mar 10, 2010 at 11:44 AM, veg...@yahoo.com wrote: Dear forum: I've been trying like for hours but I cannot solve this. I wonder if you could give me a hand. I would like to combine the following elements of an array which I must generate in order to get the syntax I need for another software, lets say: c-c(1:7) zest-c(12,34,45,132,56,23,6) p-[foresnat2] for(i in 1:7){tre[i]-paste(con(,p, == ,c[i],, ,zest[i],, ,sep=)} [1] con([foresnat2] == 1, 12, [1] con([foresnat2] == 2, 34, [1] con([foresnat2] == 3, 45, [1] con([foresnat2] == 4, 132, [1] con([foresnat2] == 5, 56, [1] con([foresnat2] == 6, 23, [1] con([foresnat2] == 7, 6, I would like the elements of the object tre to go one after another without space (or with a character I design like for example * or + )like this: [1] con([foresnat2] == 1, 12, con([foresnat2] == 2, 34, con([foresnat2] == 3, 45, con... etc I understand it could be done with the function paste invoking each of the elements, but lets say they are not only 7 but 20 or 40 elements... Is it possible? What can I do? Thank you very much in advance. Luis Vega PD: Just in case: For the other software language i was talking about, the usage of con is the same as ifelse in R, I'm trying to generate a large embed ifelse(..., ..., ifelse(...,...,ifelse( entry. but with what I'm requiring lines above should be enough I think. [[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. [[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.
Re: [R] (box-) plot annotation: italic within paste?
Hello, Try this way (not sure if it's the best way, but it works): boxplot(x[,i], main=substitute(expression(paste(a, ,italic(b), ,c)),list(a=mainlabel1,b=predictor[i],c=mainlabel2)), ylab=paste(ylabel),cex.lab=cexalabel,cex.main=cexmlabel,cex.axis=1.5) Best, Miguel On Mon, Mar 8, 2010 at 2:27 PM, Bernd Panassiti bernd.panass...@rivm.nlwrote: Dear R users, in the example below the name of the genus will be displayed in the main titles using the variable predictor[i] and paste. I would like to have the genus name in italic. However all my attempts using expression and substitute failed. Does anybody know a solution? Thanks a lot in advance. bernd Acrobeles -c(65.1,0.0,0.0,0.0,0.0,0.0) Acrobeloides -c(0.0,9.8,76.7,51.1,93.9,43.9) Alaimus-c(0.0,4.9,0.0,0.0,0.0,6.3) Aphelenchoides -c(126.5,29.3,76.7,134.1,176.7,87.9) x-data.frame(Acrobeles,Acrobeloides,Alaimus,Aphelenchoides) predictor - colnames(x) ylabel -Numerical abundance mainlabel1 -Boxplot for mainlabel2 -sp. cexalabel -1.8 # axis label cexmlabel -1.6 # main label par(oma=c(6,6,3,3),mar = c(6, 4, 4, 2) + 0.1,mfrow=c(2,2)) for (i in 1:ncol(x)){ boxplot(x[,i], main=paste(mainlabel,predictor[i],mainlabel2),ylab=paste(ylabel),cex.lab=cexalabel,cex.main=cexmlabel,cex.axis=1.5) } --- Bernd Panassiti National Institute of Public Health the Environment (RIVM) Laboratory for Ecological Risk Assessment (LER) P.O. Box 1 3720 BA Bilthoven The Netherlands e-mail: bernd.panass...@rivm.nl tel. +31 30 274 3647 Radboud University Nijmegen Department of Environmental Science b.panass...@science.ru.nl Disclaimer RIVM [[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. [[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.