[R] Generate sequence of date based on a group ID

2014-10-08 Thread Kuma Raj
I want to generate a sequence of date based on a group id(similar IDs
should have same date). The id variable contains unequal observations
and the length of the data set also varies.  How could I create a
sequence that starts on specific date (say January 1, 2000 onwards)
and continues until the end without specifying length?


Sample data follows:

df<-structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,

3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), out1 = c(0L,

0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,

0L, 1L, 0L, 0L, 0L, 1L)), .Names = c("id", "out1"), class =
"data.frame", row.names = c(NA,

-23L))

__
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] Finding Rainfall Amount

2014-10-08 Thread Jim Lemon
On Tue, 7 Oct 2014 07:25:49 PM Hafizuddin Arshad wrote:
> Dear R users,
> 
> I have this kind of data set which is part of my data:
> 
> ...
> 
> I would like to find the rainfall total for each month within a year. The
> result should be like in this form:
> 
> ...
> 
> where V1 until V12 as month and 1 until 80 as a years. How can I do 
this in
> R? 

Hi Arshad,
This might be what you want:

yearcorr<-min(raindat$Year)-1
years<-unique(raindat$Year)
rainmonth<-as.data.frame(matrix(0,nrow=2,ncol=12))
for(year in years) {
 for(month in 1:12) {
  if(any(raindat$Year==year&raindat$Month==month))
   rainmonth[year-yearcorr,month]<-
mean(raindat$Rain[raindat$Year==year&raindat$Month==month],na.rm=TRUE)
 }
}
rownames(rainmonth)<-years
names(rainmonth)<-month.abb

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] Function on an array

2014-10-08 Thread Barry King
ex1 <- c('Y', 'N', 'Y')
ex2 <- c('Y', 'N', 'Y')
ex3 <- c('N', 'N', 'Y')
ex4 <- c('N', 'N', 'Y')

status <- array(NA, dim=3)

# I am trying to return 'Okay' if any of the values
# in a column above is 'Y' but I am not constructing
# the function corrrectly.  Any assistance is
# greatly appreciated

status_fnc <- function(e){
  if (e[ ,1] == 'Y' | e[ ,2] == 'Y' | e[ ,3] == 'Y' | e[ ,4] == 'Y'){
return('Okay')
  } else return('Not okay')
}
status <- sapply(cbind(ex1, ex2, ex3, ex4), status_fnc)

[[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] Function on an array

2014-10-08 Thread Sven E. Templer
Dear Barry,

some thoughts:

1) e in your function status_fnc is a vector when applied on a matrix
like object, but you index it as a matrix (e[,i] should be e[i]).
2) You can simplify the if statement by using the function any
(replacing all the OR statements) on the vector, so use any(e=='Y')
here.
3) sapply applies over a list, which your object isn't. You provide a
matrix (cbind), so you should use apply in that case.

So one solution might be:

ex1 <- c('Y', 'N', 'Y')
ex2 <- c('Y', 'N', 'Y')
ex3 <- c('N', 'N', 'Y')
ex4 <- c('N', 'N', 'Y')
status_fnc <- function(e){
  if (any(e=='Y'))
return('Okay')
  else
return('Not okay')
}
status <- apply(cbind(ex1, ex2, ex3, ex4), 1, status_fnc)
status

On 8 October 2014 12:54, Barry King  wrote:
> ex1 <- c('Y', 'N', 'Y')
> ex2 <- c('Y', 'N', 'Y')
> ex3 <- c('N', 'N', 'Y')
> ex4 <- c('N', 'N', 'Y')
>
> status <- array(NA, dim=3)
>
> # I am trying to return 'Okay' if any of the values
> # in a column above is 'Y' but I am not constructing
> # the function corrrectly.  Any assistance is
> # greatly appreciated
>
> status_fnc <- function(e){
>   if (e[ ,1] == 'Y' | e[ ,2] == 'Y' | e[ ,3] == 'Y' | e[ ,4] == 'Y'){
> return('Okay')
>   } else return('Not okay')
> }
> status <- sapply(cbind(ex1, ex2, ex3, ex4), status_fnc)
>
> [[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] Finding Rainfall Amount

2014-10-08 Thread Duncan Mackay
If you make a factor with year with levels including all the years that you
want then use tapply eg  with dat as the name of the data.frame
The same applies for Month but I have not done so

dat$Yr <- factor(dat$Year, levels = c(1971:1980))
with(dat, tapply(Rain,  list(Yr, Month), sum, na.rm=T))
 12 9101112
1971 138.5 14.1 138.3 144.4 139.8 172.7
1972  36.0   NANANANANA
1973NA   NANANANANA
1974NA   NANANANANA
1975NA   NANANANANA
1976NA   NANANANANA
1977NA   NANANANANA
1978NA   NANANANANA
1979NA   NANANANANA
1980NA   NANANANANA

regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mac...@northnet.com.au

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Hafizuddin Arshad
Sent: Wednesday, 8 October 2014 12:26
To: r-help@r-project.org
Subject: [R] Finding Rainfall Amount

Dear R users,

I have this kind of data set which is part of my data:

structure(list(Year = c(1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1972L, 1972L, 1972L,
1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L), Month = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), Rain = c(33, 32.7, 10.1, 43.1, 0,
11.6, 3, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA,
3.9, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 1.1, 0.1, 0.1, 0.2, 1.1, 5.5, 5, 0, 0, 0,
0, 7.5, 0, 1.5, 0, 1.1, 0.1, 2.1, 0, 0, 0, 8.4, 15.5, 5.1, 5.6,
0, 1.5, 89.9, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6.9, 0, 8.1, 1.9, 0.1,
10.3, 0.5, 3.7, 19.6, 1.6, 7.6, 1.1, 1.1, 41.2, 17.6, 0.6, 2,
0, 2.5, 0.8, 0, 0, 9, 0.3, 1.8, 0.1, 2.3, 1.2, 2.1, 0, 17.5,
7.6, 1.1, 7.8, 4, 0, 0, 0, 66.9, 0, 4.9, 0, 0, 0, 0.9, 0, 0.2,
0, 2.3, 0, 9.1, 9.6, 0, 1.6, 0.7, 0, 0, 0, 30.3, 3.1, 14.4, 0.4,
0, 1.8, 14.7, 17.3, 0, 0, 3.3, 0, 0, 0, 7.5, 0.5, 0.3, 1.2, 3.3,
0, 6.9, 16.4, 0, 5.4, 30.2, 13.9, 1.8, 0, 0, 0, 0, 0, 1.2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 28.3, 2, 0.1, 0.1, 0.1,
0.1, 0.1, 1.8, 0.2, 2, 0, 0)), .Names = c("Year", "Month", "Rain"
), class = "data.frame", row.names = c(NA, -194L))


I would like to find the rainfall total for each month within a year. The
result should be like in this form:

