Re: [R] Help with loop for column means into new column by a subset Factor w/131 levels

2019-04-30 Thread Bill Poling
I ran this routine but I was thinking there must be a more elegant way of doing this. #https://community.rstudio.com/t/how-to-average-mean-variables-in-r-based-on-the-level-of-another-variable-and-save-this-as-a-new-variable/8764/8 hcd2tmp2_summmary <- hcd2tmp2 %>% select(.) %>% group_by(Pr

Re: [R] Help with loop ;(

2013-10-22 Thread arun
Hi, The conditions are not very clear. For example, it is not mentioned whether vectors are of same length or not.  Assuming the former case: fun1 <- function(x,y){  if(length(x)==length(y) & length(x)%%2==0){  res <- x+y  }  else if(length(x)==length(y) & length(x)%%2==1){  res <- abs(x-y) } el

Re: [R] Help with loop

2012-11-22 Thread arun
- From: Hefri To: r-help@r-project.org Cc: Sent: Thursday, November 22, 2012 6:43 AM Subject: Re: [R] Help with loop Hi again, I was thinking that the interval would stop when there was a NA, ignore subsequent NAs and only start a new interval when there was a new value. So in the example below, the int

Re: [R] Help with loop

2012-11-22 Thread Hefri
Hi again, I was thinking that the interval would stop when there was a NA, ignore subsequent NAs and only start a new interval when there was a new value. So in the example below, the interval would stop at row 320, and a new interval started at row 334. > head (D, 20) Salt time 319

Re: [R] Help with loop

2012-11-22 Thread Rui Barradas
Hello, I'm glad it helped. You should have kept this in the list, the odds of getting more answers are bigger. As for your problem with NAs, what do you want to do with them? Should they count as within range? Rui Barradas Em 22-11-2012 10:18, Helene Frigstad escreveu: Hi, yes, that is a v

Re: [R] Help with loop

2012-11-21 Thread Rui Barradas
Hello, If I understand it well, this might avoid a loop. dat <- read.table(text=" Salt time 1 35.65114 2003-07-19 2 35.64226 2003-07-20 3 35.62411 2003-07-21 4 35.62473 2003-07-22 5 35.65893 2003-07-23 6 35.70140 2003-07-24 7 35.62157 2003-07-25 8 35.64122 2003-07-26 9 35.63515 2

Re: [R] help with Loop

2012-07-23 Thread Rui Barradas
Hello, Try the following. d <- read.table(text=" D Y C a 2005 10 a 2006 0 a 2007 9 b 2005 1 b 2006 0 b 2007 1 c 2005 5 c 2006 NA c 2007 4 ", header=TRUE) d prn <- lapply(split(d, d$D), function(x){ x <- x[!is.na(x$C), ] x[c(FALSE, diff(x$C)/x$C[-length(x$C)] < -0.5 & diff(x$C) < -5), ]

Re: [R] Help with loop

2012-07-12 Thread arun
Hi, Try this: func1<-function(x,y,z)  {ifelse(is.na(y[[x]]),z[[x]],y[[x]])} dat3<-data.frame(lapply(colnames(df1),function(x) func1(x,df1,df2))) colnames(dat3)<-colnames(df1) dat3   cola colb  colc cold cole 1  1.4  5.0  9.00  1.6 17.0 2  1.4  6.0  0.02 14.0  0.6 3  3.0  0.8 11.00 15.0 19.0 4 

Re: [R] Help with loop

2012-07-11 Thread Charles Stangor
I think I just learned this myself: Don't put the $ extension in the bracket : df1$cola[is.na(df1$cola)]<- > > df2$cola > Instead substitute using brackets within the brackets: df1["cola"]is.na(df1["cola"])]<- > > df2["cola"] > then the "cola" s can be substituted. > Maybe this will help

Re: [R] Help with loop

2012-07-11 Thread Rui Barradas
Hello, A one-liner could be df1 <- read.table(text=" cola colb colc cold cole 1NA59 NA 17 2NA6 NA 14 NA 3 3NA 11 15 19 4 48 12 NA 20 ", header=TRUE) df2 <- read.table(text=" cola colb colc cold cole 1 1.4 0.8 0.02 1.6 0.6 ", header=

Re: [R] Help with Loop Please!

2009-09-13 Thread Henrique Dallazuanna
Try this: apply(subset.models[,-1], 1, function(x)lm(as.formula(paste('y ~', paste(names(freeny[,-1])[x], collapse = "+"), sep = "")), data = freeny)) On Sun, Sep 13, 2009 at 10:57 AM, Axel Urbiz wrote: > Hi, > > I’d like to fit one GLM model for each possible combination of inputs (i.

Re: [R] Help with Loop!

2009-07-23 Thread Steve Lianoglou
Hi, On Jul 23, 2009, at 7:30 PM, Lars Bishop wrote: Dear experts, I'm new in R and trying to learn by writing a version of the Perceptron Algorithm. How can I tell in the code below to stop the iteration when the condition in the "for loop" is not satisfied for all training examples? T

Re: [R] help with loop

2009-03-12 Thread Wacek Kusnierczyk
Wacek Kusnierczyk wrote: > is your data a data frame or a matrix? do you want to compute the > differences columnwise, i.e., for each column independently? consider > this example: > > # generate and display dummy data > (d = as.data.frame(replicate(3, sample(5 > > # compute succe

Re: [R] help with loop

2009-03-12 Thread Rafael Moral
Thank you guys, it's a lot simpler than I thought. Regards, Rafael. Veja quais são os assuntos do momento no Yahoo! +Buscados [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinf

Re: [R] help with loop

2009-03-12 Thread baptiste auguie
On 12 Mar 2009, at 13:22, richard.cot...@hsl.gov.uk wrote: I'm trying to write a loop to sum my data in the following way: (the second - the first) + (the third - the second) + (the fourth - the third) + ... for each column. This is just sum(diff(x)), or even x[length(x)] - x[1]. I think r

Re: [R] help with loop

2009-03-12 Thread Jorge Ivan Velez
Dear Rafael, Perhaps: sum(diff(x)) where x is your vector. To apply above to your data set (by rows), you could use apply(mydata,1,function(x) sum(diff(x))) See ?diff, ?sum and ?apply for more information. HTH, Jorge On Thu, Mar 12, 2009 at 9:04 AM, Rafael Moral wrote: > Dear useRs, > I'm

Re: [R] help with loop

2009-03-12 Thread Gabor Grothendieck
This is a telescoping sum that can be calculated analytically as: (a[2] - a[1]) + ... + (a[n] - a[n-1]) = a[n] - a[1] On Thu, Mar 12, 2009 at 9:04 AM, Rafael Moral wrote: > Dear useRs, > I'm trying to write a loop to sum my data in the following way: > (the second - the first) + (the third - t

Re: [R] help with loop

2009-03-12 Thread Romain Francois
Well actually, what about that (Assuming mydata is a data frame) tail( mydata, 1 ) - head( mydata, 1) since: (the second - the first) + (the third - the second) + (the fourth - the third) = the last - the first Romain Rafael Moral wrote: Dear useRs, I'm trying to write a loop to sum my data

Re: [R] help with loop

2009-03-12 Thread Richard . Cotton
> I'm trying to write a loop to sum my data in the following way: > (the second - the first) + (the third - the second) + (the fourth - > the third) + ... > for each column. This is just sum(diff(x)), or even x[length(x)] - x[1]. Regards, Richie. Mathematical Sciences Unit HSL ---

Re: [R] help with loop

2009-03-12 Thread Romain Francois
Hi, Try this; lapply( mydata, function(x){ sum( diff( x ) ) } ) Romain Rafael Moral wrote: Dear useRs, I'm trying to write a loop to sum my data in the following way: (the second - the first) + (the third - the second) + (the fourth - the third) + ... for each column. So, I wrote someth

Re: [R] help with loop

2009-03-12 Thread Wacek Kusnierczyk
is your data a data frame or a matrix? do you want to compute the differences columnwise, i.e., for each column independently? consider this example: # generate and display dummy data (d = as.data.frame(replicate(3, sample(5 # compute successive differences columnwise as.dat

Re: [R] help with loop

2009-03-12 Thread Nutter, Benjamin
Why use a loop? Try using diff() x <- c(4, 19, 21, 45, 50, 73, 78, 83, 87, 94) sum(diff(x)) -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Rafael Moral Sent: Thursday, March 12, 2009 9:04 AM To: r-help@r-project.org Subject: [R] h