Re: [R] loops in R

2014-11-05 Thread Berend Hasselman
On 05-11-2014, at 14:41, Noha Osman wrote: > Hi Folks > > Iam a new user of R and I have a question . Hopefully anyone help me in that > issue > > > I have that dataset as following > > Sample Population Species Tissue R GB > 1 Bari1_062-1 Bari1 ret

Re: [R] loops in R

2014-11-05 Thread Steve Lianoglou
While you should definitely read the tutorial that Don is referring to, I'd recommend you take a different approach and use more R idiomatic code here. In base R, this could be addressed with few approaches. Look for help on the following functions: * tapply * by * aggregate I'd rather rec

Re: [R] loops in R

2014-11-05 Thread Daniel Nordlund
On 11/5/2014 5:41 AM, Noha Osman wrote: Hi Folks Iam a new user of R and I have a question . Hopefully anyone help me in that issue I have that dataset as following Sample Population Species Tissue R GB 1 Bari1_062-1 Bari1 ret seed 94.52303 80.7

Re: [R] loops in R

2014-11-05 Thread Don McKenzie
Have you read the tutorial that comes with the R distribution? This is a very basic database calculation that you will encounter (or some slight variation of it) over and over. The solution is a few lines of code, and someone may write it out for you, but if no one does You have 20 population

Re: [R] loops with assign() and get()

2014-08-11 Thread William Dunlap
That code will not work. get() and assign() are troublesome for a variety of reasons. E.g., * adding made-up names to the current environment is dangerous. They may clobber fixed names in the environment. You may be confused about what the current environment is (especially when refactoring co

Re: [R] loops with assign() and get()

2014-08-11 Thread PO SU
It's a great method, but there is a memory problem, DFS would occupy a large memory. So from this point of view, i prefer the loop. >> for (i in 1 : nrow(unique)){ >> tmp=get(past0("DF",i))[1,] >> assign(paste0("df",i),tmp) >> dfi=dfi[,1:3] >> names(dfi)=names(tmp[c(1,4,5)]) >> dfi=rbind(dfi,tm

Re: [R] loops with assign() and get()

2014-08-09 Thread William Dunlap
> I was able to create 102 distinct dataframes (DFs1, DFs2, DFs3, etc) using > the assign() in a loop. The first step to making things easier to do is to put those data.frames into a list. I'll call it DFS and your data.frames will now be DFs[[1]], DFs[[2]], ..., DFs[[length(DFs)]]. DFs <- la

Re: [R] Loops to produce plots and leyend

2014-06-20 Thread David Winsemius
On Jun 20, 2014, at 9:18 AM, Bea GD wrote: > I've tried several things but I don't manage to get this plot right. Any help > greatly appreciated! > I'm running a for loop to produce 4 plots at once. Each plot should only show > data points for a specific group (i.e. 4 plots for groups 1 to 4).

Re: [R] Loops (run the same function per different columns)

2014-04-25 Thread CRoa
Hi Arun, Thanks a lot for your script. Ill work on it tomorrow. Cheers On 24/04/2014, at 11:54 AM, "arun kirshna [via R]" wrote: > HI, > I guess you got an output like this using my script: > ##Please use ?dput() to show the example data. > > FA <- structure(list(Sample = c("L1 Control",

Re: [R] Loops (run the same function per different columns)

2014-04-23 Thread CRoa
Hi Arun, Your script works but it does not do what I was after. To be a bit more specific, this the table FA in which Im working on ( but the original one has 34 fatty acids instead of 3: C14.0, C15.0, and C15.1). SampleC14:0C15:0C15:1 L1 Control0.4565091920.4695626870.774909216 L1 Control0.51398

Re: [R] Loops (run the same function per different columns)

2014-04-23 Thread arun
HI, I guess you got an output like this using my script: ##Please use ?dput() to show the example data. FA <- structure(list(Sample = c("L1 Control", "L1 Control", "L1 Control", "BBM Control", "BBM Control", "BBM Control", "L1 Ash", "L1 Ash", "L1 Ash", "BBM Ash", "BBM Ash", "BBM Ash"), C14.0 = c

Re: [R] Loops (run the same function per different columns)

2014-04-21 Thread CRoa
Thank you Arun for your quick answer. Ill try it and ill let you know. Cheers On 21/04/2014, at 11:30 PM, "arun kirshna [via R]" wrote: > Hi, > > Using the example data from library(gvlma) > > library(gvlma) > data(CarMileageData) > CarMileageNew <- CarMileageData[,c(5,6,3)] > lst1 <-

Re: [R] Loops (run the same function per different columns)

2014-04-21 Thread arun
Hi, Using the example data from library(gvlma) library(gvlma) data(CarMileageData) CarMileageNew <- CarMileageData[,c(5,6,3)]  lst1 <- list()  y <- c("NumGallons", "NumDaysBetw")  for(i in seq_along(y)){  lst1[[i]] <- gvlma(lm(get(y[i])~MilesLastFill,data=CarMileageNew))  lst1} pdf("gvlmaplot.pdf

Re: [R] Loops

2013-06-26 Thread PIKAL Petr
Hi > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of bett kimutai > Sent: Wednesday, June 26, 2013 3:17 AM > To: Law, Jason; r-help@r-project.org > Subject: Re: [R] Loops > > Thanks for you response.

Re: [R] Loops

2013-06-25 Thread bett kimutai
rectify my loop to run for all the pipes and then create a table to hold these values. Sorry for the syntax errors, I guess I will get them with time.  thanks again.. From: "Law, Jason" ect.org> Sent: Tuesday, June 25, 2013 3:32 PM Subject: RE: [R

Re: [R] Loops

2013-06-25 Thread Law, Jason
Not sure what you're trying to do, but it looks like most of what you're attempting to do in the code can be done just using vectors rather than loops, at least the inner loop. For example: k <- 1.15 l <- exp((1 / k) * (7.16 - 0.44 + 0.12 - 0.016)) z <- (log(1 / p) * l)^k See ifelse for how to

Re: [R] Loops

2013-06-25 Thread bett kimutai
Dear All, I  have spent most of my time trying to figure out how to simulate the number of breaks in a pipe using monte carlo simulation. i have 20,000 individual pipes that i have to run, and for each pipe i have to run 1000 times while checking some conditions and therefore, i have to use a

Re: [R] loops for matrices

2013-06-12 Thread Rainer Schuermann
The comments on StackOverflow are fair, I believe... Please dput() your matrices, so that your code becomes reproducible! On Wednesday 12 June 2013 11:14:35 maggy yan wrote: > I have to use a loop (while or for) to return the result of hadamard > product. now it returns a matrix, but when I use i

Re: [R] Loops

2013-01-28 Thread Francesca
gt; 25)+1),as.matrix),c(2,3),function(x) x)) > res<-rbind(t(pnew),colSums(p)) > row.names(res)<-1:nrow(res) > res<- 100-100*abs(res/rowSums(res)-(1/3)) > A.K. > > > - Original Message - > From: Rui Barradas > To: Francesca > Cc: r-help@r-project.org