structure(list(V1 = c(28.4, 9.2, 48.8, 185.2, 4.1, 67.6, 49.7,
21.8, 5.6, 24.2, 0.8, 32, 88.9, 61.6, 36.8, 74, 16.3, 2.6, 7.6,
149.7, 13, 65.2, 3.8, 72.1, 49, 3.8, 24.1, 4.3, 19.6, 32.3, 5.6,
54.6, 94.2, 40.7, 101.3, 0.5, 11.7, 9.9, 86.4, 24.3, 91.3, 66.5,
13.2, 0.8, 16, 24.7, 18.1, 13.5, 126.5, 18.1, 62.5, 98.9, 212.6,
19.2, 42.4, 41.6, 73.2, 15, 43.2, 43, 1.4, 167.4, 11.4, 14, 15.6,
23.4, 23.8, 104.2, 58.8, 191.6, 37.2, 121, 36.2, 5.8, 26, 13,
18.6, 63.2, 28.6, 57.8), V2 = c(53.6, 0, 73.7, 106.8, 0, 9.2,
141.6, 32.3

[R] Data formart

2014-10-08 Thread Frederic Ntirenganya
Dear All,

Change the format of the start and end columns so that data appear as the
day of the year. For instance: Apr 24 rather than 115.

The idea is that I want the non=leap years to be 366 days instead of being
365 days. ie. Each year must have 366 days. for example: in the column
Start2, Apr 18 will be Apr 17.

> head(Samaru)
  Year Start End Length Start2   End2
1 1930   108 288180 Apr 18 Oct 15
2 1931   118 288170 Apr 28 Oct 15
3 1932   115 295180 Apr 24 Oct 21
4 1933   156 294138 Jun 05 Oct 21
5 1934   116 291175 Apr 26 Oct 18
6 1935   134 288154 May 14 Oct 15

Any idea is welcome. Thamks
-- 
Frederic Ntirenganya
Maseno University,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

[[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] Changing date format

2014-10-08 Thread Frederic Ntirenganya
The idea is that I want the non-leap years to be 366 days instead of being
365 days. ie. Each year must have 366 days.

for example: in the column Start2, Apr 18 will be Apr 17.

> head(Samaru)
  Year Start End Length Start2   End2
1 1930   108 288180 Apr 18 Oct 15
2 1931   118 288170 Apr 28 Oct 15
3 1932   115 295180 Apr 24 Oct 21
4 1933   156 294138 Jun 05 Oct 21
5 1934   116 291175 Apr 26 Oct 18
6 1935   134 288154 May 14 Oct 15

Is there a way to that in R?

On Tue, Oct 7, 2014 at 3:25 PM, Frederic Ntirenganya 
wrote:

> Thanks All. Your idea is useful!!!
>
> On Tue, Oct 7, 2014 at 1:01 PM, Jim Lemon  wrote:
>
>> On Tue, 7 Oct 2014 11:51:34 AM Göran Broström wrote:
>> > On 2014-10-07 11:27, Jim Lemon wrote:
>> > > On Tue, 7 Oct 2014 10:32:42 AM Frederic Ntirenganya wrote:
>> > >> Dear All,
>> > >>
>> > >> How can I change the format of date of day of the year ? for
>> > >
>> > > example r
>> > >
>> > >> (i.e. "17 Apr" rather than "108").
>> > >>
>> > >> The following is the type of the dataset I have
>> > >>
>> > >> head(Samaru)
>> > >>
>> > >>Year Start End Length
>> > >>
>> > >> 1 1930   108 288180
>> > >> 2 1931   118 288170
>> > >> 3 1932   115 295180
>> > >> 4 1933   156 294138
>> > >> 5 1934   116 291175
>> > >> 6 1935   134 288154
>> > >
>> > > Hi Frederic,
>> > > The easiest method I can think of is this:
>> > >
>> > > Samaru$Start<-format(as.Date(
>> > >
>> > >   paste(Samaru$Year,"01-01",sep="-"))+Samaru$Start,
>> > >   "%b %d")
>> > >
>> > > Samaru$End<-format(as.Date(
>> > >
>> > >   paste(Samaru$Year,"01-01",sep="-"))+Samaru$End,
>> > >   "%b %d")
>> >
>> > In the package 'eha' I have a function 'toDate':
>> >  > require(eha)
>> >  > toDate(1930 + 108/365)
>> >
>> > [1] "1930-04-19"
>> >
>> > (Interestingly, we are all wrong; the correct answer seems to be
>> > "1930-04-18")
>> >
>> >  > toDate
>> >
>> > function (times)
>> > {
>> >  if (!is.numeric(times))
>> >  stop("Argument must be numeric")
>> >  times * 365.2425 + as.Date("-01-01")
>> > }
>> >
>> > The 'inverse' function is 'toTime'.
>> >
>> > Sometimes it misses by one day; not very important in my
>> applications,
>> > but may be otherwise.
>> >
>> Hi Goran,
>> You're correct.
>>
>>  as.Date("Apr 19 1930","%b %d %Y") -
>> + as.Date("Jan 1 1930","%b %d %Y")
>> Time difference of 108 days
>>
>> The t
>>
>> Samaru$Start<-format(as.Date(
>>  paste(Samaru$Year,"01-01",sep="-"))+Samaru$Start-1,"%b %d")
>> Samaru$End<-format(as.Date(
>>  paste(Samaru$Year,"01-01",sep="-"))+Samaru$End-1, "%b %d")
>>
>> 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.
>>
>
>
>
> --
> Frederic Ntirenganya
> Maseno University,
> Kenya.
> Mobile:(+254)718492836
> Email: fr...@aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
>



-- 
Frederic Ntirenganya
Maseno University,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

[[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] Generate sequence of date based on a group ID

2014-10-08 Thread Adams, Jean
Does this work for you?

df$in1 <- 0
df$in1[match(unique(df$id), df$id)] <- 1
df$date <- as.Date("2000-01-01") + cumsum(df$in1) - 1

Jean

On Wed, Oct 8, 2014 at 2:57 AM, Kuma Raj  wrote:

> I want to generate a sequence of date based on a group id(similar IDs
> should have same date). The id variable contains unequal observations
> and the length of the data set also varies.  How could I create a
> sequence that starts on specific date (say January 1, 2000 onwards)
> and continues until the end without specifying length?
>
>
> Sample data follows:
>
> df<-structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
>
> 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), out1 = c(0L,
>
> 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
>
> 0L, 1L, 0L, 0L, 0L, 1L)), .Names = c("id", "out1"), class =
> "data.frame", row.names = c(NA,
>
> -23L))
>
> __
> 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] write.table produces a file that read.table can't read

2014-10-08 Thread Neal H. Walfield
Hi,

I'm using R version 3.1.1 on Debian via the CRAN repositories.

Consider the following MWE that writes a data.frame out to a file and
reads it back in (note that one of the strings contains a double
quote):

  > write.table(data.frame(a=1:3, b=c("a", "b\"b", "c")), '/tmp/a', 
row.names=FALSE, na="?", sep=",")
  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',', na.strings='?', 
allowEscapes=T)
  [1] a b
  <0 rows> (or 0-length row.names)

/tmp/a contains the following:

  $ cat /tmp/a 
  "a","b"
  1,"a"
  2,"b\"b"
  3,"c"

Removing the double quote, it works:

  > write.table(data.frame(a=1:3, b=c("a", "bb", "c")), '/tmp/a', 
row.names=FALSE, na="?", sep=",")
  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',', na.strings='?', 
allowEscapes=T)
a  b
  1 1  a
  2 2 bb
  3 3  c

Why does allowEscapes not work for double quotes?  Or, why does
write.table produce a file that read.table can't read!

Thanks for any advice!

Neal

__
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] CoDA: Interpreting acomp() Summary Output

2014-10-08 Thread Rich Shepard

  Using package 'compositions' on count data and scaling with Aitchison
geometry using function acomp(), the summary() function displays matrices
for (among other descriptive statistics) minimum, Q1, median, Q3, and
maximum. These matrices are not symmetric because they are based on
log-ratios. An example is the median matrix:

$med
filter gather graze   predate shred
filter   1.000 0.09060847 0.708 0.183  1.416667
gather  11.417 1. 9.000 2.208 18.75
graze1.417 0. 1.000 0.2403846  1.75
predate  5.500 0.45299145 4.167 1.000  8.50
shred0.708 0.05341880 0.583 0.1180556  1.00

  What should I read to learn how to interpret this output?[1]

  I need to explain the results to non-technical decision-makers so they can
see (boxplot() does a great job of this) and understand the variability of
each data set.

Rich

[1] 'Analyzing Compositional Data with R', page 79, mentions these measures
of center and distribution but not how to interpret them.

__
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] an opinion question about State Space and Kalman Filters, please

