Re: [R] Simple loop problem

2016-10-31 Thread William Dunlap via R-help
Did you mean 'tolower' instead of 'to lower' in your example? Or is there a similarly named function that converts the levels of a factor to lower case instead of converting the factor to a character vector and then lower-casing that? > d %>% dplyr::mutate_if(is.factor, tolower) NumF1 F2 1

Re: [R] Simple loop problem

2016-10-31 Thread William Dunlap via R-help
Use tolower on the levels of the factor columns. E.g., > d <- data.frame(Num=1:3, F1=c("One","Two","Three"), F2=c("A","B","a")) > str(d) 'data.frame': 3 obs. of 3 variables: $ Num: int 1 2 3 $ F1 : Factor w/ 3 levels "One","Three",..: 1 3 2 $ F2 : Factor w/ 3 levels "a","A","B": 2 3 1 > fo

Re: [R] Simple loop problem

2016-10-31 Thread David L Carlson
To: r-help@r-project.org Subject: [R] Simple loop problem Hi everybody, I’m new to R and i’m trying to learn fundamentals. I’m facing a small problem for which i can’t find a solution online. What i want to do: write a function to lower case for all the columns in my data.frame if they re

[R] Simple loop problem

2016-10-31 Thread Yahya Laraki
Hi everybody, I’m new to R and i’m trying to learn fundamentals. I’m facing a small problem for which i can’t find a solution online. What i want to do: write a function to lower case for all the columns in my data.frame if they respect a condition (class = factor) This code works, but for all

Re: [R] Simple Loop Counter

2014-04-26 Thread arun
HI, May be this helps: for(i in letters) {   n <- n+1   x[n,] <- c(i, n) cat("loop", n, "\n") } x #or for(i in seq_along(letters)) {   n <- n+1   x[n,] <- c(letters[i], n) cat("loop", i, "\n") } x A.K. #How can I add the loop counter in #2 to the loop in #1.? #*** #1. x <- matrix( ,

Re: [R] Simple loop question

2014-03-28 Thread Noah Marconi
Here're a couple alternatives if you want to use the index instead of the variable name: # Reproducible data frame a1 <- 1:15 a2 <- letters[1:15] a3 <- LETTERS[1:15] a4 <- 15:1 a5 <- letters[15:1] df <- data.frame(a1, a2, a3, a4, a5) df # a1 a2 a3 a4 a5 # 1 1 a A 15 o # 2 2 b B 14

Re: [R] Simple loop question