Re: [R] Loops

2013-01-27 Thread arun
)-(1/3)) A.K. - Original Message - From: Rui Barradas To: Francesca Cc: r-help@r-project.org Sent: Sunday, January 27, 2013 6:17 AM Subject: Re: [R] Loops Hello, I think there is an error in the expression 100-(100*abs(fa1[i]/sum(fa1[i])-(1/3))) Note that fa1[i]/sum(fa1[i]) is always 1. If it&

Re: [R] Loops

2013-01-27 Thread Uwe Ligges
On 27.01.2013 12:50, Richard D. Morey wrote: Dear Contributors, I am asking help on the way how to solve a problem related to loops for that I always get confused with. I would like to perform the following procedure in a compact way. Consider that p is a matrix composed of 100 rows and three

Re: [R] Loops

2013-01-27 Thread Richard D. Morey
> Dear Contributors, > I am asking help on the way how to solve a problem related to loops for > that I always get confused with. > I would like to perform the following procedure in a compact way. > > Consider that p is a matrix composed of 100 rows and three columns. I need > to calculate the su

Re: [R] Loops

2013-01-27 Thread Rui Barradas
Hello, I think there is an error in the expression 100-(100*abs(fa1[i]/sum(fa1[i])-(1/3))) Note that fa1[i]/sum(fa1[i]) is always 1. If it's fa1[i]/sum(fa1), try the following, using lists to hold the results. # Make up some data set.seed(6628) p <- matrix(runif(300), nrow = 100) idx <- se

Re: [R] loops

2012-01-24 Thread R. Michael Weylandt
You can use the assign() function. E.g., assign("a", 3,) But the more R thing to do is to create a list of your results (which you can name) and to handle that whole object. Then if you want to do things to each object you create, it's as simple as lapply Michael On Tue, Jan 24, 2012 at 7:27 PM,

Re: [R] loops over regression models

2012-01-13 Thread Petr PIKAL
Hi > > Dear R help listers, > > I am trying to replicate results in Gelman and Hill's book (Chapter 3 > in regressions and multilevel models). Below I estimated two models > (chp3.1 and chp3.3 in R codes) with the same data and dependent > variable but different independent variables. I have been

