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
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
This looks like homework and r-help has a policy of not providing answers for
homework. But first you need to research what happens when you convert a data
frame to a matrix.
Also in your loop, i is a numeric value between 1 and the length of x:
What is x (what class)?
What is the length of an
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( ,
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
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
"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
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
>> 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
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
On Wed, May 04, 2011 at 08:52:07AM -0700, 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-p
> -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
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
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
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
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -Original Message-
> From: r-help-boun...@r-project.org
> [mailto: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
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
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
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
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
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
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(
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
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,
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
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
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
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)
>>
>
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:
(
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
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
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
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
> 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-
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
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
36 matches
Mail list logo