2014-10-08 Thread Erin Hodgess
Hello!

Could anyone recommend a good book on state space/Kalman filters, please?
Those which include R steps are nice, but not necessary.  I'm most
interested in the state space/Kalman filters.

I would like to do a sort of compare and contrast with arima results.

Thanks,
Erin


-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] Obtain a list of data tables from a main data table

2014-10-08 Thread Frank S.
 

Hi everybody,

 

I have (as an example) the following
two data tables:

 

all <-
data.table(ID = c(rep(c(100:105),c(3,2,2,3,3,3))),

 value =
c(100,120,110,90,45,35,270,50,65,40,25,55,75,30,95,70))

DT <-
data.table(ID = 100:105, code=c(2,1,3,2,3,1))

 

My aim is to construct as many sub data tables as different values for
the integer variable code, and I have
done:

code_1
<- all[ID %in% DT[code==1]$ID] 

code_2
<- all[ID %in% DT[code==2]$ID] 

code_3
<- all[ID %in% DT[code==3]$ID] 

 

Because maximum code value can be very
high, �is it possible to obtain a list of the above 3 data tables through a
loop? I mean something like:

 

for (i in
1:max(DT$code)){

  paste(code,�_�,[i]) <- }

return(list)]

 

Thank
you very much to all the members!

  
[[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] Obtain a list of data tables from a main data table

2014-10-08 Thread Jeff Newmiller
Please be aware that by posting your question in HTML format instead of plain 
text you have sent us a corrupted message. Please adjust your email software 
settings when sending to this list.

I thought the whole purpose of data.table was to allow group data processing 
without creating oodles of separate tables.

What is your intended use of ask if these tables?
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On October 8, 2014 9:06:30 AM MDT, "Frank S."  wrote:
> 
>
>Hi everybody,
>
> 
>
>I have (as an example) the following
>two data tables:
>
> 
>
>all <-
>data.table(ID = c(rep(c(100:105),c(3,2,2,3,3,3))),
>
> value =
>c(100,120,110,90,45,35,270,50,65,40,25,55,75,30,95,70))
>
>DT <-
>data.table(ID = 100:105, code=c(2,1,3,2,3,1))
>
> 
>
>My aim is to construct as many sub data tables as different values for
>the integer variable code, and I have
>done:
>
>code_1
><- all[ID %in% DT[code==1]$ID] 
>
>code_2
><- all[ID %in% DT[code==2]$ID] 
>
>code_3
><- all[ID %in% DT[code==3]$ID] 
>
> 
>
>Because maximum code value can be very
>high, �is it possible to obtain a list of the above 3 data tables
>through a
>loop? I mean something like:
>
> 
>
>for (i in
>1:max(DT$code)){
>
>  paste(code,�_�,[i]) <- }
>
>return(list)]
>
> 
>
>Thank
>you very much to all the members!
>
> 
>   [[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.


[R] Desktop icon to run R and load/run specific package

2014-10-08 Thread Karim Mezhoud
Hi,
I'm developing R package with GUI.
I would write a file that run R and load package by double clic.
I am using Kubuntu.
If there is a solution with windows or mac, I am interested too.
Thanks
Karim

[[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] Desktop icon to run R and load/run specific package

2014-10-08 Thread ce
Did you try to create a file containing 

#!/usr/bin/Rscript

library(mypackage) 



you also need to give execute permission like chmod 755 myfile.R

-Original Message-
From: "Karim Mezhoud" [kmezh...@gmail.com]
Date: 10/08/2014 12:16 PM
To: R-help@r-project.org
Subject: [R] Desktop icon to run R and load/run specific package

Hi,
I'm developing R package with GUI.
I would write a file that run R and load package by double clic.
I am using Kubuntu.
If there is a solution with windows or mac, I am interested too.
Thanks
Karim

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


[R] Revolutions blog: September 2014 roundup

2014-10-08 Thread David Smith
Revolution Analytics staff and guests write about R every weekday at
the Revolutions blog:
 http://blog.revolutionanalytics.com
and every month I post a summary of articles from the previous month
of particular interest to readers of r-help.

In case you missed them, here are some articles related to R from the
month of September:

Norm Matloff argues that T-tests shouldn't be part of the Statistics
curriculum and questions the "star system" for p-values in R:
http://bit.ly/1rX82HF

A nice video introduction to the dplyr package and the %>% operator,
presented by Kevin Markham: http://bit.ly/1rX8566

An animation of police militarization in the US, created with R and
open data published by the New York Times: http://bit.ly/1rX8564

An overview of the miscellaneous R functions in the DescTools package:
http://bit.ly/1rX82HH

Some guidance from Will Stanton on becoming a "data hacker" using R
and Hadoop: http://bit.ly/1rX8565

A tutorial on publishing ggplot2 graphics to the web with plotly:
http://bit.ly/1rX82HG

A Shiny app that implements the Travelling Salesman problem and
animates the simulating annealing algorithm behind the solution:
http://bit.ly/1rX8569

R code for comparing performance of machine learning models:
http://bit.ly/1rX85D6

Presentations at DataWeek on applications of R at companies:
http://bit.ly/1rX82HI

Announcing new members for the R Foundation and the R Core team:
http://bit.ly/1rX8568

A graduate student uses R to look at the popularity of posts on
Reddit: http://bit.ly/1rX82HJ

Google introduces the CausalImpact package for R, and uses it to
evaluate performance of marketing campaigns: http://bit.ly/1rX8567

A review of several recent and upcoming conferences that include
R-related tracks: http://bit.ly/1rX856a

More presentations and video interview from the useR! 2014 conference,
from DataScience.LA: http://bit.ly/1rX85D9

A detailed Rcpp example based on the Collatz Conjecture: http://bit.ly/1rX85D7

Use Rmarkdown to create documents combining text, mathematics, and R
graphical and tabular output: http://bit.ly/1rX856b

A very early example of data analysis: Nile floods in 450 BC
http://bit.ly/1rX85D8

The Rockefeller Institute of Government uses R to simulate the
finances of public sector pension funds: http://bit.ly/1rX856e

General interest stories (not related to R) in the past month
included: ET for the Atari 2600 (http://bit.ly/1rX856c), Talk Like a
Pirate day photos (http://bit.ly/1rX856f), a parody lifestyle magazine
for data scientists (http://bit.ly/1rX85Db) and the spread of the Ice
Bucket Challenge (http://bit.ly/1rX85Da).

Meeting times for local R user groups
(http://blog.revolutionanalytics.com/local-r-groups.html) can be found
on the updated R Community Calendar at:
http://blog.revolutionanalytics.com/calendar.html

If you're looking for more articles about R, you can find summaries
from previous months at http://blog.revolutionanalytics.com/roundups/.
You can receive daily blog posts via email using services like
blogtrottr.com, or join the Revolution Analytics mailing list at
http://revolutionanalytics.com/newsletter to be alerted to new
articles on a monthly basis.

As always, thanks for the comments and please keep sending suggestions
to me at da...@revolutionanalytics.com or via Twitter (I'm
@revodavid).

Cheers,
# David

-- 
David M Smith 
Chief Community Officer, Revolution Analytics
http://blog.revolutionanalytics.com
Tel: +1 (650) 646-9523 (Chicago IL, USA)
Twitter: @revodavid

-- 
Try Revolution Enterprise R Now!  

Get a 14 Day Free Trial of Revolution R Enterprise on AWS Marketplace

__
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] lattice add a fit

2014-10-08 Thread David Winsemius

On Oct 7, 2014, at 9:15 PM, Duncan Mackay wrote:

I'm a tad puzzled by the comments about needing to build a panel function for 
locfit. The various plot.locfit functions are actually lattice calls. 

locfit:::panel.locfit  # already exists, and even has versions for 1d, 2d and 
3d purposes.

And there is a llines.locfit function that will add locfit smooths to existing 
lattice plots.

It's a very simple function and could easily be modified to any regression 
method that has a predict functions:

> locfit:::llines.locfit
function (x, m = 100, tr = x$trans, ...) 
{
newx <- lfmarg(x, m = m)[[1]]  # probably need to modify to your purposes
y <- predict(x, newx, tr = tr)
llines(newx, y, ...)
}


-- 
David

> 
> You will have to make your own panel function for locfit if you want to use
> it
> I have done it in the past - read the help for 
> library(locfit)
> ?plot.locfit 
> and the links
> ?lattice::prepanel
> 
> Regards
> 
> Duncan
> 
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> Armidale NSW 2351
> Email: home: mac...@northnet.com.au
> 
> 
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
> Behalf Of Bond, Stephen
> Sent: Tuesday, 7 October 2014 23:02
> To: r-help@R-project.org
> Subject: [R] lattice add a fit
> 
> What is the way to add an arbitrary fit from a model to a lattice
> conditioning plot ?
> 
> For example
> xyplot(v1 ~v2 | v3,data=mydata,
>panel=function(...){
>panel.xyplot(...)
>panel.loess(...,col.line="red")
>}
> )
> Will add a loess smoother. Instead, I want to put a fit from lm (but not a
> simple straight line) and the fit has to be done for each panel separately,
> not one fit for the full data set, so sth like an lm equivalent of
> panel.locfit (there is no panel.lmfit)
> Thank you.
> 
> Stephen B
> 
> 
>   [[alternative HTML version deleted]]
> 


David Winsemius
Alameda, CA, USA

__
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] write.table produces a file that read.table can't read

2014-10-08 Thread MacQueen, Don
How about:

tmp <- data.frame(a=1:3, b=c("a", "b\"b", "c"))

write.table(tmp, './tmp.write', row.names=FALSE, sep="\t², quote=FALSE)

tmp.in <- read.table('./tmp.write', sep='\t', head=TRUE, quote="")



> all.equal(tmp, tmp.in)
[1] TRUE



-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 10/8/14, 7:22 AM, "Neal H. Walfield"  wrote:

>Hi,
>
>I'm using R version 3.1.1 on Debian via the CRAN repositories.
>
>Consider the following MWE that writes a data.frame out to a file and
>reads it back in (note that one of the strings contains a double
>quote):
>
>  > write.table(data.frame(a=1:3, b=c("a", "b\"b", "c")), '/tmp/a',
>row.names=FALSE, na="?", sep=",")
>  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',',
>na.strings='?', allowEscapes=T)
>  [1] a b
>  <0 rows> (or 0-length row.names)
>
>/tmp/a contains the following:
>
>  $ cat /tmp/a 
>  "a","b"
>  1,"a"
>  2,"b\"b"
>  3,"c"
>
>Removing the double quote, it works:
>
>  > write.table(data.frame(a=1:3, b=c("a", "bb", "c")), '/tmp/a',
>row.names=FALSE, na="?", sep=",")
>  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',',
>na.strings='?', allowEscapes=T)
>a  b
>  1 1  a
>  2 2 bb
>  3 3  c
>
>Why does allowEscapes not work for double quotes?  Or, why does
>write.table produce a file that read.table can't read!
>
>Thanks for any advice!
>
>Neal
>
>__
>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] Obtain a list of data tables from a main data table

2014-10-08 Thread David Winsemius

On Oct 8, 2014, at 8:06 AM, Frank S. wrote:

> 
> 
> Hi everybody,
> 
> 
> 
> I have (as an example) the following
> two data tables:
> 
> 
> 
> all <-
> data.table(ID = c(rep(c(100:105),c(3,2,2,3,3,3))),
> 
> value =
> c(100,120,110,90,45,35,270,50,65,40,25,55,75,30,95,70))
> 
> DT <-
> data.table(ID = 100:105, code=c(2,1,3,2,3,1))
> 
> 
> 
> My aim is to construct as many sub data tables as different values for
> the integer variable code, and I have
> done:
> 
> code_1
> <- all[ID %in% DT[code==1]$ID] 
> 
> code_2
> <- all[ID %in% DT[code==2]$ID] 
> 
> code_3
> <- all[ID %in% DT[code==3]$ID] 
> 
> 
> 
> Because maximum code value can be very
> high, ¿is it possible to obtain a list of the above 3 data tables through a
> loop? I mean something like:
> 
> 
> 
> for (i in
> 1:max(DT$code)){
> 
>  paste(code,‚_‚,[i]) <- }
> 
> return(list)]

Why not use lapply (since it is designed to return a list) and call the subset 
function in the data.table package?

> 
> 
> 
> Thank
> you very much to all the members!
> 
> 
>   [[alternative HTML version deleted]]

This is a plain text mailing list.

> __
> 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
Alameda, CA, USA

__
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] lattice add a fit

2014-10-08 Thread Bond, Stephen
Folks,

This is just misunderstanding. I did not want a panel function for locfit. In 
my email I say:

 Instead, I want to put a fit from lm (but 
 not a simple straight line) and the fit has to be done for each panel 
 separately, not one fit for the full data set, so sth like an lm 
 equivalent of panel.locfit (there is no panel.lmfit) Thank you.

Bert Gunter provided the answer to my question. Maybe I should have sent a 
thank you note to the list to finalize.
Kind regards

Stephen Bond 


-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net] 
Sent: Wednesday, October 08, 2014 12:30 PM
To: Duncan Mackay
Cc: R; Bond, Stephen
Subject: Re: [R] lattice add a fit


On Oct 7, 2014, at 9:15 PM, Duncan Mackay wrote:

I'm a tad puzzled by the comments about needing to build a panel function for 
locfit. The various plot.locfit functions are actually lattice calls. 

locfit:::panel.locfit  # already exists, and even has versions for 1d, 2d and 
3d purposes.

And there is a llines.locfit function that will add locfit smooths to existing 
lattice plots.

It's a very simple function and could easily be modified to any regression 
method that has a predict functions:

> locfit:::llines.locfit
function (x, m = 100, tr = x$trans, ...) {
newx <- lfmarg(x, m = m)[[1]]  # probably need to modify to your purposes
y <- predict(x, newx, tr = tr)
llines(newx, y, ...)
}


--
David

> 
> You will have to make your own panel function for locfit if you want 
> to use it I have done it in the past - read the help for
> library(locfit)
> ?plot.locfit
> and the links
> ?lattice::prepanel
> 
> Regards
> 
> Duncan
> 
> Duncan Mackay
> Department of Agronomy and Soil Science University of New England 
> Armidale NSW 2351
> Email: home: mac...@northnet.com.au
> 
> 
> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Bond, Stephen
> Sent: Tuesday, 7 October 2014 23:02
> To: r-help@R-project.org
> Subject: [R] lattice add a fit
> 
> What is the way to add an arbitrary fit from a model to a lattice 
> conditioning plot ?
> 
> For example
> xyplot(v1 ~v2 | v3,data=mydata,
>panel=function(...){
>panel.xyplot(...)
>panel.loess(...,col.line="red")
>}
> )
> Will add a loess smoother. Instead, I want to put a fit from lm (but 
> not a simple straight line) and the fit has to be done for each panel 
> separately, not one fit for the full data set, so sth like an lm 
> equivalent of panel.locfit (there is no panel.lmfit) Thank you.
> 
> Stephen B
> 
> 
>   [[alternative HTML version deleted]]
> 


David Winsemius
Alameda, CA, USA

__
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] Data formart

