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   1   one  a
2   2   two  b
3   3 three  a
> str(.Last.value)
'data.frame':   3 obs. of  3 variables:
 $ Num: int  1 2 3
 $ F1 : chr  "one" "two" "three"
 $ F2 : chr  "a" "b" "a"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Oct 31, 2016 at 12:39 PM, Yahya Laraki 
wrote:

> Thank you for your help! I found this solution much simpler:
> d <- data.frame(Num=1:3, F1=c("One","Two","Three"), F2=c("A","B","a »))
> d %>% mutate_if(is.factor, to lower)
>
> Thank you again for taking time to respond!!
>
> Yahya
>
> Le 31 oct. 2016 à 15:28, William Dunlap  a écrit :
>
> 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
> > for(n in names(d))
> +if (is.factor(d[[n]])) levels(d[[n]]) <- tolower(levels(d[[n]]))
> > 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/ 2 levels "a","b": 1 2 1
>
> Using data.frame(tolower(as.matrix(d))) may change your column names
> and the data in your columns - don't do it.
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Mon, Oct 31, 2016 at 7:44 AM, Yahya Laraki 
> wrote:
>
>> 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 my columns : change_lower = function(x)
>> {data.frame(tolower(as.matrix(x)))}
>>
>> I need more something like this, but it doesn’t work: function (x) { for
>> (i in 1:length(x)) {
>>   if (class(i)=="factor") {
>> data.frame(tolower(as.matrix(x)))
>>   }
>> }}
>>
>> Thank you
>>
>> Yahya
>> __
>> 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/posti
>> ng-guide.html 
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>

[[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] 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
> for(n in names(d))
+if (is.factor(d[[n]])) levels(d[[n]]) <- tolower(levels(d[[n]]))
> 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/ 2 levels "a","b": 1 2 1

Using data.frame(tolower(as.matrix(d))) may change your column names
and the data in your columns - don't do it.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Oct 31, 2016 at 7:44 AM, Yahya Laraki  wrote:

> 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 my columns : change_lower = function(x)
> {data.frame(tolower(as.matrix(x)))}
>
> I need more something like this, but it doesn’t work: function (x) { for
> (i in 1:length(x)) {
>   if (class(i)=="factor") {
> data.frame(tolower(as.matrix(x)))
>   }
> }}
>
> Thank you
>
> Yahya
> __
> 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.

[[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] Simple loop problem

2016-10-31 Thread David L Carlson
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 object of that class?
What is the value of class(i) regardless of the value of i?
What is the difference between class "character" and class "factor"?

If you can answer these questions, it will be obvious what is wrong with your 
code. They are all available online in basic introductions to R.
-
David L Carlson
Department of Anthropology
Texas A University
College Station, TX 77840-4352


-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Yahya Laraki
Sent: Monday, October 31, 2016 9:45 AM
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 respect a condition (class = factor)

This code works, but for all my columns : change_lower = function(x) 
{data.frame(tolower(as.matrix(x)))}

I need more something like this, but it doesn’t work: function (x) { for (i in 
1:length(x)) {
  if (class(i)=="factor") {
data.frame(tolower(as.matrix(x))) 
  }
}}

Thank you 

Yahya
__
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@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] 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 my columns : change_lower = function(x) 
{data.frame(tolower(as.matrix(x)))}

I need more something like this, but it doesn’t work: function (x) { for (i in 
1:length(x)) {
  if (class(i)=="factor") {
data.frame(tolower(as.matrix(x))) 
  }
}}

Thank you 

Yahya
__
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] 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( , ncol = 2, nrow = 26) # empty matrix
n - 0 #set n to 0
for(i in letters) {
  n - n+1
  x[n,] - c(i, n)
}
x
#***
#2.
for (i in 1:10) {
cat(loop, i, \n)
}

__
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] Simple loop question

2014-03-29 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  n
# 3   3  c  C 13  m
# 4   4  d  D 12  l
# 5   5  e  E 11  k
# 6   6  f  F 10  j
# 7   7  g  G  9  i
# 8   8  h  H  8  h
# 9   9  i  I  7  g
# 10 10  j  J  6  f
# 11 11  k  K  5  e
# 12 12  l  L  4  d
# 13 13  m  M  3  c
# 14 14  n  N  2  b
# 15 15  o  O  1  a




# Alternate 1

summary(df)

# prints the following but trims the longer summaries:

# a1 a2  a3  a4 a5
# Min.   : 1.0   a  :1   A  :1   Min.   : 1.0   a  :1
# 1st Qu.: 4.5   b  :1   B  :1   1st Qu.: 4.5   b  :1
# Median : 8.0   c  :1   C  :1   Median : 8.0   c  :1
# Mean   : 8.0   d  :1   D  :1   Mean   : 8.0   d  :1
# 3rd Qu.:11.5   e  :1   E  :1   3rd Qu.:11.5   e  :1
# Max.   :15.0   f  :1   F  :1   Max.   :15.0   f  :1
# (Other):9   (Other):9  (Other):9




# Alternate 2

for (i in 1:length(df)) {
  print(summary(df[[i]]))
}

# prints the following:

#   Min. 1st Qu.  MedianMean 3rd Qu.Max.
# 1.0 4.5 8.0 8.011.515.0
# a b c d e f g h i j k l m n o
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
# A B C D E F G H I J K L M N O
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
# Min. 1st Qu.  MedianMean 3rd Qu.Max.
# 1.0 4.5 8.0 8.011.515.0
# a b c d e f g h i j k l m n o
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1




On 2014-03-28 02:32, Jim Lemon wrote:

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 Luana,
Try this:

for(var in paste(a,1:5,sep=)) print(summary(get(var)))

Jim

__
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-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] Simple loop question

