Re: [R] Two conditions selection
Rodrigo Díaz wrote Hi. I have a matrix like this: cycle=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3))col=c(rep(blue,2),rep(green,2),rep(blue,2),rep(green,2),rep(blue,2),rep(green,2))values=c(1:12)data.frame(cycle,col,values) # cycle col values#1 1 blue 1#2 1 blue 2#3 1 green 3#4 2 green 4#5 2 blue 5#6 2 blue 6#7 3 green 7#8 3 green 8#9 3 blue 9#10 4 blue 10#11 4 green 11#12 4 green 12 I want to select or extract values matching 2 conditions. For example: values from col blue and cycle 1. If I use : values[col==blue] I get all blue values. I tried using values[c(col==blue,cycle==1)] but is not working. Please help. I have a very big data matrix and I do not wanna go to excel and start cutting the data. Thanks. [[alternative HTML version deleted]] __ R-help@ mailing list -- To UNSUBSCRIBE and more, see 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. How about # Your Code cycle=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3)) col=c(rep(blue,2),rep(green,2),rep(blue,2),rep(green,2),rep(blue,2),rep(green,2)) values=c(1:12) df - data.frame(cycle,col,values) # Subset data frame df df[cycle==1 col==blue,] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Two-conditions-selection-tp4710762p4710763.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Latest Xorg updates broke R x11()
Hello, Have 15 RedHat EL6 workstations patched to current. Over the weekend the kernel was patched to 6.7 and the xorg-x11-server and our R-3.1.2 will not open a xterm window. It appears to select a portion of the screen and lock onto it. This section can be moved around the screen at will, however; it is not possible to access anything inside the screen. It can only be managed by killing the R process. Any suggestions for resolution would be greatly appreciated. FYI: The kernel was upgraded to 6.7 and xorg-x11-server to 1.15.0-36.el6.x86_64. Thank you in advance for any assistance. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] sapply function and poisson distribution
dimnik wrote thank you for your answer.Yes,that sounds right.I thought the same thing but the problem is how can i generalize the command for every vector of numbers not only for the specific example?not only for c(1,2),c(0.1,0.8). 2015-01-04 0:45 GMT+00:00 Pete Brecknock [via R] ml-node+s789695n4701358h57@.nabble : dimnik wrote i want to find a function that takes in two vectors of numbers that have the same length.The output should be a list of vectors, where each vector is a sequence of randomly generated Poisson variables where the number of samples in each vector is determined by the entries in the first input vector and the lambdas come from the entries in the second input vector. For example, :If the inputs are c(1,2) and c(0.1,0.8) the output will be a list of twovectors where the first vectorhas a single sample from Poisson(0.1) and the second vector has two samples from Poisson(0.8).How can i do all that kind of stuff using sapply function? thank u in advance How about using mapply, the multivariate version of sapply? Based on your example ... mapply(function(x,y) rpois(x,y), c(1,2),c(0.1,0.8)) HTH Pete -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701358.html To unsubscribe from sapply function and poisson distribution, click here lt;http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codeamp;node=4701353amp;code=dmFnZWxpc2d1ZEBnbWFpbC5jb218NDcwMTM1M3wtMTg5MDAyODgzMA==gt; . NAML lt;http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_vieweramp;id=instant_html%21nabble%3Aemail.namlamp;base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespaceamp;breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.namlgt; Not sure how you intend to specify the input vectors for n and lambda One way would be as below - you can amend the 2 vectors with the values of your choice. n - c(1,2,3,4,5) lambda - c(0.1,0.8,1.2,2.2,4.2) mapply(function(x,y) rpois(x,y), n, lambda) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701384.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] sapply function and poisson distribution
dimnik wrote i want to find a functionthattakes in two vectors of numbers thathave the same length.The output should be a listof vectors, where each vector is a sequence of randomly generated Poisson variableswhere the number of samples in each vector is determined by the entries in the first input vector and the lambdas come from the entries in the second input vector. For example, :If the inputs are c(1,2)and c(0.1,0.8) the output will be a list of twovectors where the first vectorhas a single sample fromPoisson(0.1) andthe second vector hastwo samples from Poisson(0.8).How can i do all that kind of stuff using sapply function? thank u in advance How about using mapply, the multivariate version of sapply? Based on your example ... mapply(function(x,y) rpois(x,y), c(1,2),c(0.1,0.8)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701358.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] help with xts
I have 3 xts objects: test, cond1, cond2 You can download here: https://dl.dropboxusercontent.com/u/102669/obj.rar My problem is very simple. test [ cond1 cond2] = NA THIS WORKS test [ cond1 cond2] = -test [ cond1 cond2] THIS DOESN'T WORKS Why? My objective is to substitute all values in test (when cond1 cond2) with the corresponding values of test but with negative sign __ 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] Selecting a maximum value in same ID
Lee wrote Hi, I am struggling with this issue and need some helps. The data set 'DF' includes persons' IDs and other variables A, B, and C. Each Person has multiple values in A, B, and C. What I am trying to do is 1) selecting a maximum value of B within same ID, and 2) making a new data set (DF.2) that select rows aligning with the maximum value of B. DF and DF.2 are below. I've used functions combining which.max, subset, and loop, but it did not work. If you have ideas, please help. DF IDABC 1 12 36 2 1 15 30 2 2 56 11 2 233 30 2 383 23 2 3587 2 4752 2 482 36 2 577 35 2 575 23 2 673 10 2 676 35 2 775 14 2 721 30 2 814 11 2 846 11 2 875 11 2 830 36 2 921 35 2 975 23 2 DF.2 IDABC 1 12 36 2 233 30 2 383 23 2 482 36 2 577 35 2 676 35 2 721 30 2 830 36 2 921 35 2 Thank you in advance, Lee How about using ddply? library(plyr) txt -ID A B C 1 12 36 2 1 15 30 2 2 56 11 2 2 33 30 2 3 83 23 2 3 58 7 2 4 75 2 2 4 82 36 2 5 77 35 2 5 75 23 2 6 73 10 2 6 76 35 2 7 75 14 2 7 21 30 2 8 14 11 2 8 46 11 2 8 75 11 2 8 30 36 2 9 21 35 2 9 75 23 2 d - read.table(textConnection(txt), header = TRUE) closeAllConnections() ddply(d,~ID,function(x){x[which.max(x$B),]}) # Returns ID A B C 1 1 12 36 2 2 2 33 30 2 3 3 83 23 2 4 4 82 36 2 5 5 77 35 2 6 6 76 35 2 7 7 21 30 2 8 8 30 36 2 9 9 21 35 2 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Selecting-a-maximum-value-in-same-ID-tp4686492p4686500.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.
Re: [R] multiple plots
slavia wrote Hi, I have some values that I need to represente in the same plot. For exemple, if I have, c-c(200,205,210,215,220,225,230,235) a-c(0.032,0.44,0.86,0.65,0.53,0.213,0.46,0.231) b-c(0.325,0.657,0.784,0.236,0.798,0.287,0,748,0.785) d-c(0.786,0.217,0.538,0.513,0.870,0.326,0.647,0.217) c is a independente variable (represented in x axis) and a, b, d are diferente dependente varible (represented in a y axes). a, b and d should be diferent lines, How can I do that? Thank you Think there was a typo in the vector b where 0,748 should have been 0.748. To overlay lines on the same plot you could try the lines function. # Corrected Data c-c(200,205,210,215,220,225,230,235) a-c(0.032,0.44,0.86,0.65,0.53,0.213,0.46,0.231) b-c(0.325,0.657,0.784,0.236,0.798,0.287,0.748,0.785) d-c(0.786,0.217,0.538,0.513,0.870,0.326,0.647,0.217) # Plot plot(c,a, type=o, col=red, ylim=c(min(a,b,d),max(a,b,d))) lines(c,b, type=o,col=blue) lines(c,d, type=o,col=green) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/multiple-plots-tp4686489p4686501.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.
Re: [R] Lattice Barchart
Pete Brecknock wrote Hi The code below plots a stacked barchart. I would like to overlay on this chart a circular plotting character at the sum of the bars for each month. The plotted characters should be joined with a line. So, for 1/1/2014, I would like to see a point at 200 (-1000+1000+200). For 2/1/2014 a point at 600 (-2000+2000+600) and so on. # Barchart Plot library(lattice) d0 - structure(c(-1000,-2000,-2500,-5000,1000,2000,3000,2000,200,600,1000,900), .Dim = c(4L, 3L), .Dimnames = list(c(1/1/2014, 2/1/2014, 3/1/2014, 4/1/2014), NULL)) mycols - c(red,brown,orange) barchart(d0, horizontal=FALSE, stack=TRUE, auto.key=list(text=c(A,B,C), columns =3, title=, cex.title =0.9, border=FALSE), xlab=Month, ylab=Difference, main=Stacked Barchart, par.settings = simpleTheme(col = mycols)) Any pointers would be gratefully received. Kind regards Pete I put together the following solution but would be interested in any other approaches people may have to share. library(lattice) library(latticeExtra) d0 - structure(c(-1000,-2000,-2500,-5000,1000,2000,3000,2000,200,600,1000,900), .Dim = c(4L, 3L), .Dimnames = list(c(1/1/2014, 2/1/2014, 3/1/2014, 4/1/2014), NULL)) mycols - c(red,brown,orange) d1 - data.frame(Dt=row.names(d0), Sum=rowSums(d0)) barchart(d0, horizontal=FALSE, stack=TRUE, auto.key=list(text=c(A,B,C), columns =3, title=, cex.title =0.9, border=FALSE), xlab=Month, ylab=Difference, main=Stacked Barchart, par.settings = simpleTheme(col = mycols)) + as.layer(xyplot(Sum~Dt, data=d1, type=o, pch=19, cex=1.8, col=black, lwd=3), y.same=TRUE) Thanks Pete -- View this message in context: http://r.789695.n4.nabble.com/Lattice-Barchart-tp4685387p4685400.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.
Re: [R] Dataset to single column matrix
Krishia wrote Hello, I am pretty new to R and would like to transform my 272x12 matrix into a 3264X1. I'm trying to have the setup change from: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 13,14,15,16,17,18,19,20,21,22, 23, 24 etc. to 1 2 3 4 5 6 7 8 9 10 11 12 etc. Any suggestions? Thanks in advance Krishia Is this what you are looking for? # Create example matrice m - matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4, byrow=TRUE) # Create vector v - c(t(m)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Dataset-to-single-column-matrix-tp4683231p4683238.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.
Re: [R] midpoint between two dates
eric wrote Is there an easy way to get the midpoint between two dates in a data frame ? If I have a dataframe that looks like this : head(x) instDay remDay exp.time mpy 1 2006-02-02 2006-04-03 60 0.2 2 2006-04-17 2006-08-17 122 0.3 4 2006-08-17 2006-10-23 67 0.4 6 2006-10-23 2007-04-03 162 0.3 8 2007-04-03 2007-05-15 42 0.8 11 2007-05-15 2007-08-01 78 0.3 I would like an additional column that represents the midpoint between instDay and newDay. For those days where the time difference is an odd number and the midpoint would not be a specific date, it would be OK to round up or round down. I thought about converting both columns to numeric values and taking the difference, dividing by two with modulus operator, then adding to the first column and finally converting back to a date. But I'm think there must be a more simple way. How about ... date1 = as.Date(c(2013-10-10,2013-11-15,2013-12-25)) date2 = as.Date(c(2013-10-20,2013-11-20,2013-12-30)) df - data.frame(id=c(1,2,3),date1,date2) df$mid - df$date1 + floor((df$date2-df$date1)/2) print(df) id date1 date2mid 1 1 2013-10-10 2013-10-20 2013-10-15 2 2 2013-11-15 2013-11-20 2013-11-17 3 3 2013-12-25 2013-12-30 2013-12-27 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/midpoint-between-two-dates-tp4680649p4680654.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.
Re: [R] plot time series data in wide format
wudadan wrote Dear R users, I wonder if there is a way that I can plot a time series data which is in a wide format like this: CITY_NAME 2000Q12000Q2 2000Q32000Q4 2001Q1 2001Q2 2001Q3 2001Q4 2002Q1 2002Q2 CITY1100.5210 101.9667 103.24933 104.0506 104.4317 105.3921 106.7643 107.5202 107.2561 107.8184 CITY2100.0412 100.6146 103.20293 104.0867 104.6612 106.6126 109.3514 110.1943 110.9480 113.0071 CITY3 99.589599.2298 99.2694799.4101 100.5776 101.3719 101.5957 102.2411 103.4390 105.1745 CITY4 99.6491 101.5386 104.90953 106.1065 108.1785 110.6845 113.3746 114.1254 116.2121 119.1033 CITY5100.9828 103.6847 105.04793 106.5925 108.7437 110.5549 111.9343 112.6704 113.6201 115.3020 Ideally, each city of the five city is represented by a line in the plot. Any suggestion is appreciated! Thanks! Gary [[alternative HTML version deleted]] __ R-help@ 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. How about using the zoo package library(zoo) # Read Data text - CITY_NAME 2000Q1 2000Q2 2000Q3 2000Q4 2001Q1 2001Q2 2001Q3 2001Q4 2002Q1 2002Q2 CITY1 100.5210 101.9667 103.24933 104.0506 104.4317 105.3921 106.7643 107.5202 107.2561 107.8184 CITY2 100.0412 100.6146 103.20293 104.0867 104.6612 106.6126 109.3514 110.1943 110.9480 113.0071 CITY3 99.5895 99.2298 99.26947 99.4101 100.5776 101.3719 101.5957 102.2411 103.4390 105.1745 CITY4 99.6491 101.5386 104.90953 106.1065 108.1785 110.6845 113.3746 114.1254 116.2121 119.1033 CITY5 100.9828 103.6847 105.04793 106.5925 108.7437 110.5549 111.9343 112.6704 113.6201 115.3020 df - read.table(textConnection(text), header=TRUE, check.names=FALSE) #Create zoo object d - t(df[,-1]) ind - as.yearqtr(names(df)[-1]) z - zoo(d,ind) # Plot plot(z, plot.type=single, col=1:5, lwd=2) legend(topleft,legend=c(City1,City2,City3,City4,City5),lty=1, lwd=2, col=1:5) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/plot-time-series-data-in-wide-format-tp4679589p4679591.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.
[R] Adding List Elements To A Data Frame
Hi I am trying to add the contents of the list myList to a new column z in the data frame myDataframe myList - list(c(A1,B1), c(A2,B2,C2), c(A3,B3)) myDataframe - data.frame(x=c(1,2,3), y=c(R,S,T)) Would like to produce x y z 1 R A1,B1 2 S A2,B2,C2 3 T A3,B3 where z is a character string holding the contents of the different elements of the list. Any thoughts greatly appreciated. Pete -- View this message in context: http://r.789695.n4.nabble.com/Adding-List-Elements-To-A-Data-Frame-tp4671932.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.
Re: [R] Adding List Elements To A Data Frame
Thanks Brian. Perfect. Brian Diggs wrote On 7/19/2013 12:54 PM, Pete Brecknock wrote: Hi I am trying to add the contents of the list myList to a new column z in the data frame myDataframe myList - list(c(A1,B1), c(A2,B2,C2), c(A3,B3)) myDataframe - data.frame(x=c(1,2,3), y=c(R,S,T)) Would like to produce x y z 1 R A1,B1 2 S A2,B2,C2 3 T A3,B3 myDataframe$z - sapply(myList, paste0, collapse=,) where z is a character string holding the contents of the different elements of the list. Any thoughts greatly appreciated. Pete -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health Science University __ R-help@ 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. -- View this message in context: http://r.789695.n4.nabble.com/Adding-List-Elements-To-A-Data-Frame-tp4671932p4671936.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.
Re: [R] Ordering a matrix by row value in R2.15
fitz_ra wrote I know this is posted a lot, I've been through about 40 messages reading how to do this so let me apologize in advance because I can't get this operation to work unlike the many examples shown. I have a 2 row matrix temp [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9][,10] [1,] 17.000 9.00 26.0 5.0 23.0 21.0 19.0 17.0 10.0 63. [2,] 15.554 7.793718 33.29079 15.53094 20.44825 14.34443 11.83552 11.62997 10.16019 115.2602 I want to order the matrix using the second row in ascending order. From the many examples (usually applied to columns) the typical solution appears to be: temp[order(temp[2,]),] Error: subscript out of bounds However as you can see I get an error here. When I run this one line command: sort(temp[2,]) [1] 7.793718 10.160190 11.629973 11.835520 14.344426 15.530939 15.553999 20.448249 33.290789 [10] 115.260192 This works but I want the matrix to update and the corresponding values of row 1 to switch with the sort. Maybe consider the order function orig - matrix(c(10,20,30,3,1,2), nrow=2, byrow=TRUE) new -t(apply(orig,1,function(x) x[order(orig[2,])])) orig [,1] [,2] [,3] [1,] 10 20 30 [2,]312 new [,1] [,2] [,3] [1,] 20 30 10 [2,]123 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Ordering-a-matrix-by-row-value-in-R2-15-tp4662337p4662340.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.
Re: [R] Integrate with vectors and varying upper limit
sunny0 wrote I'd like to integrate vectors 't' and 'w' for log(w)/(1-t)^2 where i can vary the upper limit of the integral to change with each value of 't' and 'w', and then put the output into another vector. So, something like this... w=c(.33,.34,.56) t=c(.2,.5,.1) k-c(.3,.4,.5) integrand - function(t) {log(w)/(1-t)^2} integrate(integrand, lower = 0, upper = k) or maybe... integrand - function(tt) {tt} integrate(integrand, lower = 0, upper = k) ... with sapply or something similar to create the output vector. How can this be done? Something like this? w=c(.33,.34,.56) t=c(.2,.5,.1) k=c(.3,.4,.5) integrand - function(t) {log(w)/(1-t)^2} out - sapply(k,function(x) integrate(integrand, lower = 0, upper = x , subdivisions=1000)) # Output [,1] [,2][,3] value-0.4017303 -0.6249136 -0.9373696 abs.error2.798235e-05 9.17413e-05 9.209191e-05 subdivisions 32 208 91 message OK OKOK call Expression Expression Expression HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Integrate-with-vectors-and-varying-upper-limit-tp4662338p4662341.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.
[R] data.frame with NA
I have this little data.frame http://dl.dropbox.com/u/102669/nanotna.rdata Two column contains NA, so the best thing to do is use na.locf function (with fromLast = T) But locf function doesn't work because NA in my data.frame are not recognized as real NA. Is there a way to substitute fake NA with real NA? In this case na.locf function should work Thank you __ 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] Find NA in xts object
Hi to all, i'm new to R I have an xts object. Can i find: a) how many NA are in my object ? b) eventually where (in which line) they are Thank you __ 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] Ordering Table Columns
cdouglass wrote Hello all, Totally new to this and I'm just doing a frequency distribution analysis on T-shirt sales by size. I have a .csv with 60 orders. I read in the data using read.csv. If I look at the summary() or table() of the data it looks fine, except that the shirt sizes are alphabetical rather than from S-XXL--so the bar graph loses the shape of the data based on size. All I want to do is get the table to arrange the data: S M L XL XXL Here's the code that I've run that got me closer to what I want. It seems like it should be simple, but going through the R in a Nutshell and asking Google as many different ways as I can think to phrase it are turning up nothing. shirt - read.csv(http://localhost/examples/tshirt_purchases.csv;, header=TRUE, sep = ,, nrows=60) shirt.table-summary(shirt) shirt.table Shirt.Size L :20 M :20 S :11 XL : 7 XXL: 2 What I want is: Shirt.Size S :11 M :20 L :20 XL : 7 XXL: 2 Does anyone know how to do this, or am I coming at it from the wrong direction? If this has been answered previously and I've just failed to find it in my searches, please accept my apologies. Many Thanks, Chris Think you want to have a look at factors Typing ?factor will throw up the relevant help pages # Your Data tbl - read.table(header = TRUE, text = ShirtSize Number L 20 M 20 S 11 XL 7 XXL 2 ) # ShirtSize Alphabetical tbl[tbl$ShirtSize,] # Reorder Factor tbl$ShirtSize = factor(tbl$ShirtSize, levels=c(S,M,L,XL,XXL)) # ShirtSize Order by Size tbl[tbl$ShirtSize,] Pete -- View this message in context: http://r.789695.n4.nabble.com/Ordering-Table-Columns-tp4660110p4660129.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.
Re: [R] Merging value labels into indicator variable.
tasnuvat wrote I have a vaiable named NAM having value : 1,2,3,4,5,6,7,8,9. I want to make an indicator variable that will take value 1 if NAM=7 or NAM=8 or NAM=9. How can I do that? I usually do: Var001- ifelse(NAM==7,1,0) for the simplest case. [[alternative HTML version deleted]] __ R-help@ 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. How about something like ... NAM = c(1,2,3,4,5,6,7,8,9) ifelse(NAM=7,1,0) # or ifelse(NAM %in% c(7,8,9),1,0) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Merging-value-labels-into-indicator-variable-tp4659703p4659705.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.
Re: [R] Quantiles of a subset of data
bradleyd wrote Excuse the request from an R novice! I have a data frame (DATA) that has two numeric columns (YEAR and DAY) and 4000 rows. For each YEAR I need to determine the 10% and 90% quantiles of DAY. I'm sure this is easy enough, but I am a new to this. quantile(DATA$DAY,c(0.1,0.9)) 10% 90% 12 29 But this is for the entire 4000 rows, when I need it to be for each YEAR. Is there no way to use a by argument in the quantile function? Thanks for any help you can provide. David check out ?aggregate or ?by should be of help HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Quantiles-of-a-subset-of-data-tp4659063p4659064.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.
Re: [R] Quantiles of a subset of data
bradleyd wrote Thanks for your help Pete. I can almost get it to work with; by(day,year,quantile) but this only gives me 0% 25% 50% 75% 100%, not the ones I'm looking for, 10% and 90%. I have tried; by(day,year,quantile(c(0.1, 0.9))) but this is rejected by Error in FUN(X[[1L]], ...) : could not find function FUN Need to add the quantiles of interest . # Dummy Data d - data.frame(year=c(rep(2010,10),rep(2011,10),rep(2012,10)), quantity = c(1:30)) # Quantiles by Year by(d$quantity,d$year,quantile,c(0.1,0.9)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Quantiles-of-a-subset-of-data-tp4659063p4659072.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.
Re: [R] Converting the data in year month day hour and minutes to date
jdbaba wrote Hi , I am trying to convert the date as factor to date using as.date function in R. I have the date in the following format 2008-01-01 02:30 I tried to use the following command : as.Date(mydata$Date, format=%y-%m-%d ) Can somebody help me with this ? I was able to convert the format with no hour but getting difficulty with hour included. Thank you. Janesh [[alternative HTML version deleted]] __ R-help@ 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. how about as.POSIXlt(2008-01-01 02:30, format=%Y-%m-%d %H:%M) Pete -- View this message in context: http://r.789695.n4.nabble.com/Converting-the-data-in-year-month-day-hour-and-minutes-to-date-tp4659075p4659080.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.
Re: [R] Quantiles of a subset of data
bradleyd wrote That does it, thanks. Do you think you help me a little bit further? I actually have 4 columns, YEAR, DAY, TEMP , and IBI. They are all numeric. I need to calculate the average TEMP and IBI values between the 10% and 90% quantiles for each YEAR. The code * by(data$day,data$year,day,c(0.1,0.9)) * was correct in that it calculated the quantile values as intended, but I don't know how to then calculate the mean TEMP and IBI values encompasses within those quantiles. Thanks again, David have a look at trim argument of the mean function ?mean Pete -- View this message in context: http://r.789695.n4.nabble.com/Quantiles-of-a-subset-of-data-tp4659063p4659086.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.
Re: [R] Quantiles of a subset of data
bradleyd wrote Thanks Pete. The TRIM argument in the MEAN function tells me how to trim off decimal points, but I am lost as to how to append the mean values of TEMP and IBI between the 10% and 90% quantiles of DAY in each YEAR. DAY is the julian date that an event occurred in certain years. The events occurred numerous times in each year, and I want to be able to say what the mean day was in each year excluding those days greater than the 90% and less than the 10% quantile in that year. Would suggest that you forward a small, reproducible example with what you expect the results to look like. Pete -- View this message in context: http://r.789695.n4.nabble.com/Quantiles-of-a-subset-of-data-tp4659063p4659099.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.
Re: [R] doubt with function on R software
monicamir88 wrote Hello! I have a doubt with the R software. I have this function: results - function(bCODBOD, BOD, VSS, COD, sBOD, sCOD, TSS, TKNpa, T, NH3Ne, DO, Q, TKN, MLSS, NO3Ne, RAS, tanoxic1, tanoxic2, rbCOD, SDNR1, SDNR2, tdanoxic, tanaerobic, IRp, P) { bCOD - bCODBOD*BOD nbCOD - COD-bCOD nbVSS - VSS*(1-((bCODBOD)*(BOD-sBOD)/(COD-sCOD))) iTSS - TSS-VSS SF - TKNpa knt - 0.74*1.053^(T-20) kdnt - 0.08*1.04^(T-20) unmt - 0.75*1.07^(T-20) un - (unmt*NH3Ne)/(knt+NH3Ne)*(DO/(0.5+DO))-kdnt kst - 20*1^(T-20) kdt - 0.12*1.04^(T-20) umt - 6*1.07^(T-20) SRTtheo - 1/un SRT - SRTtheo*SF S - kst*(1+kdt*SRT)/(SRT*(umt-kdt)-1) Pxbh - Q*0.4*(bCOD-S)/(1+kdt*SRT) Pxdead - 0.15*kdt*Q*0.4*(bCOD-S)*SRT/(1+kdt*SRT) Nox1 - 0.8*TKN Pxbnit1 - Q*0.12*Nox1/(1+kdnt*SRT) Pxbio1 - Pxbh+Pxdead+Pxbnit1 Nox2 - TKN-NH3Ne-0.12*Pxbio1/Q Pxbnit2 - Q*0.12*Nox2/(1+kdnt*SRT) Pxbio2 - Pxbh+Pxdead+Pxbnit2 Nox3 - TKN-NH3Ne-0.12*Pxbio2/Q Pxbnit3- Q*0.12*Nox3/(1+kdnt*SRT) Pxbio3 - Pxbh+Pxdead+Pxbnit3 Pxvss - Pxbio3+Q*nbVSS Pxtss - Pxbio3/0.88+Q*nbVSS+Q*(TSS-VSS) MassMLVSS - Pxvss*SRT MassMLSS - Pxtss*SRT Vaerobic - MassMLSS/MLSS taerobic - Vaerobic/Q thaerobic - taerobic*24 VSSfraction - MassMLVSS/MassMLSS MLVSS - MLSS*VSSfraction observedyield - Pxtss/(Q*(bCOD-S))*bCODBOD Xb - SRT*Q/Vaerobic*(0.4*bCOD/(1+kdt*SRT)) IR - Nox3/NO3Ne-1-RAS Noxfeed - (IR*Q+RAS*Q)*NO3Ne Vanoxic1 - tanoxic1*Q Vanoxic2 - tanoxic2*Q FMb1 - Q*BOD/(Vanoxic1*Xb) FMb2 - Q*BOD/(Vanoxic2*Xb) rbCODbCOD - rbCOD/bCOD SDNRt1 - SDNR1*1.026^(T-20) SDNRt2 - SDNR2*1.026^(T-20) NOr1 - Vanoxic1*SDNRt1*Xb NOr2 - Vanoxic2*SDNRt2*Xb Excesscap1 - NOr1/Noxfeed Excesscap2 - NOr2/Noxfeed Vanoxic - tdanoxic*Q Vanaerobic - tanaerobic*Q NO3reac - NO3Ne*IRp/(1+IRp) rbCODremov - NO3reac*6.6 rbCODava - rbCOD-rbCODremov biorem - rbCODava/10 Pbiomassg - (Pxbh+Pxbnit3)*0.015/Q Premov - Pbiomassg+biorem Peff - P-Premov res - c(bCOD, nbCOD, nbVSS, iTSS, SF, knt, kdnt, unmt, un, kst, kdt, umt, SRTtheo, SRT, S, Pxbh, Pxdead, Nox1, Pxbnit1, Pxbio1, Nox2, Pxbnit2, Pxbio2, Nox3, Pxbnit3, Pxbio3 , Pxvss, Pxtss, MassMLVSS, MassMLSS, Vaerobic, taerobic, thaerobic, VSSfraction, MLVSS, observedyield, Xb, IR, Noxfeed, Vanoxic1, Vanoxic2, FMb1, FMb2, rbCODbCOD, SDNRt1, SDNRt2, NOr1, NOr2, Excesscap1, Excesscap2, Vanoxic, Vanaerobic, NO3reac, rbCODremov, rbCODava, biorem, Pbiomassg, Premov, Peff) names(res) - c(bCOD, nbCOD, nbVSS, iTSS, SF, knt, kdnt, unmt, un, kst, kdt, umt, SRTtheo, SRT, S, Pxbh, Pxdead, Nox1, Pxbnit1, Pxbio1, Nox2, Pxbnit2, Pxbio2, Nox3, Pxbnit3, Pxbio3 , Pxvss, Pxtss, MassMLVSS, MassMLSS, Vaerobic, taerobic, thaerobic, VSSfraction, MLVSS, observedyield, Xb, IR, Noxfeed, Vanoxic1, Vanoxic2, FMb1, FMb2, rbCODbCOD, SDNRt1, SDNRt2, NOr1, NOr2, Excesscap1, Excesscap2, Vanoxic, Vanaerobic, NO3reac, rbCODremov, rbCODava, biorem, Pbiomassg, Premov, Peff) return(res) } Where, for example, the values of the variables are: results(1.6, 140, 60, 300, 70, 132, 70, 1.5, 12, 0.5, 2, 22464, 35, 3000, 6, 0.6, 0.104, 0.0625, 80, 0.22, 0.31, 0.0625, 0.0625, 2, 6) I want to take the values, for example, from a data.frame. Is it possible? What I mean is, if I have a table with the values: Variable Value Q 22464 T12 BOD 140 ... ... When I want to call the function, I indicate where to go for each value of the variable in the table. Thus, when changing the value of the variables in the table would be easier than doing in the function. Thanks! Hi I took the liberty of simplifying things . # Define Your Function myfun - function(a,b,c){ A = a + 1 B = b + 2 C = c + 3 output - data.frame(A,B,C) return(output) } # Test The Function myfun(0,0,0) # Input Data inputData - data.frame(a=c(1,2,3,4), b=c(10,20,30,40), c=c(100,200,300,400)) # Ouput Data outputData - do.call(myfun, inputData) print(outputData) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/doubt-with-function-on-R-software-tp4658973p4658990.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.
Re: [R] R function help!
simonj16 wrote Consider an urn that contains 10 tickets, labelled: 1,1,1,1,2,5,5,10,10,10 I want to draw with replacement n=40 tickets. I am interested in the sum, Y, of the 40 ticket values that I draw Write an R function named urn.model that simulates this experiement. What I have below is not working. flip.n = function(p,n) { return(runif(n,0,1) p) } ticket.ns-c(1,1,1,1,2,5,5,10,10,10) urn.model = function(ticket.ns) { draws.per.sim = 1 prob = .1 urn.results = rep(-1, ticket.ns) for (i in 1:ticket.ns) { draws = flip.n(prob,draws.per.sim) num =sum(draws,ticket.ns) urn.results[i] = num } return(urn.results) } urn.25.samples =urn.model(25) urn.25.samples Follow up question: Use urn.model to generate a sample y={y1,...,y25) of n=25 observed sums. Any good? ticket.ns-c(1,1,1,1,2,5,5,10,10,10) draw=NULL for (i in 1:25){ draw[i] - sum(sample(ticket.ns,40,replace=TRUE)) } print(draw) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/R-function-help-tp4658998p4659001.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.
Re: [R] assign estimated values
malaka wrote Hi, I want to assign the ar1 , ma 1 and the intercept estimated by the following code to three variables a, b and c respectively. Can anyone help me with this please? code: a0 = 0.05; a1 = 0.1; b1 = 0.85 nu = rnorm(2500) epsi = rep(0, 2500) h = rep(0, 2500) for (i in 2: 2500) { h[i] = a0 + a1 * epsi[i-1]^2 + b1 * h[i-1] ; epsi[i] = nu[i] * sqrt(h[i])} epsi = epsi[1501:2500] epsi=epsi*epsi arma(epsi,order=c(1,1)) You haven't specified a library for the arma function. Your code doesn't work for me in the form you posted it. However, changing ... arma(epsi, order=c(1,1)) to mod = arima(epsi, order=c(1,0,1)) You can extract the parameters of interest using a= mod$coef[ar1] b= mod$coef[ma1] c= mod$coef[intercept] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/assign-estimated-values-tp4658139p4658141.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.
Re: [R] Converting column of strings to boolean
domcastro wrote Hi I'm trying to convert a column of strings (nominal types) to a set of boolean / binary / logical values. For example, in the column there is red, blue, green and yellow. There are 100 rows and each has a colour. I want to convert the column to 4 columns: red, blue, green,yellow and then either 1 or 0 put in the relevant row. Thanks maybe model.matrix will help # d is my understanding of your data d-factor(c(red,green,red,blue,green,yellow,red)) model.matrix(~d -1) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Converting-column-of-strings-to-boolean-tp4656739p4656741.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.
[R] Plot Header
Any recommendations for how I can embed my title below in a single red strip/box across the plot area in the outer margin? I would like to avoid the color appearing in any other area defined by the oma. # Example Plot par(mfrow=c(2,2),mar=c(4,4,2,2), oma = c(1, 1, 3, 1)) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) # Title title(MY TITLE, outer = TRUE, cex = 1.5, adj=0, col=blue, font=2) Thanks for any pointers Pete -- View this message in context: http://r.789695.n4.nabble.com/Plot-Header-tp4655654.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.
Re: [R] Plot Header
David Winsemius wrote On Jan 15, 2013, at 2:49 PM, Pete Brecknock wrote: Any recommendations for how I can embed my title below in a single red strip/box across the plot area in the outer margin? I would like to avoid the color appearing in any other area defined by the oma. The code used blue ... not sure what that last sentence meant. Or what the single strip was supposed to look like. # Example Plot par(mfrow=c(2,2),mar=c(4,4,2,2), oma = c(1, 1, 3, 1)) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) # Title title(MY TITLE, outer = TRUE, cex = 1.5, adj=0, col=blue, font=2) opar - par(mfrow=c(2,2),mar=c(4,4,2,2), oma = c(1, 1, 3, 1)) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) title(MY TITLE, outer = TRUE, cex = 1.5, adj=0, col.main=blue, font=2, adj=0.5) par(opar) -- David Winsemius Alameda, CA, USA __ R-help@ 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. Thanks for the reply. Apologies for the poor description. Let me try again. The code produces 4 charts in a 2 by 2 matrix Above these charts I have a single, left justified title in the outer margin. I would like to embed this title in a box which should run from the left hand side of the screen to the right and be say 10 lines high. I would like to color the box background red, the title MY TITLE would appear in blue inside the box. This could be described as a header. Any better? -- View this message in context: http://r.789695.n4.nabble.com/Plot-Header-tp4655654p4655665.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.
Re: [R] Plot Header
David Winsemius wrote On Jan 15, 2013, at 3:25 PM, Pete Brecknock wrote: David Winsemius wrote On Jan 15, 2013, at 2:49 PM, Pete Brecknock wrote: Any recommendations for how I can embed my title below in a single red strip/box across the plot area in the outer margin? I would like to avoid the color appearing in any other area defined by the oma. The code used blue ... not sure what that last sentence meant. Or what the single strip was supposed to look like. # Example Plot par(mfrow=c(2,2),mar=c(4,4,2,2), oma = c(1, 1, 3, 1)) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) # Title title(MY TITLE, outer = TRUE, cex = 1.5, adj=0, col=blue, font=2) opar - par(mfrow=c(2,2),mar=c(4,4,2,2), oma = c(1, 1, 3, 1)) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) title(MY TITLE, outer = TRUE, cex = 1.5, adj=0, col.main=blue, font=2, adj=0.5) par(opar) -- David Winsemius Alameda, CA, USA __ R-help@ 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. Thanks for the reply. Apologies for the poor description. Let me try again. The code produces 4 charts in a 2 by 2 matrix Above these charts I have a single, left justified title in the outer margin. I would like to embed this title in a box which should run from the left hand side of the screen to the right and be say 10 lines high. I would like to color the box background red, the title MY TITLE would appear in blue inside the box. This could be described as a header. Admittedly a hack: opar - par(mfrow=c(2,2),mar=c(4,4,2,2), oma = c(1, 1, 3, 1)) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) plot(rnorm(100),1:100) par(opar) rect(-5, 110, 5, 120, col=red, xpd=NA) title(MY TITLE, outer = TRUE, cex = 1.5, adj=0, col.main=blue, font=2, adj=0.5,line=-2) -- David Winsemius Alameda, CA, USA __ R-help@ 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. Thanks David. That will do nicely. Best regards Pete -- View this message in context: http://r.789695.n4.nabble.com/Plot-Header-tp4655654p4655676.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.
Re: [R] how to apply two or more functions to each columns in a time?
Yao He wrote Dear All: I want to calculate the mean and sd for each column in a data.frame. Taking data(iris) for example: I tried sapply(iris[,-5],mean,na.rm=T) or sapply(iris[,-5],sd,na.rm=T) to calculate the mean and sd .But sapply() transfer a function per time. How to transfer two functions in a time to generate a data.frame like this: Sepal.Length Sepal.Width Petal.Length Petal.Width mean5.843333.057333.758000 1.199333 SD0.828060.435861.765298 0.762237 Thanks a lot Yao He -- — Master candidate in 2rd year Department of Animal genetics breeding Room 436,College of Animial ScienceTechnology, China Agriculture University,Beijing,100193 E-mail: yao.h.1988@ —— __ R-help@ 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. Or maybe using apply # data d - iris[,-5] # apply function a -data.frame(apply(d, 2, function(x) c(mean=mean(x), sd=sd(x # print(a) output Sepal.Length Sepal.Width Petal.Length Petal.Width mean5.843 3.057 3.758000 1.199 sd 0.8280661 0.4358663 1.765298 0.7622377 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/how-to-apply-two-or-more-functions-to-each-columns-in-a-time-tp4654001p4654004.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.
Re: [R] how to add a column from another dataset with merge
kiotoqq wrote I want to add a shorter column to my dataset with the function merge, it should be filled with NAs wo be as long as the other colums, like this: idage 946 856 6 52 5 NA 4 NA 3 NA 1 NA i did this: pa1 - merge(pa1, an1, by=mergeid) and it says 'by' must specify uniquely valid column(s) how about ... #Data d1-data.frame(id=c(9,8,6,4,4,3,1)) d2-data.frame(id=c(9,8,6),age=c(46,56,52)) # Left Merge d-merge(d1,d2,all.x=TRUE) # Reorder d[order(d$id,decreasing=TRUE),] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-column-from-another-dataset-with-merge-tp4652482p4652486.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.
Re: [R] IMPORTANT!!!! PLEASE HELP ME
jholtman wrote What do you want to do with the samples after you generate them? What are the parameters for the normal distribution? You left a lot of information out. You can generate 500,000 numbers and then store them in a 1x50 matrix quite easily. On Sat, Nov 24, 2012 at 5:03 PM, Jasmin lt; yasemin_deniz89@ gt; wrote: Hi, I want to generate 1 samples from normal distribution with replacement case and every sample size is 50. What should I do ? -- View this message in context: http://r.789695.n4.nabble.com/IMPORTANT-PLEASE-HELP-ME-tp4650676.html Sent from the R help mailing list archive at Nabble.com. __ R-help@ 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ R-help@ 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. maybe ... replicate(1, rnorm(50)) could work for you HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/IMPORTANT-PLEASE-HELP-ME-tp4650676p4650686.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.
Re: [R] IMPORTANT!!!! PLEASE HELP ME
Jasmin wrote I try to use hansen-hurwitz and horvitz-thompson estimator.So I should generate samples which come from normal distribution (mu=50,sigma=3). I have taken the liberty of scaling the problem down to something more digestible and have changed lines 5 and 7 in your code nsamples=10 sampsize=5 i=0 y=matrix(rnorm(nsamples*sampsize,50,3),nrow=nsamples) s=matrix(NA,10,5) for(i in 1:10){ s[i,]=sample(y,5,replace=T) } HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/IMPORTANT-PLEASE-HELP-ME-tp4650676p4650692.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.
Re: [R] Summary statistics for matrix columns
frespider wrote Hi, it is possible. but don't you think it will slow the code if you convert to data.frame? Thanks Date: Thu, 22 Nov 2012 18:31:35 -0800 From: ml-node+s789695n4650500h51@.nabble To: frespider@ Subject: RE: Summary statistics for matrix columns HI, Is it possible to use as.matrix()? res-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x))) res1-as.matrix(res) is.matrix(res1) #[1] TRUE res1[c(1:4,7,5,8,6),] #Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 #Min.10.0 1.0 17.0 3.0 18.0 11.0 13.0 15.0 #1st Qu. 24.75000 29.5 26.0 7.75000 40.0 17.25000 27.5 34.75000 #Median 34.0 46.0 42.5 35.5 49.5 23.5 51.5 51.5 #Mean42.5 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 #sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 #3rd Qu. 67.75000 58.5 50.0 63.25000 54.25000 30.25000 56.25000 70.5 #IQR 43.0 29.0 24.0 55.5 14.25000 13.0 28.75000 35.75000 #Max.74.0 77.0 76.0 70.0 65.0 63.0 79.0 80.0 # Col9Col10 #Min. 2.0 6.0 #1st Qu. 24.5 12.5 #Median 33.5 48.0 #Mean34.88000 40.75000 #sd 24.39811 28.21727 #3rd Qu. 45.25000 63.0 #IQR 20.75000 50.5 #Max.71.0 72.0 Solves the order and the matrix output! A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650500.html To unsubscribe from Summary statistics for matrix columns, click here. NAML Then maybe x - matrix(sample(1:8000),nrow=100) colnames(x)- paste(Col,1:ncol(x),sep=) apply(x,2,function(x) c(Min=min(x), 1st Qu =quantile(x, 0.25,names=FALSE), Median = quantile(x, 0.5, names=FALSE), Mean= mean(x), Sd=sd(x), 3rd Qu = quantile(x,0.75,names=FALSE), IQR=IQR(x), Max = max(x))) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650547.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.
Re: [R] Using cumsum with 'group by' ?
email] 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Using-cumsum-with-group-by-tp4650457p4650505.html To unsubscribe from Using cumsum with 'group by' ?, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Using-cumsum-with-group-by-tp4650457p4650538.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ [hidden email] 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: [hidden email] Priv: [hidden email] __ [hidden email] 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Using-cumsum-with-group-by-tp4650457p4650550.html To unsubscribe from Using cumsum with 'group by' ?, click here. NAML Peter Dalgaard's suggestion works for me ... lines-id x date time 1 5 2012-06-05 12:01 1 10 2012-06-05 12:02 1 45 2012-06-05 12:03 2 5 2012-06-05 12:01 2 3 2012-06-05 12:03 2 2 2012-06-05 12:05 3 5 2012-06-05 12:03 3 5 2012-06-05 12:04 3 8 2012-06-05 12:05 1 5 2012-06-08 13:01 1 9 2012-06-08 13:02 1 3 2012-06-08 13:03 2 0 2012-06-08 13:15 2 1 2012-06-08 13:18 2 8 2012-06-08 13:20 2 4 2012-06-08 13:21 3 6 2012-06-08 13:15 3 2 2012-06-08 13:16 3 7 2012-06-08 13:17 3 2 2012-06-08 13:18 # read in data dat1-read.table(textConnection(lines), header=TRUE,stringsAsFactors=FALSE) # build csum variable newdata - transform(dat1, csum=ave(x, id, as.Date(date), FUN=cumsum)) # order data (not really necessary) newdata.ord -newdata[order(dat1[,id],dat1[,date],dat1[,time]),] Or have I misinterpreted your request? HTH Pete (B not D) -- View this message in context: http://r.789695.n4.nabble.com/Using-cumsum-with-group-by-tp4650457p4650556.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.
Re: [R] Summary statistics for matrix columns
frespider wrote Hi, is there a way I can calculate a summary statistics for a columns matrix let say we have this matrix x - matrix(sample(1:8000),nrow=100) colnames(x)- paste(Col,1:ncol(x),sep=) if I used summary summary(x) i get the output for each column but I need the output to be in matrix with rownames and all the columns beside it this how I want it Col76 Col77 Min. :739 1st Qu. :1846 1630 Median : 3631 3376 Mean: 3804 3617 Sd : 3rd Qu.:5772 5544 IQR: Max. :79527779 Is there an easy way? Thanks How about ... x - matrix(sample(1:8000),nrow=100) colnames(x)- paste(Col,1:ncol(x),sep=) apply(x,2,function(x) c(summary(x), sd=sd(x), IQR=IQR(x))) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650490.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.
Re: [R] Merging two data frames with different columns names
Peterso wrote Uwe: I was actually trying to stack one table on top of the other. All column names are the same except for the Part1 and Part 2. My final table should look like the table below. Maybe it is possible to change the names of Part1 and Part 2 to Part? A B C Part 1 0 1 550 0 1 1 669 etc A couple of approaches using the rbind function ... require(conf.design) # Your Data d1 - conf.design(c(1,1,1), p=2, block.name=blk, treatment.names = c(A,B,C)) d2 - conf.design(c(1,1,1), p=2, block.name=blk, treatment.names = c(A,B,C)) rep1 - c(550,669,633,642,1037,749,1075,729) rep2 - c(604,650,601,635,1052,868,1063,860) # Approach 1 part1 - data.frame(d1,part=rep1) part2 - data.frame(d2,part=rep2) d12 - rbind(part1,part2) # Approach 2 part1 - data.frame(d1,rep1) names(part1)- c(blk,A,B,C,part) part2 - data.frame(d2,rep2) names(part2)- c(blk,A,B,C,part) d12 - rbind(part1, part2) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Merging-two-data-frames-with-different-columns-names-tp4556400p4557974.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.
[R] [R-pkgs] New package joineR
Dear All, The 'joineR' package for the joint analysis of repeated measurements and time-to-event outcomes is now available on CRAN. The package contains utilities for creating and manipulating 'jointdata' objects, graphical summaries, a variogram function for estimating correlation structure, and maximum likelihood estimation for a class of random effects joint models. Best wishes, Pete. Pete Philipson Lecturer in Statistics School of Computing, Engineering and Information Sciences Northumbria University email: pete.philip...@northumbria.ac.uk [[alternative HTML version deleted]] ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ 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] Problems downloading file
I am running the following line to download data from the US Energy Information Administration. This function has worked successfully for me in the past but yesterday gave the error/warning messages below. If I simply type http://ir.eia.gov/wpsr/psw09.xls; (no quotes) into a browser, the file is available to download. I am running R 2.13.0 on Windows XP. # Download File Attempt download.file(http://ir.eia.gov/wpsr/psw09.xls,c:\\temp\\psw09.xls,mode=wb;) # Error Warning Messages trying URL 'http://ir.eia.gov/wpsr/psw09.xls' Error in download.file(http://ir.eia.gov/wpsr/psw09.xls;, c:\\temp\\psw09.xls, : cannot open URL 'http://ir.eia.gov/wpsr/psw09.xls' In addition: Warning message: In download.file(http://ir.eia.gov/wpsr/psw09.xls;, c:\\temp\\psw09.xls, : InternetOpenUrl failed: 'The operation timed out' Thanks for any insights. Pete Brecknock -- View this message in context: http://r.789695.n4.nabble.com/Problems-downloading-file-tp4435186p4435186.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.
Re: [R] Counting value changes
maris478 wrote Good afternoon, I've encountered a little bit of a problem, would appreciate any help here. I made a small vector consisting of ones and zeros. Something like this x - c(0,1,0,1,0,0,1,0), and all I need is to count how many times 0 becomes 1. Tried various, of what I thought, methods with built in functions. Didn't get any further. Thank you very much. How about ... x - c(0,1,0,1,0,0,0,0) sum(rle(x)$values) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Counting-value-changes-tp4400267p4400348.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.
Re: [R] finding the subscript of a vector fulfiiling a given condition
galemago wrote Dear Forum, I just recently started to work with R, I´d like to know if there is a way to give instructions/do operations related to values with different subscripts within a vector. Let´s assume I have a vector like this: A=368369370371 393394395 If I call j the subscripts of the vector I´d like to find the j value such that A(j+1)-A(j) is the maximum. This would tell me at which position of the vector I have the biggest gap. (the fifth in the example above) My problem is that I don´t find a way to define the vector subscripts as a variable, that I believe is what I need to solve this problem. Is that possible? Thanks a lot, hope to come back often in this forum, G. How about ... A - c(368,369,370,371,393,394,395) which.max(diff(A)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/finding-the-subscript-of-a-vector-fulfiiling-a-given-condition-tp4387870p4388042.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.
Re: [R] How to Calculate Percentage of Data within certain SD of Mean
How about # Read Data nb10 - read.table(http://www.adjoint-functors.net/su/web/314/R/NB10;) # Calculate Stats total = length(nb10[,1]) mean = mean(nb10[,1]) sd = sd(nb10[,1]) # Function ... nSD is the number of SD you are looking at pData - function(nSD){ lo = mean - nSD/2*sd hi = mean + nSD/2*sd percent = sum(nb10[,1]=lo nb10[,1]=hi)/total *100 } # Output ... print(paste(Percent of data within 2 SD is ,pData(2),%, sep=)) # 86% print(paste(Percent of data within 3 SD is ,pData(3),%, sep=)) # 93% print(paste(Percent of data within 4 SD is ,pData(4),%, sep=)) # 96% print(paste(Percent of data within 5 SD is ,pData(5),%, sep=)) # 97% print(paste(Percent of data within 6 SD is ,pData(6),%, sep=)) # 98% HTH Pete Ajata Paul wrote How do you calculate the percentage of data within 2SD, 3SD, 4SD, 5SD, and 6SD of the mean? I used the following link as the data I'm working with: nb10 - read.table(http://www.adjoint-functors.net/su/web/314/R/NB10;) if this helps answer my question. Can you please explain how to calculate the SD's? Please be specific in which part of the function changes when calculating the next SD up. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/How-to-Calculate-Percentage-of-Data-within-certain-SD-of-Mean-tp4359551p4359809.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.
Re: [R] text command - how to get a white background to cover grid lines
How about using the legend function ... plot(rnorm(100)) legend(60,2,100 Random Normal Draws,cex=.8,text.col=blue, box.col=red,bg=yellow) You can customize my effort to fit your specific needs HTH Pete Henry wrote New to R - rookie question. I'm a mechanical engineer and enjoying using R to make high quality graphs. I've searched. I want to put text notation on graph plot areas and have the text background box white to cover over the grid lines. my command so far text(15,5200,Air Flow,cex=.8,col=blue, background=white) # this doesn't work... I've tried bg=white, background color=white and a number of other attempts. The text is getting placed on the chart where I want it. Thanks, -Henry -- View this message in context: http://r.789695.n4.nabble.com/text-command-how-to-get-a-white-background-to-cover-grid-lines-tp4359826p4359840.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.
Re: [R] replace some values of a column with diffrent values
Hi Valerie One way would be to use the match function. # Your Data u =data.frame(coe=c(0,0,0,0,0,0,0,0), name=c(Time,Poten,AdvExp,Share,Change,Accounts,Work,Rating)) v = data.frame(coeff=c(0.7272727,0.322,0.0500123), enter=c(Accounts,Time,Poten)) # Match Function updates = v[match(u$name,v$enter),coeff] u$coe = ifelse(!is.na(updates), updates, u$coe) HTH Pete valerie wrote Hi, I have two data frames (u and v). u coe nam 1 0 Time 2 0Poten 3 0 AdvExp 4 0Share 5 0 Change 6 0 Accounts 7 0 Work 8 0 Rating v coeffenter 1 0.7272727 Accounts 2 0.322 Time 3 0.0500123Poten I want to update the values of coe in u by using the values of coeff in v. That is, I want to get the following result u coe nam 1 0.322 Time 2 0.0500123Poten 3 0 AdvExp 4 0 Share 5 0 Change 60.7272727 Accounts 7 0 Work 8 0 Rating OR the following result is also acceptable: 0.3220.0500123000 0.727272700 I tried the following, but the result is not right. replace(coe,colnames,coeff) [1] 0.7272727 0.0500123 0.322 0.000 0.000 0.000 0.000 [8] 0.000 PLEASE HELP ME. THANK YOU VERY MUCH. -- View this message in context: http://r.789695.n4.nabble.com/replace-some-values-of-a-column-with-diffrent-values-tp4360035p4360249.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.
Re: [R] combining data structures
David 1. The last line of the code should have been ... Output = as.data.frame(do.call(rbind,nn)) # Output Node Connect.up Connect.down 11 NULL 2, 3 22 1 4, 5 33 NULL 2, 3 44 1 4, 5 Apologies for any confusion 2. I am not familiar with the diagram package or the examples you describe. Why the desire to create a data frame? Why not just use a list? HTH Pete dkStevens wrote Thanks for the reply. Two things - I must have something missing because copying and pasting your example gave me an error ... your definitions of nn Output = do.call(as.data.frame(rbind),nn) Error in as.data.frame.default(rbind) : cannot coerce class 'function' into a data.frame The second is the need, and why I'm not sure this is the best way. This is to customize the flow charting library code from the library 'diagram' to use a node-and-link characterization of a network - think PERT charts or perhaps linking river segments in a water quality model. Each Node will be a, say, circle and has attributes of being connected up to one or more upstream nodes and down to one or more downstream nodes. So the Connect.up column is a vector up upstream connections, usually one but sometimes one, and likewise Connect.down. There is an accompanying table of links with attributes of which nodes are at each end of a link and other metadata that describe the link (e.g. the length of time required to traverse the link, its name etc. That said, my thought was that the situation was too simple to fire up a full-blown object system beyond what R provides natively. I guess it's like making a data frame that has some 3-d elements. On 2/3/2012 9:32 PM, Pete Brecknock wrote: nn=list() nn[[1]] = list(Node = 1, Connect.up = c(NULL), Connect.down = c(2,3)) nn[[2]] = list(Node = 2, Connect.up = c(1), Connect.down = c(4,5)) nn[[3]] = list(Node = 3, Connect.up = c(NULL), Connect.down = c(2,3)) nn[[4]] = list(Node = 4, Connect.up = c(1), Connect.down = c(4,5)) Output = do.call(as.data.frame(rbind),nn) -- David K Stevens, P.E., Ph.D., Professor Civil and Environmental Engineering Utah Water Research Laboratory 8200 Old Main Hill Logan, UT 84322-8200 435 797 3229 - voice 435 797 1363 - fax david.stevens@ [[alternative HTML version deleted]] __ R-help@ 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. -- View this message in context: http://r.789695.n4.nabble.com/combining-data-structures-tp4356288p4358435.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.
Re: [R] combining data structures
Not entirely sure why you would want a data.frame that has multiple entries in one of the columns (Connect.down) but leaving that aside is the following of any use? nn=list() nn[[1]] = list(Node = 1, Connect.up = c(NULL), Connect.down = c(2,3)) nn[[2]] = list(Node = 2, Connect.up = c(1), Connect.down = c(4,5)) nn[[3]] = list(Node = 3, Connect.up = c(NULL), Connect.down = c(2,3)) nn[[4]] = list(Node = 4, Connect.up = c(1), Connect.down = c(4,5)) Output = do.call(as.data.frame(rbind),nn) # Output value.Node value.Connect.up value.Connect.down 1 1 NULL 2, 3 2 21 4, 5 3 3 NULL 2, 3 4 41 4, 5 HTH Pete dkStevens wrote Group It's unlikely I'm trying this the best way, but I'm trying to create a data structure from elements like nNode = 2 nn = vector(list,nNode) nn[[1]] = list(Node = 1, Connect.up = c(NULL), Connect.down = c(2,3)) nn[[2]] = list(Node = 2, Connect.up = c(1), Connect.down = c(4,5)) #( and eventually many more nodes) NodeList = as.data.frame(nn[[1]]) for(i in 2:nNode) { NodeList = rbind(NodeList,as.data.frame(nn[[i]])) } and is trying to create a data frame with many rows and three columns: Node, Connect.up,Connect.down in which the Connect.up and Connect.down columns may be single numbers or vectors of numbers. The above approach gives an error: Error in data.frame(Node = 1, Connect.up = NULL, Connect.down = c(2, : arguments imply differing number of rows: 1, 0, 2 My earlier try by brute force worked fine: NodeList = as.data.frame(rbind(nn[[1]],nn[[2]])) NodeList Node Connect.up Connect.down 11 NULL 2, 3 22 1 4, 5 and gives me what I want (many more rows eventually). But I want to do this generically from the problem context in a procedure so I won't know up front how many nodes I'll have. Clearly I'm not understanding how referencing works for lists like I've created. Can anyone shed light on this? -- David K Stevens, P.E., Ph.D., Professor Civil and Environmental Engineering Utah Water Research Laboratory 8200 Old Main Hill Logan, UT 84322-8200 435 797 3229 - voice 435 797 1363 - fax david.stevens@ [[alternative HTML version deleted]] __ R-help@ 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. -- View this message in context: http://r.789695.n4.nabble.com/combining-data-structures-tp4356288p4356547.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.
Re: [R] Conditional cumulative sum
Axel Urbiz wrote Dear List, I'll appreciate your help on this. I'm trying to create a variable as in cumsum_y.cond1 below, which should compute the cumulative sum of y conditional on the value of cond==1. set.seed(1) d - data.frame(y= sample(c(0,1), 10, replace= T), cond= sample(c(0,1), 10, replace= T)) y cond cumsum_y.cond1 1 00 0 2 00 0 3 11 1 4 10 1 5 01 1 6 10 1 7 11 2 8 11 3 9 10 3 10 01 3 Thank you. Regards, Axel. [[alternative HTML version deleted]] __ R-help@ 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. is this what you are looking for ... set.seed(1) d - data.frame(y= sample(c(0,1), 10, replace= T), cond= sample(c(0,1), 10, replace= T)) d$cumsum_y.cond1 = cumsum(d$y d$cond) # Output y cond cumsum_y.cond1 1 00 0 2 00 0 3 11 1 4 10 1 5 01 1 6 10 1 7 11 2 8 11 3 9 10 3 10 01 3 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Conditional-cumulative-sum-tp4332254p4332344.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.
Re: [R] graph paper look
Erin Hodgess-2 wrote Dear R People: Short of doing a series of ablines, is there a way to produce graph paper in R please? Thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess@ __ R-help@ 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. How about ... x = rnorm(100) y = rnorm(100) plot(x,y) grid() HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/graph-paper-look-tp4308827p4308906.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.
Re: [R] problem with table.CAPM in PerformanceAnalytics
Dom Pazzula wrote All, I'm attempting to run this: table.CAPM(series[,Strat.Return,drop=FALSE],series[,spy.Return,drop=FALSE]) and getting this error Error in as.vector(data[, i]) : subscript out of bounds I've searched around and cannot find a solution to the problem. I've used this in the past without problem and I'm not sure what I did different this time. Other functions from that package like chat.CumReturns() work OK with this series. CAPM.Beta() does not. You can get the series timeSeries at http://www.pazzula.com/blog/series.rda Thanks in advance. Dominic J. Pazzula +-+-+-+-+-+-+-+-+-+-+- dompazz@ [[alternative HTML version deleted]] __ R-help@ 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. Hi Dom I notice that if I change your input series to an xts object things seem to work ... table.CAPM(as.xts(series[,Strat.Return,drop=FALSE]), as.xts(series[,spy.Return,drop=FALSE])) Strat.Return to spy.Return Alpha 0.0013 Beta0.1921 Beta+ 0.6830 Beta- -0.0803 R-squared 0.0374 Annualized Alpha0.3990 Correlation 0.1934 Correlation p-value 0. Tracking Error 0.7447 Active Premium 0.3694 Information Ratio 0.4960 Treynor Ratio 1.8885 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/problem-with-table-CAPM-in-PerformanceAnalytics-tp4298267p4298351.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.
Re: [R] add column with values found in another data frame
jdog76 wrote I am positive this problem has a very simple solution, but I have been unable to find it, so I am asking for your help. I need to know how to look something up in one data frame and add it as a column in another. If I have a data frame that looks like this: frame1 ID score test age 1 Guy1 10 1 20 2 Guy1 13 2 20 3 Guy2 9 1 33 4 Guy2 11 2 33 and another frame that looks like this: frame2 ID 1 Guy1 2 Guy2 How do I add a column to frame2 so it looks like this: ID age 1 Guy1 20 2 Guy2 33 I know this must be simple, but I couldn't find the solution by searching. thanks so much Jeremy How about frame2$age = frame1[match(frame2$ID, frame1$ID),age] print(frame2) ID age 1 Guy1 20 2 Guy2 33 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/add-column-with-values-found-in-another-data-frame-tp4295626p4296307.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.
Re: [R] Display number in currency notation with commas
eigenvalet wrote How can I display a number as currency including a $ sign and commas? -- View this message in context: http://r.789695.n4.nabble.com/Display-number-in-currency-notation-with-commas-tp4253460p4253460.html Sent from the R help mailing list archive at Nabble.com. __ R-help@ 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. One way myNum= paste($,format(myNum, big.mark=,),sep=) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Display-number-in-currency-notation-with-commas-tp4253582p4253695.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.
[R] Renaming Within A Function
I am trying to rename column names in a dataframe within a function. I am seeing an error (listed below) that I don't understand. Would be grateful of an explanation of what I am doing wrong and how I should rewrite the function to allow me to be able to rename my variables. Thanks. # Test Function myfunc -function(var){ d = c(1,2,3,4,5) dts = seq(as.Date(2011-01-01),by=month,length.out=length(d)) assign(paste(var,.df,sep=), cbind(dts, d)) names(get(paste(var,.df,sep=))) - c(Dates,Data) } # Call Function myfunc(X) # Error Message Error in names(get(paste(var, .df, sep = ))) - c(Dates, Data) : could not find function get- -- View this message in context: http://r.789695.n4.nabble.com/Renaming-Within-A-Function-tp4226368p4226368.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.
Re: [R] Group several variables and apply a function to the group
Aurélien PHILIPPOT wrote Dear R-experts, I am struggling with the following problem, and I am looking for advice from more experienced R-users: I have a data frame with 2 identifying variables (comn and mi), and an output variable (x). comn is a variable for a company and mi is a variable for a month. comn-c(abc, abc, abc, abc, abc, abc, xyz, xyz,xyz, xyz) mi- c(1, 1,1, 2, 2, 2, 1, 1, 3, 3) x- c(-0.0031, 0.0009, -0.007, 0.1929,0.0087, 0.099,-0.089, 0.005, -0.0078, 0.67 ) df- data.frame(comn=comn, mi=mi, x=x) For each company, within a particular month, I would like to compute the standard deviation of x: for example, for abc, I would like to compute the sd of x for month1 (when mi=1) and for month2 (when mi=2). In other languages (Stata for instance), I would create a grouping variable (group comnn and mi) and then, apply the sd function for each group. However, I don't find an elegant way to do the same in R: I was thinking about the following: I could subset my data frame by mi and create one file per month, and then make a loop and in each file, use a by operator for each comn. I am sure it would work, but I feel that it would be like killing an ant with a tank. I was wondering if anyone knew a more straightforward way to implement that kind of operation? Thanks a lot, Best, Aurelien [[alternative HTML version deleted]] __ R-help@ 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. One way would be to use the aggregate function. # Your Data ... # Note: I have removed the quotes off the output variable x comn-c(abc, abc, abc, abc, abc, abc, xyz, xyz,xyz, xyz) mi- c(1, 1,1, 2, 2, 2, 1, 1, 3, 3) x- c(-0.0031, 0.0009, -0.007, 0.1929,0.0087, 0.099,-0.089, 0.005, -0.0078, 0.67) df- data.frame(comn=comn, mi=mi, x=x) # Aggregate Function aggregate(df$x, by=list(df$comn,df$mi),FUN=sd) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Group-several-variables-and-apply-a-function-to-the-group-tp4158017p4158090.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.
Re: [R] lapply and Two TimeStamps as input
alaios wrote: Dear all, I have a function that recognizes the following format for timestamps %Y-%m-%d %H:%M:%S my function takes two input arguments the TimeStart and TimeEnd I would like to help me create the right list with pairs of TimeStart and TimeEnd which I can feed to lapply (I am using mclapply actually). For every lapply I want two inputs to be fed to my function. I only know how to feed one input to the lapply Could you please help me with that? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]] __ R-help@ 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. Is this of any use? # Data in list - first time is start time, second time is end time myListStartEnd = list(c(strptime(2010-01-01 09:00:00,%Y-%m-%d %H:%M:%S), strptime(2011-01-01 11:30:00,%Y-%m-%d %H:%M:%S)), c(strptime(2010-12-01 10:00:00,%Y-%m-%d %H:%M:%S), strptime(2010-12-25 06:00:00,%Y-%m-%d %H:%M:%S))) lapply(myListStartEnd,function(x) x[2]-x[1]) # Output [[1]] Time difference of 365.1042 days [[2]] Time difference of 23.8 days HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/lapply-and-Two-TimeStamps-as-input-tp3961939p3962139.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.
Re: [R] Extracting dataframe rows containing NAs in one column
kaallen wrote: Hi, I am working on a data set which looks like this: head(temp) Day Month Year PW ROW 1 1 1 1959 NA 6.40 2 2 1 1959 6.65 6.35 3 3 1 1959 2.50 3.60 4 4 1 1959 0.60 2.25 5 5 1 1959 0.85 0.30 6 6 1 1959 0.00 2.20 I am trying to extract all the rows containing NA. I can extrat all the rows without NAs in column 4 using PWna-temp[complete.cases(temp[,4]),] head(PWna) Day Month Year PW ROW 2 2 1 1959 6.65 6.35 3 3 1 1959 2.50 3.60 4 4 1 1959 0.60 2.25 5 5 1 1959 0.85 0.30 6 6 1 1959 0.00 2.20 7 7 1 1959 1.40 1.65 But can't figure out how to extract the rows WITH the NAs. Can anyone advise me? Thanks in advance I am sure others will have cleaner approaches but how about # Data temp = data.frame(Day=1:6,Month=1, Year=c(NA,1959,NA,1959,1959,1959), PW=c(6,7,8,NA,10,11)) # Extract Any Row Containing an NA myNAs = temp[apply(temp,1,function(x) any(is.na(x))),] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Extracting-dataframe-rows-containing-NAs-in-one-column-tp3937361p3937523.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.
Re: [R] column subtraction by row
Vining, Kelly wrote: Dear UseRs, I have a data frame that looks like this: head(test2) attributes start end StemExplant Callus RegenPlant 1 LTR_Unknown 120 535 3.198 1.931 1.927 3 LTR_Unknown 2955 3218 0.541 0.103 0.613 6 LTR_Unknown 6210 6423 6.080 4.650 9.081 9 LTR_Unknown 9658 10124 0.238 0.117 0.347 14 LTR_Unknown 14699 14894 3.545 3.625 2.116 25 LTR_Unknown 33201 33474 1.275 1.194 0.591 I need to subtract each value in the end column from its corresponding value in the start column, then append that difference as a new column in this data frame. It seems like apply could be the way to approach this, but I can't see any easy way to designate difference as a function, like, say, sum or mean. Plus, all the apply/lapply examples I'm looking at seem to depend on a data frame being just the two columns on which to operate, without any way to designate which columns to use in the function(x,y) part of an lapply statement. Another alternative would be a for loop, but when I try this: for(i in 1:nrow(test2)) { testout[i] - (test2$end[i] - test2$start[i]) } I get an error. So I'm stuck at the first step here. I think that once I can figure out how to get the differences, I can use cbind to append the data frame. But if there is a better way to do it, I'd like to know that as well. Any help is appreciated. --Kelly V. __ R-help@ 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. IS this what you are looking for? # Data lines - attributes start end StemExplant Callus RegenPlant LTR_Unknown 120 535 3.198 1.931 1.927 LTR_Unknown 2955 3218 0.541 0.103 0.613 LTR_Unknown 6210 6423 6.080 4.650 9.081 LTR_Unknown 9658 10124 0.238 0.117 0.347 LTR_Unknown 14699 14894 3.545 3.625 2.116 LTR_Unknown 33201 33474 1.275 1.194 0.591 d = read.table(textConnection(lines), header=TRUE) # Create new variable d$new=d$end - d$start print(d) attributes start end StemExplant Callus RegenPlant new 1 LTR_Unknown 120 535 3.198 1.931 1.927 415 2 LTR_Unknown 2955 3218 0.541 0.103 0.613 263 3 LTR_Unknown 6210 6423 6.080 4.650 9.081 213 4 LTR_Unknown 9658 10124 0.238 0.117 0.347 466 5 LTR_Unknown 14699 14894 3.545 3.625 2.116 195 6 LTR_Unknown 33201 33474 1.275 1.194 0.591 273 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/column-subtraction-by-row-tp3938399p3938461.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.
Re: [R] How to change the scale of the Y axis?
feargalr wrote: I am currently trying to create 3 histograms from 3 sets of data and in order to compare them I need them all to have a common scale, the Y axis is the only place its a problem, as one histogram only goes up to 4, another 5, and another 7 on the Y axis and obviously they all need to be in the same scale to allow comparisons. Can anyone help? Thanks, Feargal The ylim option in the hist function should do what you need A simple example with 2 sets of data # Create Data set.seed(123) d1 = 7*rnorm(100) d2 = runif(100) par(mfrow = c(2,1)) hist(d1, ylim=c(0,50)) hist(d2, ylim=c(0,50)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/How-to-change-the-scale-of-the-Y-axis-tp3883843p3884112.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.
Re: [R] Keep ALL duplicate records
Erik Svensson wrote: Hello, In a data frame I want to identify ALL duplicate IDs in the example to be able to examine OS and time. (df-data.frame(ID=c(userA, userB, userA, userC), OS=c(Win,OSX,Win, Win64), time=c(12:22,23:22,04:44,12:28))) IDOS time 1 userA Win 12:22 2 userB OSX 23:22 3 userA Win 04:44 4 userC Win64 12:28 My desired output is that ALL records with the same IDs are found: userA Win 12:22 userA Win 04:44 preferably by returning logical values (TRUE FALSE TRUE FALSE) Is there a simple way to do that? [-- With duplicated(df$ID) the output will be [1] FALSE FALSE TRUE FALSE i.e. not all user A records are found With unique(df$ID) [1] userA userB userC Levels: userA userB userC i.e. one of each ID is found --] Erik Svensson How about ... # All records ALL_RECORDS - df[df$ID==df$ID[duplicated(df$ID)],] print(ALL_RECORDS) # Logical Records TRUE_FALSE - df$ID==df$ID[duplicated(df$ID)] print(TRUE_FALSE) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Keep-ALL-duplicate-records-tp3865136p3865573.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.
[R] Loop with random sampling and write.table
Hi! I need to perform this simple sampling function several hundred times: x1=as.character(rnorm(1000, 100, 15)) x2=as.character(rnorm(1000, 150, 10)) y1=as.data.frame(x1,x2) sample1=as.data.frame(sample(y1$x1, 12, replace = FALSE, prob = NULL)) sample1 write.table(sample1, sample1.txt, sep= ,row.names=F,quote=F) My knowledge of loops is quite low. How can I produce 100 loops of the sampling leading to 100 files from sample1.txt to sample100.txt? Thanks for you help! -- View this message in context: http://r.789695.n4.nabble.com/Loop-with-random-sampling-and-write-table-tp3787895p3787895.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.
Re: [R] Saving a list as a Matrix
wizykid wrote: Hi there. I went through the manual but I couldn't find a solution for my problem. I have list like this one : lst1 [[1]] [1] 0 1 2 3 [[2]] [1] 0 1 5 [[3]] [1] 2 3 4 and I want to save it as Matrix in Matlab mat format like : 0 1 2 3 0 1 5 0 2 3 4 0 can any body help me ? Appreciate your help and thanks in advance. Reza Not pretty but this works ... lst1 = list(c(0,1,2,3),c(0,1,5),c(2,3,4)) t(sapply(lst1, function(x) c(x,rep(0,4-length(x) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Saving-a-list-as-a-Matrix-tp3788274p3788327.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.
Re: [R] Basic vector logic not working
DimmestLemming wrote: I am interning in a computer science lab and I'm very new to R. The language basics are clear, but this particular problem isn't: I have a very large dataframe called data which holds data from Halo matches. I'm trying to analyze a certain window such that data$deaths20 and data$deaths=27. When I enter the line kills = data$kills[data$deaths20] or any single condition, it selects the region I want. However, when I input kills = data$kills[data$deaths20 data$deaths=27] kills becomes the entire vector data$kills, without selecting any specific region. This is a simple problem, but I can't find anything wrong. How could I fix it? Maybe something like ... # example data d = data.frame(age=1:20, names=letters[1:20]) # 1. subset using [ d[d$age10 d$age16,] # 2. subset using subset function subset(d,d$age10 d$age16) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Basic-vector-logic-not-working-tp3656465p3656594.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.
Re: [R] Reshape from long to wide format with date variable
Thanks, Josh! The index variable (time) was my problem. My R skills are too low! :) Problem solved! -- View this message in context: http://r.789695.n4.nabble.com/Reshape-from-long-to-wide-format-with-date-variable-tp3648833p3650995.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.
[R] Reshape from long to wide format with date variable
Hi, I need to reshape my dataframe from a long format to a wide format. Unfortunately, I have a continuous date variable which gives me headaches. Consider the following example: id=c(034,034,016,016,016,340,340) date=as.Date(c(1997-09-28, 1997-10-06, 1997-11-04, 2000-09-27, 2003-07-20, 1997-11-08, 1997-11-08)) ref=c(2,2,1,1,2,1,1) data1=data.frame(id,date,ref) data1 id date ref 1 034 1997-09-28 2 2 034 1997-10-06 2 3 016 1997-11-04 1 4 016 2000-09-27 1 5 016 2003-07-20 2 6 340 1997-11-08 1 7 340 1997-11-08 1 I would like to have it like this: data2 id date1 date2 date3 ref1 ref2 ref3 1 034 1997-09-28 1997-10-06 NA22 NA 2 016 1997-11-04 2000-09-27 2003-07-20112 3 340 1997-11-08 1997-11-08 NA11 NA All I tried the reshape package but ended up in multiple variables for each of the dates and that is not what I would like to have. Thanks for you help. -- View this message in context: http://r.789695.n4.nabble.com/Reshape-from-long-to-wide-format-with-date-variable-tp3648833p3648833.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.
[R] chull increase number of points
Dear R-help, I am using the chull function to create a convex hull of a series of about 20,000 data points. A pain is temporary, glory is forever! Powered by Linux. www.linux.org Scanned for viruses using ClamAV. www.clamav.net. [[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] chull increase number of points
Dear R-help, I am using the chull function to create a convex hull of a series of about 20,000 data points. A [[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] Recode numbers
Lisa wrote: Dear all, I have two sets of numbers that look like a - c(1, 2, 3, 3, 4, 4, 5, 6, 1, 2, 2, 3, 1, 2, 1, 2, 3, 3, 4, 5, 1, 2, 3, 4) b - c(1, 5, 8, 9, 14, 20, 3, 10, 12, 6, 16, 7, 11, 13, 17, 18, 2, 4, 15, 19) I just want to use “b” to encode “a” so that “a” looks like a1- c(1, 5, 8, 8, 9, 9, 14, 20, 3, 10, 10, 12, 6, 16, 7, 11, 13, 13, 17, 18, 2, 4, 15, 19) Does anyone have a suggestion how to deal with this? Thank you in advance. Lisa is a1 = b[a] what you are looking for? HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566505.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.
[R] zoo column names
I have a zoo object that contains 2 time series named A-B and V1. When I create a third series V2, the name of the A-B series is changed to A.B. Although I could recreate the names for the 3 series I am wondering if there is a way of preventing the name change from happening ( ... maybe an equivalent of the keep.names=TRUE statement on merge.zoo)? # library(zoo) # Create zoo data with required col names d = data.frame(c(10,20,30),c(1,2,3)) names(d) = c(A-B, V1) d.z = zoo(d,1:3) # Create new variable V2 # col name changes from A-B to A.B d.z$V2 = d.z[,V1] *100 # recreate col names names(d.z) = c(names(d),V2) #- Thanks Pete -- View this message in context: http://r.789695.n4.nabble.com/zoo-column-names-tp3553939p3553939.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.
Re: [R] zoo column names
Gabor Grothendieck wrote: On Thu, May 26, 2011 at 7:35 PM, Pete Brecknock lt;peter.breckn...@bp.comgt; wrote: I have a zoo object that contains 2 time series named A-B and V1. When I create a third series V2, the name of the A-B series is changed to A.B. Although I could recreate the names for the 3 series I am wondering if there is a way of preventing the name change from happening ( ... maybe an equivalent of the keep.names=TRUE statement on merge.zoo)? # library(zoo) # Create zoo data with required col names d = data.frame(c(10,20,30),c(1,2,3)) names(d) = c(A-B, V1) d.z = zoo(d,1:3) # Create new variable V2 # col name changes from A-B to A.B d.z$V2 = d.z[,V1] *100 # recreate col names names(d.z) = c(names(d),V2) #- Its doing a merge behind the scenes and this is a side effect of that. I agree that its strange behavior and will look at it. In the meantime try performing an explicit merge so that you can control it using check.names: d.z - zoo(cbind(`A-B` = c(10, 20, 30), V1 = c(1, 2, 3))) merge(d.z, V2 = 100*d.z$V1, check.names = FALSE) A-B V1 V2 1 10 1 100 2 20 2 200 3 30 3 300 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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. Thanks Gabor. Much appreciated. -- View this message in context: http://r.789695.n4.nabble.com/zoo-column-names-tp3553939p3554049.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.
Re: [R] ARMA
boki2b wrote: Hello,Could somebody tell me what is the difference between theese 3 calls of functionsarma(x,order=c(1,0)), arima(x,order=c(1,0,0)) ar(x,order=1)?I expected same residuals of theese three models,but unexpectably for the first two R requiredinitial value of something (what?)...Thanks in advance! [[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. Firstly, all of the functions you mention can be explored in more detail by typing ?xxx, where xxx is ar, arima or arma. Note, you will need the tseries package to run the arma function. (require(tseries)) Secondly, there is some debate around interpretation of some of the output from these functions. Check out one viewpoint at http://www.stat.pitt.edu/stoffer/tsa2/Rissues.htm (ISSUE 1: When is the intercept the mean?) Finally, I have tried below to put the functions on a similar footing to allow you to compare their output. # create data set.seed(12345) d = rnorm(30) library(tseries) # need for arma function # 1. arma # fits using conditional least squares, intercept is the intercept arma(d,order=c(1,0)) ar1 intercept -0.105660.07046 # 2. arima # intercept is the mean, intercept derived as mean*(1-AR1) arima(d,order=c(1,0,0),method=CSS) ar1 intercept -0.1057 0.0638 So, intercept = 0.0638 * (1 + 0.1057) = 0.0705 ... same as arma above # 3. ar intercept is the intercept ar(d,aic=FALSE, order.max=1,method=ols,intercept=TRUE, demean=FALSE) 1 -0.1057 Intercept: -0.07054 (0.1731) There is a nice document on CRAN that you may find useful. http://cran.r-project.org/doc/contrib/Ricci-refcard-ts.pdf HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/ARMA-tp3507846p3507963.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.
Re: [R] how to calculate the mean of a group in a table
tornanddesperate wrote: Hi its me again I don't mean to get on your nerves, but the use of R proofs to be a bit more complicated than envisaged. I would like to calculate the mean of a group of values, here wage_accepted. The group is determined by the stage and period, so in the end there should be a column with the values of the wages in period 1 stage1, period 1 stage 2, period 2 stage1... Unfortunately, I haven't much of a clue on how to program this. Could you tell me how the function should roughly look like – if-else, loops included or not – in the end ? A couple of possible approaches # read in your data lines -treatment session period stage wage_accepted type 1 1 1 125 low 1 1 1 119 low 1 1 1 115 low 1 1 1 232 high 1 1 1 213 low 1 1 1 214 low 1 1 2 117 low 1 1 2 112 low d - read.table(textConnection(lines), header = TRUE) closeAllConnections() # 1. using the aggregate function aggregate(d$wage_accepted, list(period=d$period, stage=d$stage), FUN=mean) # 2. using the by function by(d$wage_accepted,list(d$period,d$stage),FUN=mean) As for tutorial resources, I would recommend visiting CRAN at http://cran.r-project.org/ ... follow the Manuals link on the left hand side of the page. HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-the-mean-of-a-group-in-a-table-tp3505986p3506024.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.
Re: [R] Creating binary variable depending on strings of two dataframes
Gabor Grothendieck wrote: On Tue, Dec 7, 2010 at 11:30 AM, Pete Pete lt;noxyp...@gmail.comgt; wrote: Hi, consider the following two dataframes: x1=c(232,3454,3455,342,13) x2=c(1,1,1,0,0) data1=data.frame(x1,x2) y1=c(232,232,3454,3454,3455,342,13,13,13,13) y2=c(E1,F3,F5,E1,E2,H4,F8,G3,E1,H2) data2=data.frame(y1,y2) I need a new column in dataframe data1 (x3), which is either 0 or 1 depending if the value E1 in y2 of data2 is true while x1=y1. The result of data1 should look like this: x1 x2 x3 1 232 1 1 2 3454 1 1 3 3455 1 0 4 342 0 0 5 13 0 1 I think a SQL command could help me but I am too inexperienced with it to get there. Try this: library(sqldf) sqldf(select x1, x2, max(y2 = 'E1') x3 from data1 d1 left join data2 d2 on (x1 = y1) group by x1, x2 order by d1.rowid) x1 x2 x3 1 232 1 1 2 3454 1 1 3 3455 1 0 4 342 0 0 5 13 0 1 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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. That works pretty cool but I need to automate this a bit more. Consider the following example: list1=c(A01,B04,A64,G84,F19) x1=c(232,3454,3455,342,13) x2=c(1,1,1,0,0) data1=data.frame(x1,x2) y1=c(232,232,3454,3454,3455,342,13,13,13,13) y2=c(E13,B04,F19,A64,E22,H44,F68,G84,F19,A01) data2=data.frame(y1,y2) I want now to creat a loop, which creates for every value in list1 a new binary variable in data1. Result should look like: x1 x2 A01 B04 A64 G84 F19 232 1 0 1 0 0 0 34541 0 0 1 0 1 34551 0 0 0 0 0 342 0 0 0 0 0 0 13 0 1 0 0 1 1 Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Creating-binary-variable-depending-on-strings-of-two-dataframes-tp3076724p3503334.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.
Re: [R] Element by Element addition of the columns of a Matrix
... is the apply function what you are looking for? A=matrix(1,2,4) apply(A,1,sum) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Element-by-Element-addition-of-the-columns-of-a-Matrix-tp3483545p3483628.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.
Re: [R] Cointegration test of panel data
You could try the urca package Also, I would maybe have a look a the CRAN Task View on computational econometrics at http://cran.r-project.org/web/views/Econometrics.html HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Cointegration-test-of-panel-data-tp3438783p3438864.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.
Re: [R] cumsum while maintaining NA
Here's one way . Lines-x1 x2 x3 x4 x5 x6 NA NA 3 4 NA NA 5 3 4 NA NA NA 7 3 4 4 NA NA 11 3 4 5 NA NA 67 4 4 NA NA NA d - read.table(textConnection(Lines), header = TRUE, colClasses=c(integer)) closeAllConnections() res = t(apply(d, 1, function(x) ave(x,is.na(x),FUN=cumsum))) print(res) x1 x2 x3 x4 x5 x6 [1,] NA NA 3 7 NA NA [2,] 5 8 12 NA NA NA [3,] 7 10 14 18 NA NA [4,] 11 14 18 23 NA NA [5,] 67 71 75 NA NA NA HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/cumsum-while-maintaining-NA-tp3421513p3422619.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.
Re: [R] List extraction
Doesn't use apply but maybe the following will work for you? temp1- data.frame(ID=c(Herb,Shrub),stat=c(4,5),pvalue=c(.03,.04)) temp2- data.frame(ID=c(Herb,Shrub, Tree),stat=c(12,15,13),pvalue=c(.2,0.4,.3)) L-list(a=temp1,b=temp2) d.f = do.call(rbind,L) d.f$tableName = substring(rownames(x),1,1) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/List-extraction-tp3413374p3413564.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.
[R] Reference Lines Using Grid Graphics
Hi I would like to be able to add reference lines to a series of plots that are built using the Grid graphics package. These lines should coincide with tick marks which are different on each plot. I can add the lines manually using the grid.lines() function but would like to understand how to generate them on the fly. Any help would be gratefully received. Kind regards Pete ### # FUNCTION MYPLOT ### myplot=function(i,j){ pushViewport(viewport(layout.pos.col=i,layout.pos.row=j)) pushViewport(plotViewport(c(3,2,2,3))) x - runif(10) y - (i+j)*runif(10) pushViewport(dataViewport(range(x),pretty(y),name=plotRegion)) grid.points(x, y) grid.rect() grid.xaxis() grid.yaxis(main=FALSE) # How can I use grid.lines to create reference lines at the tick marks for each graph? # grid.lines(x=0:1,y=0.2) grid.text(paste(Plot ,i, ,j,sep=), y = unit(1, npc) + unit(0.5, lines), gp = gpar(fontsize = 16)) popViewport() popViewport(2) } ### # END FUNCTION MYPLOT ### grid.newpage() grid.rect(gp=gpar(fill=white)) pushViewport(viewport(layout=grid.layout(2, 2))) myplot(1,1) myplot(1,2) myplot(2,1) myplot(2,2) popViewport() -- View this message in context: http://r.789695.n4.nabble.com/Reference-Lines-Using-Grid-Graphics-tp3349185p3349185.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.
Re: [R] Reference Lines Using Grid Graphics
Apologies I forgot to include that the reference lines should be for the y axis only. Thanks. Pete -- View this message in context: http://r.789695.n4.nabble.com/Reference-Lines-Using-Grid-Graphics-tp3349185p3349206.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.
Re: [R] Reference Lines Using Grid Graphics
Thanks Gabor. I owe you again. Kind regards Pete -- View this message in context: http://r.789695.n4.nabble.com/Reference-Lines-Using-Grid-Graphics-tp3349185p3349259.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.
[R] does rpy support R 2.12.2
Hi, I am getting the following error when I try to run import rpy from the the python IDE: Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python2.6/dist-packages/rpy.py, line 134, in module % RVERSION) RuntimeError: No module named _rpy2122 RPy module can not be imported. Please check if your rpy installation supports R 2.12.2. If you have multiple R versions installed, you may need to set RHOME before importing rpy. For example: from rpy_options import set_options set_options(RHOME='c:/progra~1/r/rw2011/') from rpy import * I am wondering if rpy supports R 2.12.2? Thanks [[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] Calculate a mean for several months for several years
For 5 years set.seed = 1 d=data.frame(year=rep(2007:2011,each=12), month=rep(1:12,5), meanTemp = rnorm(60,10,5)) meanByMonth = ave(d$meanTemp, d$month, FUN = mean)[7:9] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Calculate-a-mean-for-several-months-for-several-years-tp3318363p3318501.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.
Re: [R] From SPSS Syntax to R code
You might want to take a look at Bob Muenchen's book R for SAS and SPSS Users There is an 80 page preview at .. http://rforsasandspssusers.com/ Additionally, there is lots of documentation for getting started with R on the CRAN website http://cran.r-project.org/ HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/From-SPSS-Syntax-to-R-code-tp3302666p3302707.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.
Re: [R] Downloading SP monthly data into R
1. Using the quantmod package. Look at ?getSymbols e.g. getSymbols(^GSPC,src=yahoo) then use the to.monthly function 2. Using the tseries package . Look at ?get.hist.quote e.g. get.hist.quote(instrument = ^GSPC, start = as.yearmon(Jan 1950) ,compression = m, quote = Close) There will be many other approaches. HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Downloading-S-P-monthly-data-into-R-tp3302339p3302395.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.
Re: [R] expression for index of pairwise combinations
If I have understood correctly, then as an example for 4 columns how about d=data.frame(C1=c(1,1,1,1,1),C2=c(2,2,2,2,2),C3=c(3,3,3,3,3),C4=c(4,4,4,4,4)) combs=combn(paste(C,seq_len(4),sep=),2,simplify=FALSE) one = sapply(combs,function(x) d[x[1]]) two = sapply(combs,function(x) d[x[2]]) ret = mapply(cbind,one,two) colnames(ret) = paste(C,1:length(combs),sep=) You will need to change seq_len(4) to seq_len(269) in the second line. HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/expression-for-index-of-pairwise-combinations-tp3263025p3298495.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.
Re: [R] clustering fuzzy
After ordering the table of membership degrees , i must get the difference between the first and second coloumns , between the first and second largest membership degree of object i. This for K=2,K=3,to K.max=6. This difference is multiplyed by the Crisp silhouette index vector (si). Too it dependending on K=2,...,K.max=6; the result divided by the sum of these differences I need a final vector composed of the indexes for each clustering (K=2,...,K.max=6). There is a method, i think that is classe.memb, but i can't to solve problem because trasformation of the membership degrees matrix( (ris$membership) and of list object (ris$silinfo), does not permitme to use classe.memb propertyes. . Σí(uί1-uí2)sí/Σí(uí1-uí2) head(t(A.sort)) membership degrees table ordering by max to min value [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 head(t(A.sort)) [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 H.Asort=head(t(A.sort)) H.Asort[,1]-H.Asort[,2] 123456 0.36 0.80 0.86 0.50 0.75 0.87 H.Asort=t(H.Asort[,1]-H.Asort[,2]) This is the differences vector by multiplying trasformed table ris$silinfo. ris$silinfo $widths cluster neighbor sil_width 72 13 0.43820207 54 13 0.43427773 29 16 0.41729079 62 16 0.40550562 64 16 0.32686757 32 13 0.30544722 45 13 0.30428723 79 13 0.30192624 12 13 0.30034472 60 16 0.29642495 41 13 0.29282778 113 0.28000788 85 13 0.24709237 74 13 0.239 P=ris$silinfo P=P[1] P=as.data.frame(P) V4=rownames(P) mode(V4)=numeric P[,4]=V4 P[order(P$V4),] widths.cluster widths.neighbor widths.sil_width V4 1 1 3 0.28000788 1 2 2 4 0.07614849 2 3 2 3 -0.11676440 3 4 2 4 0.15436648 4 5 2 3 0.14693927 5 6 3 1 0.57083836 6 7 4 5 0.36391826 7 8 5 4 0.63491118 8 9 4 2 0.54458733 9 10 5 4 0.51059626 10 11 2 5 0.03908952 11 12 1 3 0.30034472 12 13 1 3 -0.04928562 13 14 4 3 0.20337180 14 15 3 4 0.46164324 15 18 5 4 0.52066782 18 20 4 3 0.45517287 20 21 3 4 0.39405507 21 22 4 5 0.05574547 22 23 6 1 -0.06750403 23 P= P[order(P$V4),] P=P[,3] This is trasformed vector ris$silinfo =P. I can't to use this vector object in the classe.memb. K=2 K.max=6 while (K=K.max) { ris=fanny(frj,K,memb.exp=m,metric=SqEuclidean,stand=TRUE,maxit=1000,tol=1e-6) ris$centroid=matrix(0,nrow=K,ncol=J) for (k in 1:K) { ris$centroid[k,]=(t(ris$membership[,k]^m)%*%as.matrix(frj))/sum(ris$membership[,k]^m) } rownames(ris$centroid)=1:K colnames(ris$centroid)=colnames(frj) print(K) print(round(ris$centroid,2)) print(classe.memb(ris$membership)$table.U) print(ris$silinfo$avg.width) K=K+1 } this should be scheme clearly are determined centroid based on classe.memb. classe.memb=function(U) { info.U=cbind(max.col(U),apply(U,1,max)) i=1 while (i = nrow(U)) { if (apply(U,1,max)[i]0.5) info.U[i,1]=0 i=i+1 } K=ncol(U) table.U=matrix(0,nrow=K,ncol=4) cl=1 while (cl = K) { table.U[cl,1] = length(which(info.U[info.U[,1]==cl,2]=.90)) table.U[cl,2] = length(which(info.U[info.U[,1]==cl,2]=.70)) - table.U[cl,1] table.U[cl,3] = length(which(info.U[info.U[,1]==cl,2]=.50)) - table.U[cl,1] - table.U[cl,2] table.U[cl,4] = sum(table.U[cl,]) cl = cl+1 } rownames(table.U) = c(1:K) colnames(table.U) = c(Alto, Medio, Basso, Totale) out=list() out$info.U=round(info.U,2) out$table.U=table.U return(out) } -- View this message in context: http://r.789695.n4.nabble.com/clustering-fuzzy-tp3229853p3261837.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.
[R] clustering fuzzy
After ordering the table of membership degrees , i must get the difference between the first and second coloumns , between the first and second largest membership degree of object i. This for K=2,K=3,to K.max=6. This difference is multiplyed by the Crisp silhouette index vector (si). Too it dependending on K=2,...,K.max=6; the result divided by the sum of these differences I need a final vector composed of the indexes for each clustering (K=2,...,K.max=6). There is a method, i think that is classe.memb, but i can't to solve problem because trasformation of the membership degrees matrix( (ris$membership) and of list object (ris$silinfo), does not permitme to use classe.memb propertyes. . Σí(uί1-uí2)sí/Σí(uí1-uí2) head(t(A.sort)) membership degrees table ordering by max to min value [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 head(t(A.sort)) [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 H.Asort=head(t(A.sort)) H.Asort[,1]-H.Asort[,2] 123456 0.36 0.80 0.86 0.50 0.75 0.87 H.Asort=t(H.Asort[,1]-H.Asort[,2]) This is the differences vector by multiplying trasformed table ris$silinfo. ris$silinfo $widths cluster neighbor sil_width 72 13 0.43820207 54 13 0.43427773 29 16 0.41729079 62 16 0.40550562 64 16 0.32686757 32 13 0.30544722 45 13 0.30428723 79 13 0.30192624 12 13 0.30034472 60 16 0.29642495 41 13 0.29282778 113 0.28000788 85 13 0.24709237 74 13 0.239 P=ris$silinfo P=P[1] P=as.data.frame(P) V4=rownames(P) mode(V4)=numeric P[,4]=V4 P[order(P$V4),] widths.cluster widths.neighbor widths.sil_width V4 1 1 3 0.28000788 1 2 2 4 0.07614849 2 3 2 3 -0.11676440 3 4 2 4 0.15436648 4 5 2 3 0.14693927 5 6 3 1 0.57083836 6 7 4 5 0.36391826 7 8 5 4 0.63491118 8 9 4 2 0.54458733 9 10 5 4 0.51059626 10 11 2 5 0.03908952 11 12 1 3 0.30034472 12 13 1 3 -0.04928562 13 14 4 3 0.20337180 14 15 3 4 0.46164324 15 18 5 4 0.52066782 18 20 4 3 0.45517287 20 21 3 4 0.39405507 21 22 4 5 0.05574547 22 23 6 1 -0.06750403 23 P= P[order(P$V4),] P=P[,3] This is trasformed vector ris$silinfo =P. I can't to use this vector object in the classe.memb. K=2 K.max=6 while (K=K.max) { ris=fanny(frj,K,memb.exp=m,metric=SqEuclidean,stand=TRUE,maxit=1000,tol=1e-6) ris$centroid=matrix(0,nrow=K,ncol=J) for (k in 1:K) { ris$centroid[k,]=(t(ris$membership[,k]^m)%*%as.matrix(frj))/sum(ris$membership[,k]^m) } rownames(ris$centroid)=1:K colnames(ris$centroid)=colnames(frj) print(K) print(round(ris$centroid,2)) print(classe.memb(ris$membership)$table.U) print(ris$silinfo$avg.width) K=K+1 } this should be scheme clearly are determined centroid based on classe.memb. classe.memb=function(U) { info.U=cbind(max.col(U),apply(U,1,max)) i=1 while (i = nrow(U)) { if (apply(U,1,max)[i]0.5) info.U[i,1]=0 i=i+1 } K=ncol(U) table.U=matrix(0,nrow=K,ncol=4) cl=1 while (cl = K) { table.U[cl,1] = length(which(info.U[info.U[,1]==cl,2]=.90)) table.U[cl,2] = length(which(info.U[info.U[,1]==cl,2]=.70)) - table.U[cl,1] table.U[cl,3] = length(which(info.U[info.U[,1]==cl,2]=.50)) - table.U[cl,1] - table.U[cl,2] table.U[cl,4] = sum(table.U[cl,]) cl = cl+1 } rownames(table.U) = c(1:K) colnames(table.U) = c(Alto, Medio, Basso, Totale) out=list() out$info.U=round(info.U,2) out$table.U=table.U return(out) -- View this message in context: http://r.789695.n4.nabble.com/clustering-fuzzy-tp3262027p3262027.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.
[R] (no subject)
-- View this message in context: http://r.789695.n4.nabble.com/no-subject-tp3262024p3262024.html Sent from the R help mailing list archive at Nabble.com. [[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] problem subsetting a data frame
try subset(D, D$x 5|D$y 5) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/problem-subsetting-a-data-frame-tp3258981p3259360.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.
Re: [R] clustering fuzzy
After ordering the table of membership degrees , i must get the difference between the first and second coloumns , between the first and second largest membership degree of object i. This for K=2,K=3,to K.max=6. This difference is multiplyed by the Crisp silhouette index vector (si). Too it dependending on K=2,...,K.max=6; the result divided by the sum of these differences I need a final vector composed of the indexes for each clustering (K=2,...,K.max=6). There is a method, i think that is classe.memb, but i can't to solve problem because trasformation of the membership degrees matrix( (ris$membership) and of list object (ris$silinfo), does not permitme to use classe.memb propertyes. . Σí(uί1-uí2)sí/Σí(uí1-uí2) head(t(A.sort)) membership degrees table ordering by max to min value [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 head(t(A.sort)) [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 H.Asort=head(t(A.sort)) H.Asort[,1]-H.Asort[,2] 123456 0.36 0.80 0.86 0.50 0.75 0.87 H.Asort=t(H.Asort[,1]-H.Asort[,2]) This is the differences vector by multiplying trasformed table ris$silinfo. ris$silinfo $widths cluster neighbor sil_width 72 13 0.43820207 54 13 0.43427773 29 16 0.41729079 62 16 0.40550562 64 16 0.32686757 32 13 0.30544722 45 13 0.30428723 79 13 0.30192624 12 13 0.30034472 60 16 0.29642495 41 13 0.29282778 113 0.28000788 85 13 0.24709237 74 13 0.239 P=ris$silinfo P=P[1] P=as.data.frame(P) V4=rownames(P) mode(V4)=numeric P[,4]=V4 P[order(P$V4),] widths.cluster widths.neighbor widths.sil_width V4 1 1 3 0.28000788 1 2 2 4 0.07614849 2 3 2 3 -0.11676440 3 4 2 4 0.15436648 4 5 2 3 0.14693927 5 6 3 1 0.57083836 6 7 4 5 0.36391826 7 8 5 4 0.63491118 8 9 4 2 0.54458733 9 10 5 4 0.51059626 10 11 2 5 0.03908952 11 12 1 3 0.30034472 12 13 1 3 -0.04928562 13 14 4 3 0.20337180 14 15 3 4 0.46164324 15 18 5 4 0.52066782 18 20 4 3 0.45517287 20 21 3 4 0.39405507 21 22 4 5 0.05574547 22 23 6 1 -0.06750403 23 P= P[order(P$V4),] P=P[,3] This is trasformed vector ris$silinfo =P. I can't to use this vector object in the classe.memb. K=2 K.max=6 while (K=K.max) { ris=fanny(frj,K,memb.exp=m,metric=SqEuclidean,stand=TRUE,maxit=1000,tol=1e-6) ris$centroid=matrix(0,nrow=K,ncol=J) for (k in 1:K) { ris$centroid[k,]=(t(ris$membership[,k]^m)%*%as.matrix(frj))/sum(ris$membership[,k]^m) } rownames(ris$centroid)=1:K colnames(ris$centroid)=colnames(frj) print(K) print(round(ris$centroid,2)) print(classe.memb(ris$membership)$table.U) print(ris$silinfo$avg.width) K=K+1 } this should be scheme clearly are determined centroid based on classe.memb. classe.memb=function(U) { info.U=cbind(max.col(U),apply(U,1,max)) i=1 while (i = nrow(U)) { if (apply(U,1,max)[i]0.5) info.U[i,1]=0 i=i+1 } K=ncol(U) table.U=matrix(0,nrow=K,ncol=4) cl=1 while (cl = K) { table.U[cl,1] = length(which(info.U[info.U[,1]==cl,2]=.90)) table.U[cl,2] = length(which(info.U[info.U[,1]==cl,2]=.70)) - table.U[cl,1] table.U[cl,3] = length(which(info.U[info.U[,1]==cl,2]=.50)) - table.U[cl,1] - table.U[cl,2] table.U[cl,4] = sum(table.U[cl,]) cl = cl+1 } rownames(table.U) = c(1:K) colnames(table.U) = c(Alto, Medio, Basso, Totale) out=list() out$info.U=round(info.U,2) out$table.U=table.U return(out) } -- View this message in context: http://r.789695.n4.nabble.com/clustering-fuzzy-tp3229853p3255223.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.
Re: [R] Select just numeric values from rows
Terry Any good? list -A B C N1 N2 N3 N4 N5 N6 N7 N8 N9 N10 Apples Bananas Peaches 1 2 3 4 5 6 7 8 9 10 Oranges Kumquats Plums 1 1 3 5 5 5 7 6 6 12 Pears Kiwis Grapes 2 4 6 8 5 6 7 6 9 1 d = read.table(textConnection(list), header=TRUE) Nrowsums = apply(d[,sapply(d,is.numeric)],1,sum) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Select-just-numeric-values-from-rows-tp3256237p3256670.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.
Re: [R] time bin sum
Jessica Any good? lines -DateTime, Q 2004-12-09 15:30:01, 2 2004-12-09 15:30:01, 1 2004-12-09 15:30:06, 1 2004-12-09 15:30:14, 5 2004-12-09 15:30:21, 1 2004-12-09 15:30:22, 11 2004-12-09 15:30:24, 4 2004-12-09 15:30:32, 1 2004-12-09 15:30:32, 1 2004-12-09 15:30:32, 3 2004-12-09 15:30:41, 4 d = read.table(textConnection(lines), sep=, ,header = TRUE) d$DateTime = as.POSIXct(d$DateTime) time - seq(as.POSIXct('2004-12-09 15:30:00'),by='5 sec', length=10) bins = cut(d$DateTime,breaks=time) counts = as.data.frame(tapply(d$Q,bins,sum)) # Clean up counts[is.na(counts)]=0 colnames(counts) = Counts print(counts) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/time-bin-sum-tp3252376p3253400.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.
[R] silhouette fuzzy
After ordering the table of membership degrees , i must get the difference between the first and second coloumns , between the first and second largest membership degree of object i. This for K=2,K=3,to K.max=6. This difference is multiplyed by the Crisp silhouette index vector (si). Too it dependending on K=2,...,K.max=6; the result divided by the sum of these differences I need a final vector composed of the indexes for each clustering (K=2,...,K.max=6). There is a method, i think that is classe.memb, but i can't to solve problem because trasformation of the membership degrees matrix( (ris$membership) and of list object (ris$silinfo), does not permitme to use classe.memb propertyes. . Σí(uί1-uí2)sí/Σí(uí1-uí2) head(t(A.sort)) membership degrees table ordering by max to min value [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 head(t(A.sort)) [,1] [,2] [,3] [,4] 1 0.66 0.30 0.04 0.01 2 0.89 0.09 0.02 0.00 3 0.92 0.06 0.01 0.01 4 0.71 0.21 0.07 0.01 5 0.85 0.10 0.04 0.01 6 0.91 0.04 0.02 0.02 H.Asort=head(t(A.sort)) H.Asort[,1]-H.Asort[,2] 123456 0.36 0.80 0.86 0.50 0.75 0.87 H.Asort=t(H.Asort[,1]-H.Asort[,2]) This is the differences vector by multiplying trasformed table ris$silinfo. ris$silinfo $widths cluster neighbor sil_width 72 13 0.43820207 54 13 0.43427773 29 16 0.41729079 62 16 0.40550562 64 16 0.32686757 32 13 0.30544722 45 13 0.30428723 79 13 0.30192624 12 13 0.30034472 60 16 0.29642495 41 13 0.29282778 113 0.28000788 85 13 0.24709237 74 13 0.239 P=ris$silinfo P=P[1] P=as.data.frame(P) V4=rownames(P) mode(V4)=numeric P[,4]=V4 P[order(P$V4),] widths.cluster widths.neighbor widths.sil_width V4 1 1 3 0.28000788 1 2 2 4 0.07614849 2 3 2 3 -0.11676440 3 4 2 4 0.15436648 4 5 2 3 0.14693927 5 6 3 1 0.57083836 6 7 4 5 0.36391826 7 8 5 4 0.63491118 8 9 4 2 0.54458733 9 10 5 4 0.51059626 10 11 2 5 0.03908952 11 12 1 3 0.30034472 12 13 1 3 -0.04928562 13 14 4 3 0.20337180 14 15 3 4 0.46164324 15 18 5 4 0.52066782 18 20 4 3 0.45517287 20 21 3 4 0.39405507 21 22 4 5 0.05574547 22 23 6 1 -0.06750403 23 P= P[order(P$V4),] P=P[,3] This is trasformed vector ris$silinfo =P. I can't to use this vector object in the classe.memb. K=2 K.max=6 while (K=K.max) { ris=fanny(frj,K,memb.exp=m,metric=SqEuclidean,stand=TRUE,maxit=1000,tol=1e-6) ris$centroid=matrix(0,nrow=K,ncol=J) for (k in 1:K) { ris$centroid[k,]=(t(ris$membership[,k]^m)%*%as.matrix(frj))/sum(ris$membership[,k]^m) } rownames(ris$centroid)=1:K colnames(ris$centroid)=colnames(frj) print(K) print(round(ris$centroid,2)) print(classe.memb(ris$membership)$table.U) print(ris$silinfo$avg.width) K=K+1 } this should be scheme clearly are determined centroid based on classe.memb. classe.memb=function(U) { info.U=cbind(max.col(U),apply(U,1,max)) i=1 while (i = nrow(U)) { if (apply(U,1,max)[i]0.5) info.U[i,1]=0 i=i+1 } K=ncol(U) table.U=matrix(0,nrow=K,ncol=4) cl=1 while (cl = K) { table.U[cl,1] = length(which(info.U[info.U[,1]==cl,2]=.90)) table.U[cl,2] = length(which(info.U[info.U[,1]==cl,2]=.70)) - table.U[cl,1] table.U[cl,3] = length(which(info.U[info.U[,1]==cl,2]=.50)) - table.U[cl,1] - table.U[cl,2] table.U[cl,4] = sum(table.U[cl,]) cl = cl+1 } rownames(table.U) = c(1:K) colnames(table.U) = c(Alto, Medio, Basso, Totale) out=list() out$info.U=round(info.U,2) out$table.U=table.U return(out) } -- View this message in context: http://r.789695.n4.nabble.com/silhouette-fuzzy-tp3249893p3249893.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.
Re: [R] Finding the correlation coefficient of two stocks
Dieter is correct, the lengths of the 2 series are different Try s = merge(s1,s2) corr = cor(s[,Close.s1],s[,Close.s2],use=pairwise.complete.obs) print(corr) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Finding-the-correlation-coefficient-of-two-stocks-tp3246992p3247261.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.
Re: [R] Help plz
Typing ? (no quotes) followed by a topic of interest will throw up the R Help documentation. 1. Have a look at ?runif 2. Try ?subset and ?cumsum 3. Look at ?rle. Use in conjunction with cumsum and maybe ifelse. HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Help-plz-tp3247449p3247576.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.
Re: [R] How to do a moving window on standard deviation
One way is to convert your data into a zoo object and use the rollapply function # Your data lines = Date Close 2011-01-28 56.42 2011-01-27 57.37 2011-01-26 56.48 2011-01-25 56.39 2011-01-24 55.74 2011-01-21 55.46 d = read.table(textConnection(lines), header = TRUE) # create zoo object d.z=zoo(d[,-1],order.by=as.Date(d$Date)) # generate rolling std devs sd.z = rollapply(d.z,5,sd,align=right) # you wanted a data frame df = as.data.frame(merge(d.z,sd.z,all=TRUE)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247592.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.
Re: [R] How to do a moving window on standard deviation
how about ... j=NULL for(i in 4: length(xyz$Close)) { j[i] = sd(xyz$Close[i-3:i]) } print(j) -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247634.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.
Re: [R] How to do a moving window on standard deviation
I believe there are two reasons why your code doesn't work 1. You should replace sd(Close[i]:Close[(i-3)]) with sd(Close[(i-3):i]). This will ensure you select the appropriate obsevations to feed in the sd function. 2. Per Ray's point above, you need to output the calculated value of sd for each value of the loop The code I supplied previously should work for you. -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247754.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.
Re: [R] sapply puzzlement
In addition to what has already been suggested you could use .. mapply(function(x,y) x-y, z,means) which returns V1 V2 [1,] 0.333 -2.7142857 [2,] NA 7.2857143 [3,] -0.667 -3.7142857 [4,] -6.667 NA [5,] NA -0.7142857 [6,] 1.333 1.2857143 [7,] 3.333 -1.7142857 [8,] 2.333 0.2857143 The results you see when you use the z-means approach are caused by the vectors being different lengths. The shorter one (means) is repeated. Phil Spector's book describes a nice example which illustrates the behaviour nicely. nums = 1:10 nums +c(1,2) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/sapply-puzzlement-tp3243520p3243583.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.
Re: [R] How do I fix this ?
Eric Your problem lies in the way cumprod deals with NAs If you look at ?cumprod you will see An NA value in x causes the corresponding and following elements of the return value to be NA Not sure what behaviour you want to see on encountering an NA (ignore it, restart the cumprod process, .). Making things easy for myself (it's late), if you wish to simply ignore an NA the following would work # sample data y2=c(NA,1,2,3,4,5) # ignore NA ave(y2,is.na(y2),FUN=cumprod) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/How-do-I-fix-this-tp3239239p3241432.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.