2014-10-08 Thread Jeff Newmiller
This is like using the formula Area=3*r^2 for the area of a circle... you can 
figure out a way to do it but it is still wrong. Why not leave the data as day 
of year instead of misleading anyone who looks at it?
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On October 8, 2014 7:36:37 AM MDT, Frederic Ntirenganya  
wrote:
>Dear All,
>
>Change the format of the start and end columns so that data appear as
>the
>day of the year. For instance: Apr 24 rather than 115.
>
>The idea is that I want the non=leap years to be 366 days instead of
>being
>365 days. ie. Each year must have 366 days. for example: in the column
>Start2, Apr 18 will be Apr 17.
>
>> head(Samaru)
>  Year Start End Length Start2   End2
>1 1930   108 288180 Apr 18 Oct 15
>2 1931   118 288170 Apr 28 Oct 15
>3 1932   115 295180 Apr 24 Oct 21
>4 1933   156 294138 Jun 05 Oct 21
>5 1934   116 291175 Apr 26 Oct 18
>6 1935   134 288154 May 14 Oct 15
>
>Any idea is welcome. Thamks

__
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] Data formart

2014-10-08 Thread Amos B. Elberg
You can adapt:   format(as.Date(paste(Year, sep = "-", ifelse(Year %% 4 == 0, 
Start, ifelse(Start > 59, Start - 1, Start))), "%Y-%j"), "%b %d”)

But 60% of the dates in your data.frame will then be wrong.  


From: Frederic Ntirenganya 
Reply: Frederic Ntirenganya >
Date: October 8, 2014 at 9:38:34 AM
To: r-help@r-project.org >
Subject:  [R] Data formart  

Dear All,  

Change the format of the start and end columns so that data appear as the  
day of the year. For instance: Apr 24 rather than 115.  

The idea is that I want the non=leap years to be 366 days instead of being  
365 days. ie. Each year must have 366 days. for example: in the column  
Start2, Apr 18 will be Apr 17.  

> head(Samaru)  
Year Start End Length Start2 End2  
1 1930 108 288 180 Apr 18 Oct 15  
2 1931 118 288 170 Apr 28 Oct 15  
3 1932 115 295 180 Apr 24 Oct 21  
4 1933 156 294 138 Jun 05 Oct 21  
5 1934 116 291 175 Apr 26 Oct 18  
6 1935 134 288 154 May 14 Oct 15  

Any idea is welcome. Thamks  
--  
Frederic Ntirenganya  
Maseno University,  
Kenya.  
Mobile:(+254)718492836  
Email: fr...@aims.ac.za  
https://sites.google.com/a/aims.ac.za/fredo/  

[[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] Generate sequence of date based on a group ID

2014-10-08 Thread arun


If the `ids` are ordered as shown in the example, perhaps you need

   tbl <- table(df$id)
   
   rep(seq(as.Date("2000-01-01"), length.out=length(tbl), by=1), tbl)
[1] "2000-01-01" "2000-01-01" "2000-01-01" "2000-01-01" "2000-01-01"
[6] "2000-01-02" "2000-01-02" "2000-01-02" "2000-01-02" "2000-01-02"
[11] "2000-01-03" "2000-01-03" "2000-01-03" "2000-01-03" "2000-01-03"
[16] "2000-01-04" "2000-01-04" "2000-01-04" "2000-01-04" "2000-01-05"
[21] "2000-01-05" "2000-01-05" "2000-01-05"

A.K.


On Wednesday, October 8, 2014 3:57 AM, Kuma Raj  wrote:



I want to generate a sequence of date based on a group id(similar IDs
should have same date). The id variable contains unequal observations
and the length of the data set also varies.  How could I create a
sequence that starts on specific date (say January 1, 2000 onwards)
and continues until the end without specifying length?


Sample data follows:

df<-structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,

3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), out1 = c(0L,

0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,

0L, 1L, 0L, 0L, 0L, 1L)), .Names = c("id", "out1"), class =
"data.frame", row.names = c(NA,

-23L))