2014-03-28 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 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] Simple loop question

2014-03-28 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 Luana,
Try this:

for(var in paste(a,1:5,sep=)) print(summary(get(var)))

Jim

__
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] 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
 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
Site Prof  H newCol2
  111 24   8
  211 16   0
  311 67  51
  412 23   0
  512 56  33
  612 45  22
  721 67  21
  821 46   0
  Warning message:
  In min(y) : no non-missing arguments to min; returning Inf
 The warning is unfortunate: ave() calls FUN even for when
 there is no data for a particular group (Site=2, Prof=2 in this
 case).
 
 The warning may be avoided using min(y, Inf) instead of min().
 
 Yes, but the fact remains that ave() wastes time and causes
 unnecessary warnings and errors by calling FUN when it knows
 it will do nothing with the result (because there are no entries
 in x with a given combination of the factor levels in the ...
 arguments).
 
 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 result but represent different
 Site/Prof combinations.

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 suppose ?ave should be a bit more explicit about passing ...)

 
 Bill Dunlap
 Spotfire, TIBCO Software
 wdunlap tibco.com 
 
 
  cbind(x, newCol2 = with(x, ave(H, Site, Prof, 
 FUN=function(y)y-min(y,Inf
 
Site Prof  H newCol2
  111 24   8
  211 16   0
  311 67  51
  412 23   0
  512 56  33
  612 45  22
  721 67  21
  821 46   0
 
 Another approach is to combine Site, Prof to a single column
 in any way suitable for the application. For example
 
  cbind(x, newCol2 = with(x, ave(H, paste(Site, Prof), 
 FUN=function(y)y-min(y
 
 Petr Savicky.
 
 __
 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-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.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@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.


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 result but represent different
 Site/Prof combinations.

 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.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
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] 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.
 
 Hadley

Defensive programming, maybe? It's not the default for interaction(), which 
makes sense, and if you pass a non-default option, you cannot revert it. Not 
that I can think up a case where you'd actually want to process the empty 
levels either.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@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.


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:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

peter dalgaard pda...@gmail.com wrote:

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.   Hadley Defensive 
programming, maybe? It's not the default for interaction(), which makes sense, 
and if you pass a non-default option, you cannot revert it. Not that I can 
think up a case where you'd actually want to process the empty levels either. 
-- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg 
Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk 
Priv: PDalgd@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. 


[[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] 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
   412 23   0
   512 56  33
   612 45  22
   721 67  21
   821 46   0
   Warning message:
   In min(y) : no non-missing arguments to min; returning Inf
 The warning is unfortunate: ave() calls FUN even for when
 there is no data for a particular group (Site=2, Prof=2 in this
 case).

The warning may be avoided using min(y, Inf) instead of min().

  cbind(x, newCol2 = with(x, ave(H, Site, Prof, FUN=function(y)y-min(y,Inf

Site Prof  H newCol2
  111 24   8
  211 16   0
  311 67  51
  412 23   0
  512 56  33
  612 45  22
  721 67  21
  821 46   0

Another approach is to combine Site, Prof to a single column
in any way suitable for the application. For example

  cbind(x, newCol2 = with(x, ave(H, paste(Site, Prof), 
FUN=function(y)y-min(y

Petr Savicky.

__
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] 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: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
412 23   0
512 56  33
612 45  22
721 67  21
821 46   0
Warning message:
In min(y) : no non-missing arguments to min; returning Inf
  The warning is unfortunate: ave() calls FUN even for when
  there is no data for a particular group (Site=2, Prof=2 in this
  case).
 
 The warning may be avoided using min(y, Inf) instead of min().

Yes, but the fact remains that ave() wastes time and causes
unnecessary warnings and errors by calling FUN when it knows
it will do nothing with the result (because there are no entries
in x with a given combination of the factor levels in the ...
arguments).

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 result but represent different
Site/Prof combinations.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

 
   cbind(x, newCol2 = with(x, ave(H, Site, Prof, 
 FUN=function(y)y-min(y,Inf
 
 Site Prof  H newCol2
   111 24   8
   211 16   0
   311 67  51
   412 23   0
   512 56  33
   612 45  22
   721 67  21
   821 46   0
 
 Another approach is to combine Site, Prof to a single column
 in any way suitable for the application. For example
 
   cbind(x, newCol2 = with(x, ave(H, paste(Site, Prof), 
 FUN=function(y)y-min(y
 
 Petr Savicky.
 
 __
 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-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] Simple loop

2011-05-04 Thread Petr Savicky
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-project.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
   Site Prof  H newCol2
 111 24   8
 211 16   0
 311 67  51
 412 23   0
 512 56  33
 612 45  22
 721 67  21
 821 46   0
 Warning message:
 In min(y) : no non-missing arguments to min; returning Inf
   The warning is unfortunate: ave() calls FUN even for when
   there is no data for a particular group (Site=2, Prof=2 in this
   case).
  
  The warning may be avoided using min(y, Inf) instead of min().
 
 Yes, but the fact remains that ave() wastes time and causes
 unnecessary warnings and errors by calling FUN when it knows
 it will do nothing with the result (because there are no entries
 in x with a given combination of the factor levels in the ...
 arguments).

I agree. For the original question, avoiding the warning is
preferrable. The general question belongs more to R-devel.

 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 result but represent different
 Site/Prof combinations.

Thank you for this remark. I used the formulation combine
... in any way suitable for the application with this effect in
mind, but let us be more specific. For numbers, in particular
integers, paste() seems to be good enough. For character vectors,
a possible approach is

  paste(X, Y, sep=\r)

since the character \r is unlikely to be used in character
vectors. A similar approach is used, for example in unique.matrix().
I did not like it much, but it also has advantages.

Petr Savicky.

__
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] 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
And I would like to create a new column that subtracts the minimum of H from
H, but for S1 and P1
only the minimum of the data points falling into this category should be
taken.
So for example the three first numbers of the new column write: 24-16,
16-16, 67-16
the following numbers refering to Site1 and Prof2 write: 23-23, 56-23,
45-23.
I think with two loops one refering to the Site, the other to the Prof, it
should be possible to automatically
create the new column.
Thanks a lot for any help.

--
View this message in context: 
http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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] 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 between with and within
as a bonus.

HTH,
Jon

On Tue, May 3, 2011 at 11:44 AM, Woida71 w.gost...@ipp.bz.it wrote:
 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
 And I would like to create a new column that subtracts the minimum of H from
 H, but for S1 and P1
 only the minimum of the data points falling into this category should be
 taken.
 So for example the three first numbers of the new column write: 24-16,
 16-16, 67-16
 the following numbers refering to Site1 and Prof2 write: 23-23, 56-23,
 45-23.
 I think with two loops one refering to the Site, the other to the Prof, it
 should be possible to automatically
 create the new column.
 Thanks a lot for any help.

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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.




-- 
===
Jon Daily
Technician
===
#!/usr/bin/env outside
# It's great, trust me.

__
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] 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,newCol=unlist(tapply(x[,3],paste(x[,1],x[,2],sep=),
function(x) x-min(x)))
   Site Prof  H newCol
11111 24  8
11211 16  0
11311 67 51
12112 23  0
12212 56 33
12312 45 22
21121 67 21
21221 46  0

Andrija


On Tue, May 3, 2011 at 5:44 PM, Woida71 w.gost...@ipp.bz.it wrote:

 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
 And I would like to create a new column that subtracts the minimum of H
 from
 H, but for S1 and P1
 only the minimum of the data points falling into this category should be
 taken.
 So for example the three first numbers of the new column write: 24-16,
 16-16, 67-16
 the following numbers refering to Site1 and Prof2 write: 23-23, 56-23,
 45-23.
 I think with two loops one refering to the Site, the other to the Prof, it
 should be possible to automatically
 create the new column.
 Thanks a lot for any help.

 --
 View this message in context:
 http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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.


[[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] Simple loop

2011-05-03 Thread William Dunlap


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
 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  2 45
 2  1 67
 2  1 46), header = TRUE)
 closeAllConnections()
 x
 cbind(x,newCol=unlist(tapply(x[,3],paste(x[,1],x[,2],sep=),
 function(x) x-min(x)))
Site Prof  H newCol
 11111 24  8
 11211 16  0
 11311 67 51
 12112 23  0
 12212 56 33
 12312 45 22
 21121 67 21
 21221 46  0

That works when Site and Prof are ordered as shown, but if 
they are not sorted cbind(...,tapply) won't line up the the
new entries with the old rows properly.  Try doing it on
x[8:1,] to see this.  

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
  412 23   0
  512 56  33
  612 45  22
  721 67  21
  821 46   0
  Warning message:
  In min(y) : no non-missing arguments to min; returning Inf
The warning is unfortunate: ave() calls FUN even for when
there is no data for a particular group (Site=2, Prof=2 in this
case).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 


 
 Andrija
 
 
 On Tue, May 3, 2011 at 5:44 PM, Woida71 w.gost...@ipp.bz.it wrote:
 
  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
  And I would like to create a new column that subtracts the 
 minimum of H
  from
  H, but for S1 and P1
  only the minimum of the data points falling into this 
 category should be
  taken.
  So for example the three first numbers of the new column 
 write: 24-16,
  16-16, 67-16
  the following numbers refering to Site1 and Prof2 write: 
 23-23, 56-23,
  45-23.
  I think with two loops one refering to the Site, the other 
 to the Prof, it
  should be possible to automatically
  create the new column.
  Thanks a lot for any help.
 
  --
  View this message in context:
  http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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.
 
 
   [[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-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] Simple loop

2011-05-03 Thread andrija djurovic
William, you are right. Thanks for clarification.

Andrija

On Tue, May 3, 2011 at 9:04 PM, William Dunlap wdun...@tibco.com wrote:



 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
  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  2 45
  2  1 67
  2  1 46), header = TRUE)
  closeAllConnections()
  x
  cbind(x,newCol=unlist(tapply(x[,3],paste(x[,1],x[,2],sep=),
  function(x) x-min(x)))
 Site Prof  H newCol
  11111 24  8
  11211 16  0
  11311 67 51
  12112 23  0
  12212 56 33
  12312 45 22
  21121 67 21
  21221 46  0

 That works when Site and Prof are ordered as shown, but if
 they are not sorted cbind(...,tapply) won't line up the the
 new entries with the old rows properly.  Try doing it on
 x[8:1,] to see this.

 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
  412 23   0
  512 56  33
  612 45  22
  721 67  21
  821 46   0
  Warning message:
  In min(y) : no non-missing arguments to min; returning Inf
 The warning is unfortunate: ave() calls FUN even for when
 there is no data for a particular group (Site=2, Prof=2 in this
 case).

 Bill Dunlap
 Spotfire, TIBCO Software
 wdunlap tibco.com


 
  Andrija
 
 
  On Tue, May 3, 2011 at 5:44 PM, Woida71 w.gost...@ipp.bz.it wrote:
 
   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
   And I would like to create a new column that subtracts the
  minimum of H
   from
   H, but for S1 and P1
   only the minimum of the data points falling into this
  category should be
   taken.
   So for example the three first numbers of the new column
  write: 24-16,
   16-16, 67-16
   the following numbers refering to Site1 and Prof2 write:
  23-23, 56-23,
   45-23.
   I think with two loops one refering to the Site, the other
  to the Prof, it
   should be possible to automatically
   create the new column.
   Thanks a lot for any help.
  
   --
   View this message in context:
   http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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.
  
 
[[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.
 


[[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] 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   33
612 45   22
721 67   21
821 460

library(data.table)
dt - data.table(dd, key = 'Site, Prof')
dt[, list(H = H, Hadj = H - min(H)), by = 'Site, Prof']

same output as above

HTH,
Dennis

On Tue, May 3, 2011 at 8:44 AM, Woida71 w.gost...@ipp.bz.it wrote:
 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
 And I would like to create a new column that subtracts the minimum of H from
 H, but for S1 and P1
 only the minimum of the data points falling into this category should be
 taken.
 So for example the three first numbers of the new column write: 24-16,
 16-16, 67-16
 the following numbers refering to Site1 and Prof2 write: 23-23, 56-23,
 45-23.
 I think with two loops one refering to the Site, the other to the Prof, it
 should be possible to automatically
 create the new column.
 Thanks a lot for any help.

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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-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] 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, xu...@pdx.edu 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 just two values of 2 and 4. I want to match column C  
with column B, and return the matched indexes. So what I desire is  
to return:


[1] 1 2 6
[2] 3 5

Since value 2 corresponds to indexes 1,2,6, and 4 corresponds to  
indexes 3,5.


Assuming this to be in a data.frame named dat:

 dat$A[which(dat$B==2) ]
[1] 1 2 6


I've been reminded that in many instances one gets comparable results  
with dat$A[ dat$B==2 ] (logical indexing).


The reason I choose to use which() is that it returns a numeric vector  
and leaves out any NA's that result from dat$B==2. the [ function  
returns all logical NA rows. It may be important to return such NA's  
to alert the analyst to their presence. (This does result in extensive/ 
useless/unexpected screen output when large datasets with even a small  
faction of NA's are being manipulated.)


--
David


 dat$A[which(dat$B==4) ]
[1] 3 5





Results of varying lengths generally need to be returned in list form:

 sapply(c(2,4), function(x) dat$A[which(dat$B==x) ] )
[[1]]
[1] 1 2 6

[[2]]
[1] 3 5





David Winsemius, MD
West Hartford, CT

__
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] 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 column C with column  
B, and return the matched indexes. So what I desire is to return:


[1] 1 2 6
[2] 3 5

Since value 2 corresponds to indexes 1,2,6, and 4 corresponds to indexes 3,5.

Is there any way to write a loop to have this done? Thank you for the help.

Xueke

__
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] simple loop questions

2011-04-23 Thread David Winsemius


On Apr 23, 2011, at 6:31 AM, xu...@pdx.edu 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 just two values of 2 and 4. I want to match column C with  
column B, and return the matched indexes. So what I desire is to  
return:


[1] 1 2 6
[2] 3 5

Since value 2 corresponds to indexes 1,2,6, and 4 corresponds to  
indexes 3,5.


Assuming this to be in a data.frame named dat:

 dat$A[which(dat$B==2) ]
[1] 1 2 6
 dat$A[which(dat$B==4) ]
[1] 3 5

Results of varying lengths generally need to be returned in list form:

 sapply(c(2,4), function(x) dat$A[which(dat$B==x) ] )
[[1]]
[1] 1 2 6

[[2]]
[1] 3 5



Is there any way to write a loop to have this done? Thank you for  
the help.


Xueke

__
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.


David Winsemius, MD
West Hartford, CT

__
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] 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- (c(0:N))/N
for (j in 1:p)
{
z - rnorm(N,0,1)
x[1,j] - 0
y[1,j] - S0
for (i in 1:N)
{
x[i+1,j] - (1/sqrt(N))*sum(z[1:i])
y[i+1,j] - y[1,j]*exp(nu*t[i+1]+sigma*x[i+1,j])
}

plot(t,y,type=l,xlab=time, ylab=Geometric Brownian motion)

}


Any help would be appreciated, thanks.


-- 
View this message in context: 
http://r.789695.n4.nabble.com/simple-loop-problemo-Geo-brownian-motion-tp3050762p3050762.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] 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(rep(0,(N+1)*p),nrow=(N+1))
t- (c(0:N))/N
for (j in 1:p)
{
z- rnorm(N,0,1)
x[1,j]- 0
y[1,j]- S0
for (i in 1:N)
{
x[i+1,j]- (1/sqrt(N))*sum(z[1:i])
y[i+1,j]- y[1,j]*exp(nu*t[i+1]+sigma*x[i+1,j])
}

plot(t,y,type=l,xlab=time, ylab=Geometric Brownian motion)

}


Any help would be appreciated, thanks.



You can use the matplot function to plot multiple columns of a matrix at 
once.  So with your code as above, leave out the call to plot(), then  
matplot(t, y) would give you approximately what you're looking for 
(though the colour and symbol choices need some work).


You can get rid of the inner loop by using cumsum() and vectorized 
calculations; then it will be reasonably fast.


Another way to do this is to create the plot before the loop, then use 
lines() to add lines to it within the loop.  Then you need to guess the 
scale ahead of time, or do a little experimentation.


Duncan Murdoch

__
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] 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 several parallel groups of
random walks:

https://github.com/hadley/ggplot2/wiki/Using-ggplot2-animations-to-demonstrate-several-parallel-numerical-experiments-in-a-single-layout

Regards,
Yihui
--
Yihui Xie xieyi...@gmail.com
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



On Fri, Nov 19, 2010 at 12:09 PM, newbster gpm8...@yahoo.com 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(rep(0,(N+1)*p),nrow=(N+1))
 t- (c(0:N))/N
 for (j in 1:p)
 {
 z - rnorm(N,0,1)
 x[1,j] - 0
 y[1,j] - S0
 for (i in 1:N)
 {
 x[i+1,j] - (1/sqrt(N))*sum(z[1:i])
 y[i+1,j] - y[1,j]*exp(nu*t[i+1]+sigma*x[i+1,j])
 }

 plot(t,y,type=l,xlab=time, ylab=Geometric Brownian motion)

 }


 Any help would be appreciated, thanks.


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/simple-loop-problemo-Geo-brownian-motion-tp3050762p3050762.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-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] 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 species at a time.
I have been searching for a thread that will help me to understand how best
to repeat this process for each different species identifier in that
variable column. I can’t seem to find one that is similar to what I am
trying to do. It might be the case that I am not looking for the right thing
or that I do not fully understand the process.

How do I run a simple loop that produces a regression for each species as
identified in the variable species id, which is one column in the large data
set that I am using?

Simple regression that I wish to repeat

data- read.table(…/STUDY.txt,header=T)
names(data)
model- with(data,{lm(o2con~bm)})
summary(model)


sample data set

species id  o2con   bm
1   0.5 5
1   0.6 2
1   0.4 4
1   0.4 2
1   0.5 3
2   0.3 7
2   0.4 8
2   0.5 3
2   0.7 4
2   0.9 2
3   0.3 6
3   0.7 2
3   0.4 1
3   0.3 7
5   0.3 2
5   0.6 1
5   0.9 7
5   0.2 5

I would be very grateful for some help with this. I really like using R and
I can usually figure out what I want to do but I have been trying to figure
this out for a while now and I am getting nowhere. 

Thank you.

-- 
View this message in context: 
http://r.789695.n4.nabble.com/simple-loop-analysing-subsets-tp2293383p2293383.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] 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 it easier to keep code
straight.

HTH,

Josh


On Sun, Jul 18, 2010 at 2:29 PM, karmakiller roisinmoria...@gmail.com wrote:

 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 species at a time.
 I have been searching for a thread that will help me to understand how best
 to repeat this process for each different species identifier in that
 variable column. I can’t seem to find one that is similar to what I am
 trying to do. It might be the case that I am not looking for the right thing
 or that I do not fully understand the process.

 How do I run a simple loop that produces a regression for each species as
 identified in the variable species id, which is one column in the large data
 set that I am using?

 Simple regression that I wish to repeat

 data- read.table(…/STUDY.txt,header=T)
 names(data)
 model- with(data,{lm(o2con~bm)})
 summary(model)


 sample data set

 species id      o2con       bm
 1               0.5         5
 1               0.6         2
 1               0.4         4
 1               0.4         2
 1               0.5         3
 2               0.3         7
 2               0.4         8
 2               0.5         3
 2               0.7         4
 2               0.9         2
 3               0.3         6
 3               0.7         2
 3               0.4         1
 3               0.3         7
 5               0.3         2
 5               0.6         1
 5               0.9         7
 5               0.2         5

 I would be very grateful for some help with this. I really like using R and
 I can usually figure out what I want to do but I have been trying to figure
 this out for a while now and I am getting nowhere.

 Thank you.

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/simple-loop-analysing-subsets-tp2293383p2293383.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.




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.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] 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, which
may require a bit more ingenuity in delving into aRcana :)

-
Aside:
To reinforce Joshua's point, variable names with spaces not explicitly
enclosed in quotes is bad practice, especially when someone who wants to
help tries to copy and paste your data into his/her R session:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
:
  line 1 did not have 4 elements

R expected four columns of data, but you provided three. In the future, it's
a good idea to include your data example with dput(), which outputs

dput(d)
structure(list(species = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L), o2con = c(0.5, 0.6, 0.4,
0.4, 0.5, 0.3, 0.4, 0.5, 0.7, 0.9, 0.3, 0.7, 0.4, 0.3, 0.3, 0.6,
0.9, 0.2), bm = c(5L, 2L, 4L, 2L, 3L, 7L, 8L, 3L, 4L, 2L, 6L,
2L, 1L, 7L, 2L, 1L, 7L, 5L)), .Names = c(species, o2con,
bm), class = data.frame, row.names = c(NA, -18L))

This is easily copied and pasted into anyone's R sessionbut I digress.
--

Calling your data frame d, here's how to run the same regression model on
all species:

# Create a function to perform the modeling, taking a data frame df as input
f - function(df) lm(o2con ~ bm, data = df)

# Use lapply() to apply the function to each 'split' of the data, by
species:
v - lapply(split(d, d$species), f)

# v is a list object, where each component of the list is an lm object,
# which itself is a list. In other words, it's a list of lists. do.call() is
a
# very useful function that applies a function to components of a list.
# rbind and cbind are commonly used to slurp together common elements
# from each component of a list.

# Pulling out the coefficients from each model:
 do.call(rbind, lapply(v, coef))
  (Intercept)  bm
1   0.5176471 -0.01176471
2   0.9253731 -0.07611940
3   0.5942308 -0.04230769
5   0.3351648  0.04395604

# Extract the r-squared values from each model:
g - function(m) summary(m)$r.squared
 do.call(rbind, lapply(v, g))
[,1]
1 0.03361345
2 0.66932578
3 0.43291592
5 0.14652015

# But you have to be careful...e.g., since you have unequal sample sizes per
species,
 do.call(cbind, lapply(v, resid))
1   23  5
1  0.04117647 -0.09253731 -0.040384615 -0.1230769
2  0.10588235  0.08358209  0.190384615  0.2208791
3 -0.07058824 -0.19701493 -0.151923077  0.2571429
4 -0.09411765  0.07910448  0.001923077 -0.3549451
5  0.01764706  0.12686567 -0.040384615 -0.1230769
Warning message:
In function (..., deparse.level = 1)  :
  number of rows of result is not a multiple of vector length (arg 3)

Notice how the first residual is recycled in each of groups 3 and 5. That's
a potential gotcha.

This gives you a small glimpse into the power that R can deliver in data
analysis.

HTH,
Dennis

On Sun, Jul 18, 2010 at 2:29 PM, karmakiller roisinmoria...@gmail.comwrote:


 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 species at a
 time.
 I have been searching for a thread that will help me to understand how best
 to repeat this process for each different species identifier in that
 variable column. I can’t seem to find one that is similar to what I am
 trying to do. It might be the case that I am not looking for the right
 thing
 or that I do not fully understand the process.

 How do I run a simple loop that produces a regression for each species as
 identified in the variable species id, which is one column in the large
 data
 set that I am using?

 Simple regression that I wish to repeat

 data- read.table(…/STUDY.txt,header=T)
 names(data)
 model- with(data,{lm(o2con~bm)})
 summary(model)


 sample data set

 species id  o2con   bm
 1   0.5 5
 1   0.6 2
 1   0.4 4
 1   0.4 2
 1   0.5 3
 2   0.3 7
 2   0.4 8
 2   0.5 3
 2   0.7 4
 2   0.9 2
 3   0.3 6
 3   0.7 2
 3   0.4 1
 3   0.3 7
 5   0.3 2
 5   0.6 1
 5   0.9 7
 5   0.2 5

 I would be very grateful for some help with this. I really like using R and
 I can usually figure out what I want to do but I have been trying to figure
 this out for a while now and I am getting nowhere.

 Thank you.

 --
 View this 

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 djmu...@gmail.com wrote:
 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, which
 may require a bit more ingenuity in delving into aRcana :)

 -
 Aside:
 To reinforce Joshua's point, variable names with spaces not explicitly
 enclosed in quotes is bad practice, especially when someone who wants to
 help tries to copy and paste your data into his/her R session:

 Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
 :
  line 1 did not have 4 elements

 R expected four columns of data, but you provided three. In the future, it's
 a good idea to include your data example with dput(), which outputs

 dput(d)
 structure(list(species = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
 2L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L), o2con = c(0.5, 0.6, 0.4,
 0.4, 0.5, 0.3, 0.4, 0.5, 0.7, 0.9, 0.3, 0.7, 0.4, 0.3, 0.3, 0.6,
 0.9, 0.2), bm = c(5L, 2L, 4L, 2L, 3L, 7L, 8L, 3L, 4L, 2L, 6L,
 2L, 1L, 7L, 2L, 1L, 7L, 5L)), .Names = c(species, o2con,
 bm), class = data.frame, row.names = c(NA, -18L))

 This is easily copied and pasted into anyone's R sessionbut I digress.
 --

 Calling your data frame d, here's how to run the same regression model on
 all species:

 # Create a function to perform the modeling, taking a data frame df as input
 f - function(df) lm(o2con ~ bm, data = df)

 # Use lapply() to apply the function to each 'split' of the data, by
 species:
 v - lapply(split(d, d$species), f)

 # v is a list object, where each component of the list is an lm object,
 # which itself is a list. In other words, it's a list of lists. do.call() is
 a
 # very useful function that applies a function to components of a list.
 # rbind and cbind are commonly used to slurp together common elements
 # from each component of a list.

 # Pulling out the coefficients from each model:
 do.call(rbind, lapply(v, coef))
  (Intercept)          bm
 1   0.5176471 -0.01176471
 2   0.9253731 -0.07611940
 3   0.5942308 -0.04230769
 5   0.3351648  0.04395604

 # Extract the r-squared values from each model:
 g - function(m) summary(m)$r.squared
 do.call(rbind, lapply(v, g))
        [,1]
 1 0.03361345
 2 0.66932578
 3 0.43291592
 5 0.14652015

 # But you have to be careful...e.g., since you have unequal sample sizes per
 species,
 do.call(cbind, lapply(v, resid))
            1           2            3          5
 1  0.04117647 -0.09253731 -0.040384615 -0.1230769
 2  0.10588235  0.08358209  0.190384615  0.2208791
 3 -0.07058824 -0.19701493 -0.151923077  0.2571429
 4 -0.09411765  0.07910448  0.001923077 -0.3549451
 5  0.01764706  0.12686567 -0.040384615 -0.1230769
 Warning message:
 In function (..., deparse.level = 1)  :
  number of rows of result is not a multiple of vector length (arg 3)

 Notice how the first residual is recycled in each of groups 3 and 5. That's
 a potential gotcha.

 This gives you a small glimpse into the power that R can deliver in data
 analysis.

 HTH,
 Dennis

 On Sun, Jul 18, 2010 at 2:29 PM, karmakiller roisinmoria...@gmail.comwrote:


 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 species at a
 time.
 I have been searching for a thread that will help me to understand how best
 to repeat this process for each different species identifier in that
 variable column. I can’t seem to find one that is similar to what I am
 trying to do. It might be the case that I am not looking for the right
 thing
 or that I do not fully understand the process.

 How do I run a simple loop that produces a regression for each species as
 identified in the variable species id, which is one column in the large
 data
 set that I am using?

 Simple regression that I wish to repeat

 data- read.table(…/STUDY.txt,header=T)
 names(data)
 model- with(data,{lm(o2con~bm)})
 summary(model)


 sample data set

 species id      o2con       bm
 1               0.5         5
 1               0.6         2
 1               0.4         4
 1               0.4         2
 1               0.5         3
 2               0.3         7
 2               0.4         8
 2               0.5         3
 2               0.7         4
 2               0.9         2
 3               0.3         6
 3               0.7         2
 3               0.4         1
 3               0.3         7
 5               0.3         2
 5               0.6         1
 5               0.9         7
 

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-tp2075322p2123641.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] 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 if anyone could help with
a simple example using my script so I could follow this and build on it, so
for example, wanting to change an ID code from alphanumeric to numeric. The
example below works, but takes ages, given I have a lot of IDs, to do
manually! 

Any thoughts on how to create a loop to go through each ID and give them a
unique number would be most welcome!

Cheers,

Ross


levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A1']-1
levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A2']-2
levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D1']-3
levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D2']-4
levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D4']-5
levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D5']-6
levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D6']-7
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075322.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] 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 ross.cull...@dur.ac.uk 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 the excellent things R can do! I have tried to look at
 'for' but I don't really follow it, and I wondered if anyone could help
 with
 a simple example using my script so I could follow this and build on it, so
 for example, wanting to change an ID code from alphanumeric to numeric. The
 example below works, but takes ages, given I have a lot of IDs, to do
 manually!

 Any thoughts on how to create a loop to go through each ID and give them a
 unique number would be most welcome!

 Cheers,

 Ross


 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A1']-1
 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A2']-2
 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D1']-3
 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D2']-4
 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D4']-5
 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D5']-6
 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D6']-7
 --
 View this message in context:
 http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075322.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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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] 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 context: 
http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075586.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] 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:


(ff - factor(substring(statistics, 1:10, 1:10), levels=letters))
 levels(ff)[ff]
# [1] s t a t i s t i c s
 as.character(ff)
# [1] s t a t i s t i c s


##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 all of their  
elements to be of the same type. Numeric columns would become  
character vectors. Not generally a desired result. This would be safer:


dat.I$ID2.cf  - as.character( factor(dat.ID$ID2, labels = 1:7)  )
--
David.


Thanks again, I expected it to be more complicated!

Cheers,

Ross
--
View this message in context: 
http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075586.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.


David Winsemius, MD
West Hartford, CT

__
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] Simple loop code

2010-04-29 Thread Henrique Dallazuanna
On Thu, Apr 29, 2010 at 12:05 PM, David Winsemius dwinsem...@comcast.netwrote:


 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:

 (ff - factor(substring(statistics, 1:10, 1:10), levels=letters))
  levels(ff)[ff]
 # [1] s t a t i s t i c s
  as.character(ff)
 # [1] s t a t i s t i c s


  ##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 all of their elements to be of
 the same type. Numeric columns would become character vectors. Not generally
 a desired result. This would be safer:


X is a data.frame, so, the object created is a data.frame.


 dat.I$ID2.cf  - as.character( factor(dat.ID$ID2, labels = 1:7)  )
 --
 David.


 Thanks again, I expected it to be more complicated!

 Cheers,

 Ross
 --
 View this message in context:
 http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075586.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.


 David Winsemius, MD
 West Hartford, CT

 __
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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] 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 dwinsem...@comcast.net 
 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 all of their  
elements to be of the same type. Numeric columns would become  
character vectors. Not generally a desired result. This would be  
safer:



X is a data.frame, so, the object created is a data.frame.


Thank you for the correction. I probably read at one time that there  
was a cbind.data.frame method but I obviously forgot that point. Good  
to know.




dat.I$ID2.cf  - as.character( factor(dat.ID$ID2, labels = 1:7)  )
--
David.


Thanks again, I expected it to be more complicated!

Cheers,

Ross






David Winsemius, MD
West Hartford, CT

__
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] 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 in length(A[,P]-1){
  delta[i]- A[i,P]-A[i+1,P]
}

All I get is a vector of  the correct length but with no readings.

Thanks for any help on this.



-- 
Niklaus Hürlimann

Université de Lausanne  
Institut de Minéralogie et Géochimie 
L'Anthropole 
CH-1015 Lausanne 
Suisse

E-mail: niklaus.hurlim...@unil.ch
Tel:+41(0)21 692 4452 

[[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] 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
niklaus.hurlim...@unil.ch 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 simplified data.frame (matrix) now I try to create following
 loop for subtraction of element within the data.frame:

 for(i in length(A[,P]-1){
  delta[i]- A[i,P]-A[i+1,P]
 }

 All I get is a vector of  the correct length but with no readings.

 Thanks for any help on this.



 --
 Niklaus Hürlimann

 Université de Lausanne
 Institut de Minéralogie et Géochimie
 L'Anthropole
 CH-1015 Lausanne
 Suisse

 E-mail: niklaus.hurlim...@unil.ch
 Tel:+41(0)21 692 4452

        [[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.





-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
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] 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 simplified data.frame (matrix) now I try to create following
loop for subtraction of element within the data.frame:


for(i in length(A[,P]-1){

   delta[i]- A[i,P]-A[i+1,P]
}

All I get is a vector of  the correct length but with no readings.

Thanks for any help on this.






__
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.


--
Peter Ehlers
University of Calgary

__
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] 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, industry,year,X1)
data

I need a loop that calculates the mean of X1 by year and 
by industry and I need this values in a dataframe or a 
matrix.

I have done:

means-matrix(nrow=3,ncol=4)

for ( i in unique(data$industry))
 for (j in 
data$year){means[i,j]-mean(data$X1,na.rm=TRUE)}


But it doesn´t work. Could anyone help me?

Cecília Carmo

__
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] simple loop

2009-06-28 Thread Jorge Ivan Velez
Dear Cecilia,
Here is one way:

 with(yourdata, 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 cecilia.ca...@ua.pt 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(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, industry,year,X1)
 data

 I need a loop that calculates the mean of X1 by year and by industry and I
 need this values in a dataframe or a matrix.
 I have done:

 means-matrix(nrow=3,ncol=4)

 for ( i in unique(data$industry))
  for (j in data$year){means[i,j]-mean(data$X1,na.rm=TRUE)}

 But it doesn´t work. Could anyone help me?

 Cecília Carmo

 __
 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.


[[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] 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.j + 1
 means[counter.i,counter.j]- mean(data$X1 [data$industry == i  data$year
== j]  ,na.rm=TRUE)
}
 counter.j - 0
}

means



Also consider ddply in the plyr package (although that's an over kill if
your only having two loops)

Or Jorge solution.

Cheers,
Tal G





On Sun, Jun 28, 2009 at 7:39 PM, Cecilia Carmo cecilia.ca...@ua.pt wrote:

 for ( i in unique(data$industry))
  for (j in data$year){means[i,j]-mean(data$X1,na.rm=TRUE)}




-- 
--


My contact information:
Tal Galili
Phone number: 972-50-3373767
FaceBook: Tal Galili
My Blogs:
http://www.r-statistics.com/
http://www.talgalili.com
http://www.biostatistics.co.il

[[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] 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-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.