2014-03-27 Thread Jim Lemon
On 03/28/2014 05:27 PM, Luana Marotta wrote: Hi all, I'm trying to find out what is the equivalent in R for the following Stata code: Let's say I have the variables: a1, a2, a3, a4, a5 forvalues i = 1(1)5 { summary a`i' } That is, I want to know how to loop through variables in R. Hi L

[R] Simple loop question

2014-03-27 Thread Luana Marotta
Hi all, I'm trying to find out what is the equivalent in R for the following Stata code: Let's say I have the variables: a1, a2, a3, a4, a5 forvalues i = 1(1)5 { summary a`i' } That is, I want to know how to loop through variables in R. Thank you in advance, Luana [[alternative H

Re: [R] Simple loop

2011-05-07 Thread Jeff Newmiller
"Cannot revert it"? Doesn't setting up a new default parameter in the calling function ("ave") accomplish that? --- Jeff Newmiller The . . Go Live... DCN: Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing

Re: [R] Simple loop

2011-05-07 Thread peter dalgaard
On May 7, 2011, at 17:21 , Hadley Wickham wrote: >> >> Well, ave() uses interaction(...) and interaction() has a "drop" argument, so >> >>> with(x, ave(H, Site, Prof, drop=TRUE, FUN=function(y)y-min(y))) >> [1] 8 0 51 0 33 22 21 0 > > I don't understand why this isn't the default. > > Had

Re: [R] Simple loop

2011-05-07 Thread Hadley Wickham
>> Using paste(Site,Prof) when calling ave() is ugly, in that it >> forces you to consider implementation details that you expect >> ave() to take care of (how does paste convert various types >> to strings?).  It also courts errors  since paste("A B", "C") >> and paste("A", "B C") give the same re

Re: [R] Simple loop

2011-05-07 Thread peter dalgaard
On May 4, 2011, at 17:52 , William Dunlap wrote: >> -Original Message- >> From: r-help-boun...@r-project.org >> [mailto:r-help-boun...@r-project.org] On Behalf Of Petr Savicky >> Sent: Wednesday, May 04, 2011 12:51 AM >> To: r-help@r-project.org >> Su

Re: [R] Simple loop

2011-05-04 Thread Petr Savicky
roject.org > > Subject: Re: [R] Simple loop > > > > On Tue, May 03, 2011 at 12:04:47PM -0700, William Dunlap wrote: > > [...] > > > ave() can deal that problem: > > > > cbind(x, newCol2 = with(x, ave(H, Site, Prof, > > > FUN=function(y)y-min(y))

Re: [R] Simple loop

2011-05-04 Thread William Dunlap
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Petr Savicky > Sent: Wednesday, May 04, 2011 12:51 AM > To: r-help@r-project.org > Subject: Re: [R] Simple loop > > On Tue, May 03, 2011 at 12:04:47

Re: [R] Simple loop

2011-05-04 Thread Petr Savicky
On Tue, May 03, 2011 at 12:04:47PM -0700, William Dunlap wrote: [...] > ave() can deal that problem: > > cbind(x, newCol2 = with(x, ave(H, Site, Prof, > FUN=function(y)y-min(y > Site Prof H newCol2 > 111 24 8 > 211 16 0 > 311 67 51 > 4

Re: [R] Simple loop

2011-05-03 Thread Dennis Murphy
Hi: Here are two more candidates, using packages plyr and data.table. Your toy data frame is called dd below. library(plyr) ddply(dd, .(Site, Prof), transform, Hadj = H - min(H)) Site Prof H Hadj 111 248 211 160 311 67 51 412 230 512 56 3

Re: [R] Simple loop

2011-05-03 Thread andrija djurovic
lto:r-help-boun...@r-project.org] On Behalf Of andrija djurovic > > Sent: Tuesday, May 03, 2011 11:28 AM > > To: Woida71 > > Cc: r-help@r-project.org > > Subject: Re: [R] Simple loop > > > > Hi. > > There is no need to do this in a for loop. > > Here

Re: [R] Simple loop

2011-05-03 Thread William Dunlap
> Subject: Re: [R] Simple loop > > Hi. > There is no need to do this in a for loop. > Here is one approach: > > x <- read.table(textConnection("Site Prof H > 1 1 24 > 1 1 16 > 1 1 67 > 1 2 23 > 1 2 56 > 1

Re: [R] Simple loop

2011-05-03 Thread andrija djurovic
Hi. There is no need to do this in a for loop. Here is one approach: x <- read.table(textConnection("Site Prof H 1 1 24 1 1 16 1 1 67 1 2 23 1 2 56 1 2 45 2 1 67 2 1 46"), header = TRUE) closeAllConnections() x cbind(x,newCo

Re: [R] Simple loop

2011-05-03 Thread Jonathan Daily
It is actually possible and preferable to do this with no loops. Assuming your data is in a dataframe called dat: idx <- with(dat, Site == 1 & Prof == 1) dat <- within(dat, { new = H - ifelse(Site == 1 & Prof == 1, min(H[idx]), min(H[!idx])) }) dat which also serves to illuminate the difference b

[R] Simple loop

2011-05-03 Thread Woida71
Hello everybody, I am beginning with loops and functions and would be glad to have help in the following question: If i have a dataframe like this Site Prof H 1 1 24 1 1 16 1 1 67 1 2 23 1 2 56 1 2 45 2 1 67 2 1 46 An

Re: [R] simple loop questions

2011-04-24 Thread David Winsemius
On Apr 23, 2011, at 9:34 AM, David Winsemius wrote: On Apr 23, 2011, at 6:31 AM, wrote: Hi all, I am trying to write a loop to return the matched index for a column of values. Here is an simple example of this, A B 0 5 1 2 2 2 3 4 4 1 5 4 6 2 In this case, A is an inde

Re: [R] simple loop questions

2011-04-23 Thread David Winsemius
On Apr 23, 2011, at 6:31 AM, wrote: Hi all, I am trying to write a loop to return the matched index for a column of values. Here is an simple example of this, A B 0 5 1 2 2 2 3 4 4 1 5 4 6 2 In this case, A is an index column for B. Now I have this new column C with jus

[R] simple loop questions

2011-04-23 Thread xueke
Hi all, I am trying to write a loop to return the matched index for a column of values. Here is an simple example of this, A B 0 5 1 2 2 2 3 4 4 1 5 4 6 2 In this case, A is an index column for B. Now I have this new column C with just two values of 2 and 4. I want to match

Re: [R] simple loop problemo (Geo brownian motion)

2010-11-19 Thread Yihui Xie
Maybe you can consider the brownian.motion() function in the animation package, e.g. library(animation) ani.options(interval = 0.05, nmax = 150) brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow", main = "Demonstration of Brownian Motion") Or here is another (awkward) example of severa

Re: [R] simple loop problemo (Geo brownian motion)

2010-11-19 Thread Duncan Murdoch
On 19/11/2010 1:09 PM, newbster wrote: I would like to plot multiple random walks onto the same graph. My p variable dictates how may random walks there will be. par(mfrow=c(1,1)) p<- 100 N<- 1000 S0<- 10 mu<- 0.03 sigma<- 0.2 nu<- mu-sigma^2/2 x<- matrix(rep(0,(N+1)*p),nrow=(N+1)) y<- matrix(

[R] simple loop problemo (Geo brownian motion)

2010-11-19 Thread newbster
I would like to plot multiple random walks onto the same graph. My p variable dictates how may random walks there will be. par(mfrow=c(1,1)) p <- 100 N <- 1000 S0 <- 10 mu <- 0.03 sigma <- 0.2 nu <- mu-sigma^2/2 x <- matrix(rep(0,(N+1)*p),nrow=(N+1)) y <- matrix(rep(0,(N+1)*p),nrow=(N+1)) t<-

Re: [R] simple loop(?) analysing subsets

2010-07-18 Thread Joshua Wiley
Not to hijack the thread, but for my edification, what are the advantages/disadvantages of split() + lapply() compared to by()? Josh On Sun, Jul 18, 2010 at 9:50 PM, Dennis Murphy wrote: > Hi: > > Time to jack up your level of R knowledge, courtesy of the apply family. > > The 'R way' to do what

Re: [R] simple loop(?) analysing subsets

2010-07-18 Thread Dennis Murphy
Hi: Time to jack up your level of R knowledge, courtesy of the apply family. The 'R way' to do what you want is to split the data by species into list components, run lm() on each component and save the resulting lm objects in a list. The next trick is to figure out how to extract what you want,

Re: [R] simple loop(?) analysing subsets

2010-07-18 Thread Joshua Wiley
Hello, Take a look at ?by Something like: by(data, data[ , "species id"], function(x) {lm(o2con ~ bm, data = x)}) As an aside, it can be a bit cumbersome to deal with spaces in a variables name (because it has to be quoted then). Also since data is a function, something like mydata might make

[R] simple loop(?) analysing subsets

2010-07-18 Thread karmakiller
Hi All, I have a large data set with many columns of data. One of these columns is a species identifier and the remainder are variables such as temperature or mass. Currently I am carrying out a single regression on subsets of the data set, e.g. separated data sets with only the data from one spe

Re: [R] Simple loop code

2010-05-03 Thread RCulloch
Thanks David & Henrique, I've been using R for over two years and always used cbind or rbind, that was what I was taught by several folk, and on training courses, you learn something new every day! Cheers, Ross -- View this message in context: http://r.789695.n4.nabble.com/Simple-loop-code-t

Re: [R] Simple loop code

2010-04-29 Thread David Winsemius
On Apr 29, 2010, at 1:42 PM, Henrique Dallazuanna wrote: On Thu, Apr 29, 2010 at 12:05 PM, David Winsemius > wrote: snipped ##Then bind to your data z <- cbind(y,x) Oooh. Not a good practice, at least for the newish useR. cbind and rbind create matrices and as a consequence coerce a

Re: [R] Simple loop code

2010-04-29 Thread Henrique Dallazuanna
On Thu, Apr 29, 2010 at 12:05 PM, David Winsemius wrote: > > On Apr 29, 2010, at 10:37 AM, RCulloch wrote: > > >> Thanks Henrique, >> >> that works! for anyone else as slow as me, just: >> >> ##Assign >> x <- factor(dat.ID$ID2, labels = 1:7) >> ##Convert to dataframe >> x <- as.data.frame(x) >> >

Re: [R] Simple loop code

2010-04-29 Thread David Winsemius
On Apr 29, 2010, at 10:37 AM, RCulloch wrote: Thanks Henrique, that works! for anyone else as slow as me, just: ##Assign x <- factor(dat.ID$ID2, labels = 1:7) ##Convert to dataframe x <- as.data.frame(x) The more typical methods for converting a factor to a character vector would be: (

Re: [R] Simple loop code

2010-04-29 Thread RCulloch
Thanks Henrique, that works! for anyone else as slow as me, just: ##Assign x <- factor(dat.ID$ID2, labels = 1:7) ##Convert to dataframe x <- as.data.frame(x) ##Then bind to your data z <- cbind(y,x) Thanks again, I expected it to be more complicated! Cheers, Ross -- View this message in

Re: [R] Simple loop code

2010-04-29 Thread Henrique Dallazuanna
Try this: factor(dat.ID$ID2, labels = 1:7) On Thu, Apr 29, 2010 at 8:39 AM, RCulloch wrote: > > Hi fellow R Users, > > I find that I typically rewrite my data specific to data in columns, which > is by no means efficient and I am struggling to break out of this bad habit > and utalise some of t

[R] Simple loop code

2010-04-29 Thread RCulloch
Hi fellow R Users, I find that I typically rewrite my data specific to data in columns, which is by no means efficient and I am struggling to break out of this bad habit and utalise some of the excellent things R can do! I have tried to look at 'for' but I don't really follow it, and I wondered i

Re: [R] simple loop iteration

2010-03-30 Thread Peter Ehlers
Perhaps you're just looking for the diff() function? See ?diff. -Peter Ehlers On 2010-03-30 7:15, Niklaus Hurlimann wrote: Hi R mailing list, probably a very basic problem here, I try to do the following: Q<-c(1,2,3) P<-c(4,5,6) A<- data.frame(Q,P) A Q P 1 1 4 2 2 5 3 3 6 this is my si

Re: [R] simple loop iteration

2010-03-30 Thread Henrique Dallazuanna
Try this: Reduce("-", as.data.frame(embed(A$P, 2))) On Tue, Mar 30, 2010 at 10:15 AM, Niklaus Hurlimann wrote: > Hi R mailing list, > > probably a very basic problem here, I try to do the following: > >> Q<-c(1,2,3) >> P<-c(4,5,6) >> A<- data.frame(Q,P) >> A >  Q P > 1 1 4 > 2 2 5 > 3 3 6 > > th

[R] simple loop iteration

2010-03-30 Thread Niklaus Hurlimann
Hi R mailing list, probably a very basic problem here, I try to do the following: > Q<-c(1,2,3) > P<-c(4,5,6) > A<- data.frame(Q,P) > A Q P 1 1 4 2 2 5 3 3 6 this is my simplified data.frame (matrix) now I try to create following loop for subtraction of element within the data.frame: > for(i

Re: [R] simple loop

2009-06-28 Thread hadley wickham
> Also consider ddply in the plyr package (although that's an over kill if > your only having two loops) Maybe, but it sure is much simpler: library(plyr) ddply(data, c("industry","year"), summarise, avg = mean(X1)) Hadley -- http://had.co.nz/ __ R-

Re: [R] simple loop

2009-06-28 Thread Tal Galili
Hi Cecilia, Trying it your way there where three reasons for errors, I fixed them in the following code: means<-matrix(nrow=3,ncol=4) counter.i <- 0 counter.j <- 0 for (i in levels(factor(data$industry))) { counter.i <- counter.i + 1 for (j in levels(factor(data$year))) { counter.j <- counter

Re: [R] simple loop

2009-06-28 Thread Jorge Ivan Velez
Dear Cecilia, Here is one way: with(, tapply(X1, list(year, industry), mean)) Also, take a look at ?ave and its examples. HTH, Jorge On Sun, Jun 28, 2009 at 12:39 PM, Cecilia Carmo wrote: > Hi everyone! > > I have this dataframe: > > firm<-c(rep(1,4),rep(2,4),rep(3,4),rep(4,4),rep(5,4),rep

[R] simple loop

2009-06-28 Thread Cecilia Carmo
Hi everyone! I have this dataframe: firm<-c(rep(1,4),rep(2,4),rep(3,4),rep(4,4),rep(5,4),rep(6,4)) year<-c(rep(2000:2003,6)) industry<-c(rep(10,4),rep(20,4),rep(30,4),rep(10,4),rep(20,4),rep(30,4)) X1<-c(10,14,18,16,20,45,23,54,24,67,98,58,16,32,57,12,54,0,0,22,11,3,5,6) data<-data.frame(firm, i