__
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] Evaluation of global variables in function definitions

2014-10-08 Thread H. Dieter Wilhelm
Duncan Murdoch  writes:

> On 07/10/2014 2:45 AM, H. Dieter Wilhelm wrote:
>> Hello (),
>>
>> I'd like to do the following
>>
>> a <- 3
>> f1 <- function (x){
>>   a*x^2
>> }
>>
>> a <- 5
>> f2 <- function (x){
>>   a*x^2
>> }
>>
>> plotting f1, f2, ...
>>
>> but f1 and f2 are the same, how can I evaluated the variables in the
>> function definition?
>>
>> Thank you
>>Dieter
> See the "open.account" example in section 10.7, "Scope", of the
> Introduction to R manual.

Sorry but above section teaches my how to change global variables in
functions.  But I would like to overcome the "lazy evaluation" of global
variables in functions.  Because I can't put expensive computations into
each function.

a <- complicated_a_computation(args1)
b <- complicated_b_computation(args1)
c <- ...
d
...

f1 <- function(x) a*x+b*exp(x)+c/x+...

a <- complicated_a_computation(args2)
b <- complicated_b_computation(args2)
c <- ...
d
...

f2 <- function(x) a*x+b*exp(x)+c/x +...

Yes in principle I could rename the global variables to a1, a2, b1, b2,
...  But I'd like to learn the principles of R.  Is above somehow
possible with the use of the substitute() command?

Thanks
-- 
Best wishes
H. Dieter Wilhelm
Darmstadt, Germany

__
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] reading in hexadecimal data - not working with data ending in E

2014-10-08 Thread mark . hogue
I am trying to read in data from an instrument that is recorded in 
hexadecimal format. Using either: 

y.hex <- read.table(file="hex.data", as.is=TRUE) 

or 

y.hex <- read.table(file="hex.data", text=TRUE) 

gets all the data in just fine except points like `055E` or `020E`. In 
these cases, the E is stripped so I get just 055 or 020. 

The question is how should this data be imported to avoid the E-ending 
problem? 

(By the way, my follow-up is to convert this data using, `y <- 
strtoi(y.hex, 16L)`) 

Thanks for any suggestions, 