Re: [R] Loops and vector operations

2011-12-30 Thread Mago84
Thanks a lot, it was very helpful, I did something like that: EV <- ifelse(sprd > mediaSDP & sprd_d <= mediaSDP_d, -1, 0) SV <- ifelse(sprd > media & sprd_d <= media_d, -17, 0) EC <- ifelse(sprd < mediaSDN & sprd_d >= mediaSDN_d,1,0) SC <- ifelse(sprd < media & sprd_d >= media_d,17,0) in order t

Re: [R] Loops and vector operations

2011-12-28 Thread R. Michael Weylandt
Look at ?ifelse. You'll need to nest them however. If you are feeling a little more ambitious this can be done with ?switch, but for clarity, I'd rather see pre-allocation followed by assignments Something along this pattern: out <- numeric(50) x <- sample(3, 50, TRUE) out[x==3] <- "A" out[x==2

Re: [R] Loops

2011-09-15 Thread Fred
Thank you so much! foreL<-8 b0f<-matrix(nrow=9, ncol=foreL) ct<-1 ### use this as the index of b0f for(ar.ord in 1:3){ for(ma.ord in 1:3){ b0f[ct,]<-c(predict(arima(para_qtr[1:(n-8),1],order=c(ar.ord,1,ma.ord)), n.ahead=foreL)$pred) ct<-ct+1 ### increment the counter } } this one works! Best rega

Re: [R] Loops

2011-09-14 Thread R. Michael Weylandt
Look at the combinations function in gtools and index by the rows of that output for a single loop. Pass values as the three parameters. Sorry for being terse - writing on my phone. Michael On Sep 14, 2011, at 5:36 PM, Fred wrote: > Dear forum, > > I would like to forecast e.g. with the ar

Re: [R] Loops

2011-09-14 Thread Weidong Gu
A little bit change of your code may do the work foreL<-8 b0f<-matrix(nrow=9, ncol=foreL) ct<-1 ### use this as the index of b0f for(ar.ord in 1:3){ for(ma.ord in 1:3){ b0f[ct,]<-c(predict(arima(para_qtr[1:(n-8),1],order=c(ar.ord,1,ma.ord)), n.ahead=foreL)$pred) ct<-ct+1 ### increment the counter

Re: [R] Loops on data˜1

2011-09-14 Thread R. Michael Weylandt
The first thing I'd mention is that *apply are really just well implemented loops themselves so you can't expect true magic from them. However, if you really want to use them rather than just a regular loop (which seems more intuitive for a request like this), this might do what you are looking for

Re: [R] Loops on data˜1

2011-09-13 Thread Trying To learn again
Hi Michael, First of all thanks for your response. I do know that if I make my stimation on a single data the regression has no sense but it will be getting sense in the growing next estimations. I change my asking doubt. I want to use this regressions as a first filter. Only this. Can anyone

Re: [R] Loops on data˜1

2011-09-12 Thread R. Michael Weylandt
I may be totally off base with this, but I'm wondering what exactly this would suggest or why you want to do it. Specifically "multiple regression with only intercept" -- how is it multiple if you don't have any regressors? Furthermore, you want to run a "regression" on a single data point -- reall

Re: [R] Loops for repetitive task

2011-08-10 Thread Dennis Murphy
Hi: Try this: ## Function that takes a data frame as input and outputs a data frame: chrSumm <- function(d) { # d is a data frame colnames(d) <- c("chr","start","end","base1","base2", "totalreads","methylation","strand") TR <- nrow(d) RG1 <- sum(d['totalreads']

Re: [R] Loops for repetitive task

2011-08-09 Thread Peter Alspach
Tena koe Try something along the following lines: chrData <- vector('list', 22) names(chrData) <- paste('chr', 1:22, sep='') for (i in 1:length(chrData)) { chrData[[i]] <- read.table(file=paste('chr', i, '.out.txt', sep=''), header=F) ... } HTH Peter Alspach > -Original Message--

Re: [R] Loops to assign a unique ID to a column

2011-08-02 Thread Bert Gunter
Whoa! 1. First and most important, there is very likely no reason you need to do this. R can handle multiple groupings automatically in fitting and plotting without creating artificial labels of the sort you appear to want to create. Please read an "Intro to R" and/or get help to see how. 2. The

Re: [R] Loops to assign a unique ID to a column

2011-08-02 Thread David L Carlson
How about this? > indx <- unique(cbind(Dates, Groups)) > indx DatesGroups [1,] "12/10/2010" "A" [2,] "12/10/2010" "B" [3,] "13/10/2010" "A" [4,] "13/10/2010" "B" [5,] "13/10/2010" "C" > indx <- data.frame(indx, id=1:nrow(indx)) > indx Dates Groups id 1 12/10/2010

Re: [R] Loops to assign a unique ID to a column

2011-08-02 Thread ONKELINX, Thierry
Dear Chandra, You're on the wrong track. You don't need for loops as you can do this vectorised. as.numeric(interaction(data$Groups, data$Dates, drop = TRUE)) Best regards, Thierry > -Oorspronkelijk bericht- > Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > N

Re: [R] loops and simulation

2011-07-22 Thread Dylan Beaudette
On Wed, Jul 20, 2011 at 10:43 PM, David Winsemius wrote: > > On Jul 21, 2011, at 1:04 AM, Daniel Malter wrote: > >> http://mlg.eng.cam.ac.uk/dave/rmbenchmark.php >> >> I haven't ever tried it myself, but online sources suggest that Matlab >> possibly gains speed by internally avoiding loops rather

Re: [R] loops and simulation

2011-07-20 Thread David Winsemius
rent. -- David. Daniel From: David Winsemius [dwinsem...@comcast.net] Sent: Wednesday, July 20, 2011 9:01 AM To: Daniel Malter Cc: r-help@r-project.org Subject: Re: [R] loops and simulation On Jul 20, 2011, at 1:34 AM, Daniel Malter wrote: snipped requests,

Re: [R] loops and simulation

2011-07-20 Thread Daniel Malter
aniel From: David Winsemius [dwinsem...@comcast.net] Sent: Wednesday, July 20, 2011 9:01 AM To: Daniel Malter Cc: r-help@r-project.org Subject: Re: [R] loops and simulation On Jul 20, 2011, at 1:34 AM, Daniel Malter wrote: snipped > requests, except that

Re: [R] loops and simulation

2011-07-20 Thread David Winsemius
On Jul 20, 2011, at 1:34 AM, Daniel Malter wrote: snipped requests, except that you were referring to SAS and had heard that R "does not like loops." (This is factually wrong. But R can be slow looping). Where did you hear this? Can you cites any references? -- David Winsemius, MD West Ha

Re: [R] loops and simulation

2011-07-19 Thread Daniel Malter
Yes, there are in Europe. And there are summer classes in the US, as well. And no, this list is not so much about helping beginners to learn R. For that, there is a myriad of online sources. Rather, this list is for people who have exhausted their ability to (elegantly) solve a problem. Also, it s

Re: [R] loops and simulation

2011-07-19 Thread Daniel Malter
I dare the conjecture that if you had written the code, you would know how to do this. This suggests that you are asking us to do your homework, which is not the purpose of this list. A simple inclusion of the code in a for or while loop and storing the estimated parameters with the index of the it

Re: [R] loops and if statements

2010-04-18 Thread Erich Neuwirth
I would prefer version 1. Version to creates a global variable R which you do not really need since it contains the same values as d$r. In option 2, you should probably remove the variable r itself after it has been appended to d. On 4/18/2010 5:23 PM, Laura Ferrero-Miliani wrote: > Thanks for co

Re: [R] loops and if statements

2010-04-18 Thread Laura Ferrero-Miliani
Thank you so much all! How a simple problem can get complicated by not having enough knowledge, but hopefully I am learning Have a great Sunday! L On Sun, Apr 18, 2010 at 4:56 PM, jim holtman wrote: > You are working with a matrix, so the "$" operator is not allowed (e.g., > d$c). > > Also in

Re: [R] loops and if statements

2010-04-18 Thread Joshua Wiley
Hey Laura, Just to add a cautionary note, in > a <- c(1:4) > b <- c("meep", "foo", "meep", "foo") > d <- cbind(a, b) d is a matrix and will only be one type of matrix.  Since you have both integer (a) and character (b) data, it has to be at the character level.  From the help for cbind: "The t

Re: [R] loops and if statements

2010-04-18 Thread jim holtman
You are working with a matrix, so the "$" operator is not allowed (e.g., d$c). Also in your test, you have to test against the second column (e.g., d[i, 2]) try this: > a <- c(1:4) > b <- c("meep", "foo", "meep", "foo") > d <- cbind(a, b) > > > for(i in seq(along=d[,2])) {if (d[i,2]=="meep") { p

Re: [R] loops & sampling

2007-11-01 Thread Julian Burgos
Hi Garth, Your code is really confusing! You should start by reading the help file on the for() function and understanding what it does: ?"for" Your line for(i in 1:nboot){ } is simply starting a loop around the variable 'i', which will change values following the sequence 1:nboot. It seems