Mark Hogue 
[[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] glm - rgamma vector as predictor

2014-10-08 Thread Wim Kreinen
 I have a strange question concerning the fit of a Gamma generalized linear
model with glm (and further using gamma.shape to measure the shape
parameter).

Actually, I started with rgamma to generate some random vectors because I
wanted to play around with various conditions (and become familiar with
gamma shape). But before you can start with gamma shape you need to have a
glm object.

Assuming

gamma.random <- rgamma (1000,1.5)

is my random vector.

How do I create a glm object if I only have one vector? I guess, my random
vector is the predictor and the response is the probability (in the sense
of a pdf). Can anybody give me a hint how the syntax is?

glm (? ~ gamma.random, family=Gamma)

Thanks Wim

[[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] Obtain a list of data tables from a main data table

2014-10-08 Thread Frank S.
Hi,
 
I think I got it!
 
The clue: Function split!
  
[[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] cbind in a loop...better way?

2014-10-08 Thread Evan Cooch
...or some such. I'm trying to work up a function wherein the user 
passes a list of matrices to the function, which then (1) takes each 
matrix, (2) performs an operation to 'vectorize' the matrix (i.e., given 
an (m x n) matrix x, this produces the vector Y of length  m*n that 
contains the columns of the matrix x, stacked below each other), and 
then (3) cbinds them together.


Here is an example using the case where I know how many matrices I need 
to cbind together. For this example, 2 square (3x3) matrices:


 a <- matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
 b <- matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

I want to vec them, and then cbind them together. So,

result  <- cbind(matrix(a,nr=9), matrix(b,nr=9))

which yields the following:

  [,1]  [,2]
 [1,]  0.00  0.00
 [2,]  0.05  0.15
 [3,]  0.00  0.00
 [4,] 20.00 15.00
 [5,]  0.00  0.00
 [6,]  0.10  0.20
 [7,] 50.00 45.00
 [8,]  0.00  0.00
 [9,]  0.00  0.00

Easy enough. But, I want to put it in a function, where the number and 
dimensions  of the matrices is not specified. Something like


Using matrices (a) and (b) from above, let

  env <- list(a,b).

Now, a function (or attempt at same) to perform the desired operations:

  vec=function(matlist) {

  n_mat=length(matlist);
  size_mat=dim(matlist[[1]])[1];

  result=cbind()

   for (i in 1:n_mat) {
 result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
  }

 return(result)

   }


When I run vec(env), I get the *right answer*, but I am wondering if 
there is a *better* way to get there from here than the approach I use 
(above). I'm not so much interested in 'computational efficiency' as I 
am in stability, and flexibility.


Thanks...

__
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] reading in hexadecimal data - not working with data ending in E

2014-10-08 Thread William Dunlap
I did not see a reproducible example, but you should use
colClasses="character" to
read in the data as character, then use something like as.hexmode() to
convert it to integers.  E.g.,

  > z <- read.table(text="ColA ColB\n1e 33\n2e  44\n10  5e\n",
header=TRUE, colClasses="character")
  > str(z)
  'data.frame':   3 obs. of  2 variables:
   $ ColA: chr  "1e" "2e" "10"
   $ ColB: chr  "33" "44" "5e"
  > z[] <- lapply(z, function(zi)as.integer(as.hexmode(zi)))
  > str(z)
  'data.frame':   3 obs. of  2 variables:
   $ ColA: int  30 46 16
   $ ColB: int  51 68 94

If you leave out the colClasses="character" then read.table will say
that all those
strings look like decimal numerals ("1e" being read as "1e+0", giving 1*10^0).


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, Oct 8, 2014 at 4:02 AM,   wrote:
> I am trying to read in data from an instrument that is recorded in
> hexadecimal format. Using either:
>
> y.hex <- read.table(file="hex.data", as.is=TRUE)
>
> or
>
> y.hex <- read.table(file="hex.data", text=TRUE)
>
> gets all the data in just fine except points like `055E` or `020E`. In
> these cases, the E is stripped so I get just 055 or 020.
>
> The question is how should this data be imported to avoid the E-ending
> problem?
>
> (By the way, my follow-up is to convert this data using, `y <-
> strtoi(y.hex, 16L)`)
>
> Thanks for any suggestions,
>
> Mark Hogue
> [[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] reading in hexadecimal data - not working with data ending in E

2014-10-08 Thread Duncan Murdoch

On 08/10/2014 7:02 AM, mark.ho...@srs.gov wrote:

I am trying to read in data from an instrument that is recorded in
hexadecimal format. Using either:

 y.hex <- read.table(file="hex.data", as.is=TRUE)

or

 y.hex <- read.table(file="hex.data", text=TRUE)

gets all the data in just fine except points like `055E` or `020E`. In
these cases, the E is stripped so I get just 055 or 020.


You need to read the ?read.table help.  as.is and text don't do anything 
like what you want.


The argument you need to use is colClasses.  I think you want to set it 
to "character".


Duncan Murdoch


The question is how should this data be imported to avoid the E-ending
problem?

(By the way, my follow-up is to convert this data using, `y <-
strtoi(y.hex, 16L)`)

Thanks for any suggestions,

Mark Hogue
[[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] Evaluation of global variables in function definitions

2014-10-08 Thread Duncan Murdoch

On 08/10/2014 5:53 AM, H. Dieter Wilhelm wrote:

Duncan Murdoch  writes:

> On 07/10/2014 2:45 AM, H. Dieter Wilhelm wrote:
>> Hello (),
>>
>> I'd like to do the following
>>
>> a <- 3
>> f1 <- function (x){
>>   a*x^2
>> }
>>
>> a <- 5
>> f2 <- function (x){
>>   a*x^2
>> }
>>
>> plotting f1, f2, ...
>>
>> but f1 and f2 are the same, how can I evaluated the variables in the
>> function definition?
>>
>> Thank you
>>Dieter
> See the "open.account" example in section 10.7, "Scope", of the
> Introduction to R manual.

Sorry but above section teaches my how to change global variables in
functions.


That's only part of what it tells you.  The open.account example binds 
values into the environment of the function.  That's what you want to 
do:  when you create f1, you want the values of a,b,c, etc. bound into 
its environment.  Then when you create f2, you want new values bound 
there.  So do it something like this:


makefun <- function(a,b,c,d) {
  force(a); force(b); force(c); force(d) # evaluate them now!
  function(x)  a*x+b*exp(x)+c/x+...
}

a <- complicated_a_computation(args1)
b <- complicated_b_computation(args1)
c <- ...
d
...

f1 <- makefun(a,b,c,d)

a <- complicated_a_computation(args2)
b <- complicated_b_computation(args2)
c <- ...
d
...

f2 <- makefun(a,b,c,d)

Duncan Murdoch


  But I would like to overcome the "lazy evaluation" of global
variables in functions.  Because I can't put expensive computations into
each function.

a <- complicated_a_computation(args1)
b <- complicated_b_computation(args1)
c <- ...
d
...

f1 <- function(x) a*x+b*exp(x)+c/x+...

a <- complicated_a_computation(args2)
b <- complicated_b_computation(args2)
c <- ...
d
...

f2 <- function(x) a*x+b*exp(x)+c/x +...

Yes in principle I could rename the global variables to a1, a2, b1, b2,
...  But I'd like to learn the principles of R.  Is above somehow
possible with the use of the substitute() command?

Thanks


__
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] reading in hexadecimal data - not working with data ending in E

2014-10-08 Thread John McKown
On Wed, Oct 8, 2014 at 6:02 AM,  wrote:

> I am trying to read in data from an instrument that is recorded in
> hexadecimal format. Using either:
>
> y.hex <- read.table(file="hex.data", as.is=TRUE)
>
> or
>
> y.hex <- read.table(file="hex.data", text=TRUE)
>
> gets all the data in just fine except points like `055E` or `020E`. In
> these cases, the E is stripped so I get just 055 or 020.
>
> The question is how should this data be imported to avoid the E-ending
> problem?
>
> (By the way, my follow-up is to convert this data using, `y <-
> strtoi(y.hex, 16L)`)
>
> Thanks for any suggestions,
>

​Please don't post in HTML, it is against forum policy. And it often
results in poorly formatted messages, which many ignore.​

​Try:

y.hex <- read.table(file="hex.data",as.is=TRUE,colClasses="character");​



>
> Mark Hogue
>


-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

[[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] Evaluation of global variables in function definitions

2014-10-08 Thread William Dunlap
fFactory <- function(args) {
a <- complicated_a_computation(args)
b <- complicated_b_computation(args)
function(x) a*x + b*exp(x)
}
f1 <- fFactory(args1)
f2 <- fFactory(args2)

f1(x)
f2(x) # will get different result than f1(x)

f1 and f2 will look the same but produce different results because
environment(f1)
contains different values of a and b than environment(f2).

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, Oct 8, 2014 at 2:53 AM, H. Dieter Wilhelm
 wrote:
> Duncan Murdoch  writes:
>
>> On 07/10/2014 2:45 AM, H. Dieter Wilhelm wrote:
>>> Hello (),
>>>
>>> I'd like to do the following
>>>
>>> a <- 3
>>> f1 <- function (x){
>>>   a*x^2
>>> }
>>>
>>> a <- 5
>>> f2 <- function (x){
>>>   a*x^2
>>> }
>>>
>>> plotting f1, f2, ...
>>>
>>> but f1 and f2 are the same, how can I evaluated the variables in the
>>> function definition?
>>>
>>> Thank you
>>>Dieter
>> See the "open.account" example in section 10.7, "Scope", of the
>> Introduction to R manual.
>
> Sorry but above section teaches my how to change global variables in
> functions.  But I would like to overcome the "lazy evaluation" of global
> variables in functions.  Because I can't put expensive computations into
> each function.
>
> a <- complicated_a_computation(args1)
> b <- complicated_b_computation(args1)
> c <- ...
> d
> ...
>
> f1 <- function(x) a*x+b*exp(x)+c/x+...
>
> a <- complicated_a_computation(args2)
> b <- complicated_b_computation(args2)
> c <- ...
> d
> ...
>
> f2 <- function(x) a*x+b*exp(x)+c/x +...
>
> Yes in principle I could rename the global variables to a1, a2, b1, b2,
> ...  But I'd like to learn the principles of R.  Is above somehow
> possible with the use of the substitute() command?
>
> Thanks
> --
> Best wishes
> H. Dieter Wilhelm
> Darmstadt, Germany
>
> __
> 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] cbind in a loop...better way?

2014-10-08 Thread David L Carlson
How about

> do.call(cbind, lapply(env, as.vector))
   [,1]  [,2]
 [1,]  0.00  0.00
 [2,]  0.05  0.15
 [3,]  0.00  0.00
 [4,] 20.00 15.00
 [5,]  0.00  0.00
 [6,]  0.10  0.20
 [7,] 50.00 45.00
 [8,]  0.00  0.00
 [9,]  0.00  0.00

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Evan Cooch
Sent: Wednesday, October 8, 2014 2:13 PM
To: r-help@r-project.org
Subject: [R] cbind in a loop...better way?

...or some such. I'm trying to work up a function wherein the user 
passes a list of matrices to the function, which then (1) takes each 
matrix, (2) performs an operation to 'vectorize' the matrix (i.e., given 
an (m x n) matrix x, this produces the vector Y of length  m*n that 
contains the columns of the matrix x, stacked below each other), and 
then (3) cbinds them together.

Here is an example using the case where I know how many matrices I need 
to cbind together. For this example, 2 square (3x3) matrices:

  a <- matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
  b <- matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

I want to vec them, and then cbind them together. So,

result  <- cbind(matrix(a,nr=9), matrix(b,nr=9))

which yields the following:

   [,1]  [,2]
  [1,]  0.00  0.00
  [2,]  0.05  0.15
  [3,]  0.00  0.00
  [4,] 20.00 15.00
  [5,]  0.00  0.00
  [6,]  0.10  0.20
  [7,] 50.00 45.00
  [8,]  0.00  0.00
  [9,]  0.00  0.00

Easy enough. But, I want to put it in a function, where the number and 
dimensions  of the matrices is not specified. Something like

Using matrices (a) and (b) from above, let

   env <- list(a,b).

Now, a function (or attempt at same) to perform the desired operations:

   vec=function(matlist) {

   n_mat=length(matlist);
   size_mat=dim(matlist[[1]])[1];

   result=cbind()

for (i in 1:n_mat) {
  result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
   }

  return(result)

}


When I run vec(env), I get the *right answer*, but I am wondering if 
there is a *better* way to get there from here than the approach I use 
(above). I'm not so much interested in 'computational efficiency' as I 
am in stability, and flexibility.

Thanks...

__
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] glm - rgamma vector as predictor

2014-10-08 Thread Ben Bolker
Wim Kreinen  gmail.com> writes:

>

[snip]

> 
> Actually, I started with rgamma to generate some random vectors because I
> wanted to play around with various conditions (and become familiar with
> gamma shape). But before you can start with gamma shape you need to have a
> glm object.
> 
> Assuming
> 
> gamma.random <- rgamma (1000,1.5)
> 
> is my random vector.
> 
> How do I create a glm object if I only have one vector? I guess, my random
> vector is the predictor and the response is the probability (in the sense
> of a pdf). Can anybody give me a hint how the syntax is?

 [snip]

I think you're looking for

  MASS::gamma.shape(glm(gamma.random ~ 1, family=Gamma))

__
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] ADF test

2014-10-08 Thread Midea Algaf
Hi 
I need to know how can I do dicky-fuller test in R
Regards
Mideast

‏‫من جهاز الـ iPad الخاص بي‬
[[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] ADF test

2014-10-08 Thread Rui Barradas

Hello,

Try package tseries, function adf.test().

Hope this helps,

Rui Barradas

Em 08-10-2014 20:47, Midea Algaf escreveu:

Hi
I need to know how can I do dicky-fuller test in R
Regards
Mideast

‏‫من جهاز الـ iPad الخاص بي‬
[[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] lattice add a fit

2014-10-08 Thread Duncan Mackay
Hi

Then try

xyplot(... , type = c("p","r"))

Have a look at

? lattice::panel.xyplot for full type explanation 

I cannot remember what Bert wrote. Your mention of smoothers and locfit can
be quite a different story 

Duncan



-Original Message-
From: Bond, Stephen [mailto:stephen.b...@cibc.com] 
Sent: Thursday, 9 October 2014 04:15
To: David Winsemius; Duncan Mackay
Cc: R
Subject: RE: [R] lattice add a fit

Folks,

This is just misunderstanding. I did not want a panel function for locfit.
In my email I say:

 Instead, I want to put a fit from lm (but 
 not a simple straight line) and the fit has to be done for each panel 
 separately, not one fit for the full data set, so sth like an lm 
 equivalent of panel.locfit (there is no panel.lmfit) Thank you.

Bert Gunter provided the answer to my question. Maybe I should have sent a
thank you note to the list to finalize.
Kind regards

Stephen Bond 


-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net] 
Sent: Wednesday, October 08, 2014 12:30 PM
To: Duncan Mackay
Cc: R; Bond, Stephen
Subject: Re: [R] lattice add a fit


On Oct 7, 2014, at 9:15 PM, Duncan Mackay wrote:

I'm a tad puzzled by the comments about needing to build a panel function
for locfit. The various plot.locfit functions are actually lattice calls. 

locfit:::panel.locfit  # already exists, and even has versions for 1d, 2d
and 3d purposes.

And there is a llines.locfit function that will add locfit smooths to
existing lattice plots.

It's a very simple function and could easily be modified to any regression
method that has a predict functions:

> locfit:::llines.locfit
function (x, m = 100, tr = x$trans, ...) {
newx <- lfmarg(x, m = m)[[1]]  # probably need to modify to your
purposes
y <- predict(x, newx, tr = tr)
llines(newx, y, ...)
}


--
David

> 
> You will have to make your own panel function for locfit if you want 
> to use it I have done it in the past - read the help for
> library(locfit)
> ?plot.locfit
> and the links
> ?lattice::prepanel
> 
> Regards
> 
> Duncan
> 
> Duncan Mackay
> Department of Agronomy and Soil Science University of New England 
> Armidale NSW 2351
> Email: home: mac...@northnet.com.au
> 
> 
> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Bond, Stephen
> Sent: Tuesday, 7 October 2014 23:02
> To: r-help@R-project.org
> Subject: [R] lattice add a fit
> 
> What is the way to add an arbitrary fit from a model to a lattice 
> conditioning plot ?
> 
> For example
> xyplot(v1 ~v2 | v3,data=mydata,
>panel=function(...){
>panel.xyplot(...)
>panel.loess(...,col.line="red")
>}
> )
> Will add a loess smoother. Instead, I want to put a fit from lm (but 
> not a simple straight line) and the fit has to be done for each panel 
> separately, not one fit for the full data set, so sth like an lm 
> equivalent of panel.locfit (there is no panel.lmfit) Thank you.
> 
> Stephen B
> 
> 
>   [[alternative HTML version deleted]]
> 


David Winsemius
Alameda, CA, USA

__
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] par("plt") behaving inconsistely? bug?

2014-10-08 Thread Paul Murrell

Hi

The canonical poison is this ...

par(plt)
plot.new()
...
par(plt)
par(new=TRUE)
plot.new()
...
par(plt)
par(new=TRUE)
plot.new()

The idea is to set up parameters that control the placement of a plot, 
e.g., par(plt), and then start a plot, with plot.new().  If you want 
more than one plot on a page (and par(mfrow) does not suffice) then you 
set up new placement parameters, tell R not to start a new page, with 
par(new=TRUE), then start another plot, with plot.new().


Your code does not follow the "spirit" of the R graphics model because 
it starts a plot with one set of placement parameters and then changes 
those placement parameters several times within the same plot (instead 
of starting a new plot for each new set of placement parameters).


I had a look at the C code that leads to your "surprising" result and 
there is at least one infelicity in there, but I could not see a simple 
fix that would make your example "work" without a high risk of breaking 
other things.  So I'm afraid my best advice is to change your code to 
work with the graphics system.


The layout() function might provide a less unpleasant approach to having 
more than one plot region on the page, depending on how complex your 
arrangement of plot regions is.


Another alternative is to use the 'grid' graphics package, which is 
designed to allow for the flexible creation of multiple regions, 
depending on what you want to draw in each of those regions.


Paul

On 10/07/14 15:33, Murat Tasan wrote:

6. iteratively downgrade to earlier versions of R until it's working
again... then try to diff out the offending source code change.
i can try this, but i probably won't get to it for at least a few weeks :-/

in the meantime, i'm tacking on box(lty = 0) to every par(plt = ...) call, e.g.

par("plt" = some_plt_coordinates); box(lty = 0)


in the short term, this works.
clip(...), a combination of par("new" = TRUE); plot.new(), and a whole
bunch of other kludges work, too... pick your poison :-)

cheers and thanks!

-murat

On Mon, Oct 6, 2014 at 2:08 PM, Greg Snow <538...@gmail.com> wrote:

I believe that what is happening is that the clipping region is being
reset when you call box, but not when you call rect.  If you insert
the command "par(xpd=NA)" (or TRUE instead of NA) after the plot.new
and use the rect commands then you can see both rectangles (because
this turns the clipping off).  Working with the clipping region
(indirectly in your case) is complex since some functions properly
reset the region and others do not (and  making the others
automatically reset it may cause other problems when they reset a
clipping region that should not be reset).

So the options are:

1 dive into the source code enough to figure out if fixing rect to
work with the clipping region is simple or not and submitting a patch
2 wait for Prof Brian Ripley to notice this fact and do the above (he
has fixed a couple of other functions when made aware)
3 use par(xpd=TRUE) (or NA) to not clip to the plotting region (this
is simple, but will allow things to be drawn outside of the plotting
region, on simple example is using abline)
4 use a function that properly sets the clipping region (such as box)
before plotting anything else
5 call clip(0,1,0,1) (or with the actual user coordinates) to manually
set the clipping region
6 other?


On Mon, Oct 6, 2014 at 12:00 PM, Murat Tasan  wrote:

Hi all -- I just encountered a behavior that I believe has changed
from previous versions, though I haven't chased back the last version
that behaves as my existing code expects quite yet.
Perhaps this is a bug, though perhaps I'm missing a subtle detail
somewhere in the documentation...

Here's some code that works as expected (in R 3.1.1):


pdf()
plot.new()

original_plt <- par("plt")

plt_1 <- c(original_plt[1],
original_plt[1] + (original_plt[2] - original_plt[1]) / 2,
original_plt[3],
original_plt[3] + (original_plt[4] - original_plt[3]) / 2)
par("plt" = plt_1)
plot.window(xlim = c(0, 1), ylim = c(0, 1))
box()
plt_2 <- c(plt_1[2],
original_plt[2],
plt_1[4],
original_plt[4])
par("plt" = plt_2)
plot.window(xlim = c(0, 1), ylim = c(0, 1))
box()
par("plt" = original_plt)
box(lty = 2)
dev.off()


This will draw 3 boxes... one in the lower left corner (specified by
plt_1), one in the top right corner (specified by plt_2), and one
dotted box around the full plot box (original_plt).

Now, if you replace the first two box() calls by: rect(0, 0, 1, 1),
only the lower-left rectangle is drawn.
If you _add_ rect(0, 0, 1, 1) after each box() call, all boxes and
rectangles are correctly drawn.

It seems that after setting plt once, subsequent plt alterations put
the device into a state that will permits drawing of _some_ things
(e.g. box()), but not other things (e.g. rect, lines, points).

A kludge to fix this is to call box(co

Re: [R] cbind in a loop...better way?

2014-10-08 Thread Evan Cooch
That works as well. I'll collate your response and a couple of others, 
and post tomorrow.

On 10/8/2014 4:17 PM, David L Carlson wrote:
> How about
>
>> do.call(cbind, lapply(env, as.vector))
> [,1]  [,2]
>   [1,]  0.00  0.00
>   [2,]  0.05  0.15
>   [3,]  0.00  0.00
>   [4,] 20.00 15.00
>   [5,]  0.00  0.00
>   [6,]  0.10  0.20
>   [7,] 50.00 45.00
>   [8,]  0.00  0.00
>   [9,]  0.00  0.00
>
> -
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf Of Evan Cooch
> Sent: Wednesday, October 8, 2014 2:13 PM
> To: r-help@r-project.org
> Subject: [R] cbind in a loop...better way?
>
> ...or some such. I'm trying to work up a function wherein the user
> passes a list of matrices to the function, which then (1) takes each
> matrix, (2) performs an operation to 'vectorize' the matrix (i.e., given
> an (m x n) matrix x, this produces the vector Y of length  m*n that
> contains the columns of the matrix x, stacked below each other), and
> then (3) cbinds them together.
>
> Here is an example using the case where I know how many matrices I need
> to cbind together. For this example, 2 square (3x3) matrices:
>
>a <- matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
>b <- matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)
>
> I want to vec them, and then cbind them together. So,
>
> result  <- cbind(matrix(a,nr=9), matrix(b,nr=9))
>
> which yields the following:
>
> [,1]  [,2]
>[1,]  0.00  0.00
>[2,]  0.05  0.15
>[3,]  0.00  0.00
>[4,] 20.00 15.00
>[5,]  0.00  0.00
>[6,]  0.10  0.20
>[7,] 50.00 45.00
>[8,]  0.00  0.00
>[9,]  0.00  0.00
>
> Easy enough. But, I want to put it in a function, where the number and
> dimensions  of the matrices is not specified. Something like
>
> Using matrices (a) and (b) from above, let
>
> env <- list(a,b).
>
> Now, a function (or attempt at same) to perform the desired operations:
>
> vec=function(matlist) {
>
> n_mat=length(matlist);
> size_mat=dim(matlist[[1]])[1];
>
> result=cbind()
>
>  for (i in 1:n_mat) {
>result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
> }
>
>return(result)
>
>  }
>
>
> When I run vec(env), I get the *right answer*, but I am wondering if
> there is a *better* way to get there from here than the approach I use
> (above). I'm not so much interested in 'computational efficiency' as I
> am in stability, and flexibility.
>
> Thanks...
>
> __
> 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] How can I overwrite a method in R?

2014-10-08 Thread Tim Hesterberg
How can I create an improved version of a method in R, and have it be used?

Short version:
I think plot.histogram has a bug, and I'd like to try a version with a fix.
But when I call hist(), my fixed version doesn't get used.

Long version:
hist() calls plot() which calls plot.histogram() which fails to pass ...
when it calls plot.window().
As a result hist() ignores xaxs and yaxs arguments.
I'd like to make my own copy of plot.histogram that passes ... to
plot.window().

If I just make my own copy of plot.histogram, plot() ignores it, because my
version is not part of the same graphics package that plot belongs to.

If I copy hist, hist.default and plot, the copies inherit the same
environments as
the originals, and behave the same.

If I also change the environment of each to .GlobalEnv, hist.default fails
in
a .Call because it cannot find C_BinCount.

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