Re: [R] How to specify a data frame column using command line arg?

2016-06-05 Thread Jim Lemon
Yes, I see what you want. I can't run this myself as my work computer
runs Windows and insists on starting Statistica when I run an R file
in batch mode. What about:

plot(adl1[,args[1]],adl1[,args[2]])

I just noticed that you are plotting in ggplot, so this won't help. Maybe:

aes(x=args[1], y=args[2])

?

Jim


On Mon, Jun 6, 2016 at 1:29 PM, Douglas Johnson  wrote:
> Hi Jim,
>
> Thanks for the quick reply. I was trying to be clear but I think maybe the
> example was too simple. What I really have is a csv file with lots of
> columns (let's say 26) with a header (say a,b,c,d,e,z). I want to
> generate lots of scatter plots -- 'a' vs. 'b' and 'd' vs 'j' and so on and
> so on. I don't want to create a separate program for every combination of
> columns -- that would be hundreds of programs. I just want one program that
> takes three args --
> 1) the csv file name
> 2) the name of the 'x-axis' column (e.g. 'd')
> 3) the name of the 'y-asix' column (e.g. 'j')
>
> so can run:
>
> scatter_plot.r sample01.csv a b
> scatter_plot.r sample01.csv d j
> .
>
> I can't figure out how to get  the column names from the args[] to the
> aes(x=?, y=?).
> There must be some kind of indirect  reference or eval() or substitute()
> operator in R but I can't find it.
>
> Anyway, thanks for taking a shot at this.
>
> Best,
>
> doug
>
>
>
>
> On Sun, Jun 5, 2016 at 10:44 PM, Jim Lemon  wrote:
>>
>> Hi Doug,
>> I think this will work for you:
>>
>> adl1<-read.csv("test.csv")
>> adl1[,"a"]
>> [1] 1 4 7
>>
>> so, adl1[,args[1]] should get you the column that you pass as the
>> first argument.
>>
>> Jim
>>
>>
>> On Mon, Jun 6, 2016 at 5:45 AM, Douglas Johnson  wrote:
>> > I'm guessing this is trivial but I've spent two hours searching and
>> > reading
>> > FAQ, tutorial, introductory and 'idiom' documents and haven't found a
>> > solution. All I want to do is select a data frame column at run time
>> > using
>> > a command line arg. For example, the "test.csv" file contains:
>> >
>> > a,b,c
>> > 1,2,3
>> > 4,5,6
>> > 7,8,9
>> >
>> > If I run "test.r a", I get [1,4,7].
>> > If I run "test.r b", I get [2,5,8].
>> >
>> > Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or
>> > 'c'?
>> >
>> > args <- commandArgs(trailingOnly=T)
>> >
>> > adl1<-read.csv(file="test.csv",head=T,sep=",")
>> >
>> > adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
>> >
>> >
>> > All I'm really trying to do is generate a bunch of scatter plots of
>> > column
>> > 'x' vs. 'y' without writing a separate program for each pair.
>> >
>> >
>> > Thanks,
>> >
>> >
>> > Doug
>> >
>> >
>> >
>> > --
>> > http://www.dojopico.org 
>> >
>> > [[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.
>
>
>
>
> --
> http://www.dojopico.org

__
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] detecting if a variable has changed

2016-06-05 Thread Duncan Murdoch

On 05/06/2016 2:13 PM, Bert Gunter wrote:

Nope, Ted. I asked for  a O(log(n)) solution, not an O(n) one.


I don't think that's possible with a numeric vector.  Inserting an entry 
at a random location is an O(n) operation, since you need to move all 
following values out of the way.


Duncan Murdoch



I will check out the data.table package, as suggested.

-- Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Jun 5, 2016 at 11:01 AM, Ted Harding  wrote:

Surely it is straightforward, since the vector (say 'X') is already sorted?

Example (raw code with explicit example):

set.seed(54321)
X <- sort(runif(10))
# X ## The initial sorted vector
# [1] 0.04941009 0.17669234 0.20913493 0.21651016 0.27439354
# [6] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110

y <- runif(1)
# y ## The new value to be inserted
[1] 0.1366424

Y <- c(X[X<=y],y,X[X>y]) ## Now insert y into X:
Y
[1] 0.04941009 0.13664239 0.17669234 0.20913493 0.21651016 0.27439354
[7] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110

## And there it is at Y[2]

Easy to make such a function!
Best wishes to all,
Ted.

On 05-Jun-2016 17:44:29 Neal H. Walfield wrote:

On Sun, 05 Jun 2016 19:34:38 +0200,
Bert Gunter wrote:

This help thread suggested a question to me:

Is there a function in some package that efficiently (I.e. O(log(n)) )
inserts a single new element into the correct location in an
already-sorted vector? My assumption here is that doing it via sort()
is inefficient, but maybe that is incorrect. Please correct me if so.


I think data.table will do this if the the column is marked
appropriately.


I realize that it would be straightforward to write such a function,
but I just wondered if it already exists. My google & rseek searches
did not succeed, but maybe I used the wrong keywords.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
 wrote:

I don't know what you mean by "without having to use any special
interfaces", but "reference classes" will do what I think you want.  E.g.,
the following makes a class called 'SortedNumeric' that only sorts the
vector when you want to get its value, not when you append values.  It
stores the sorted vector so it does not get resorted each time you ask for
it.

SortedNumeric <- setRefClass("sortedNumeric",
fields = list(
fData = "numeric",
fIsUnsorted = "logical"),
methods = list(
initialize = function(Data = numeric(), isUnsorted = TRUE)
{
fData <<- Data
stopifnot(is.logical(isUnsorted),
  length(isUnsorted)==1,
  !is.na(isUnsorted))
fIsUnsorted <<- isUnsorted
},
getData = function() {
if (isUnsorted) {
fData <<- sort(fData)
fIsUnsorted <<- FALSE
}
fData
},
appendData = function(newEntries) {
fData <<- c(fData, newEntries)
fIsUnsorted <<- TRUE
}
))

Use it as:


x <- SortedNumeric$new()
x$appendData(c(4,2,5))
x$appendData(c(1,8,9))
x

Reference class object of class "sortedNumeric"
Field "fData":
[1] 4 2 5 1 8 9
Field "fIsUnsorted":
[1] TRUE

x$getData()

[1] 1 2 4 5 8 9

x

Reference class object of class "sortedNumeric"
Field "fData":
[1] 1 2 4 5 8 9
Field "fIsUnsorted":
[1] FALSE


Outside of base R, I think the R6 package gives another approach to this.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield 
wrote:


Hi,

I have a huge list.  Normally it is sorted, but I want to be able to
add elements to it without having to use any special interfaces and
then sort it on demand.  My idea is to use something like weak
references combined with attributes.  Consider:

  # Initialization.
  l = as.list(1:10)
  # Note that it is sorted.
  attr(l, 'sorted') = weakref(l)

  # Modify the list.
  l = append(l, 1:3)

  # Check if the list is still sorted.  (I use identical here, but it
  # probably too heavy weight: I just need to compare the addresses.)
  if (! identical(l, attr(l, 'sorted'))) {
l = sort(unlist(l))
attr(l, 'sorted') = weakref(l)
  }
  # Do operation that requires sorted list.
  ...

This is obviously a toy example.  I'm not actually sorting integers
and I may use a matrix instead of a list.

I've read:

  http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
  

Re: [R] How to specify a data frame column using command line arg?

2016-06-05 Thread Jim Lemon
Hi Doug,
I think this will work for you:

adl1<-read.csv("test.csv")
adl1[,"a"]
[1] 1 4 7

so, adl1[,args[1]] should get you the column that you pass as the
first argument.

Jim


On Mon, Jun 6, 2016 at 5:45 AM, Douglas Johnson  wrote:
> I'm guessing this is trivial but I've spent two hours searching and reading
> FAQ, tutorial, introductory and 'idiom' documents and haven't found a
> solution. All I want to do is select a data frame column at run time using
> a command line arg. For example, the "test.csv" file contains:
>
> a,b,c
> 1,2,3
> 4,5,6
> 7,8,9
>
> If I run "test.r a", I get [1,4,7].
> If I run "test.r b", I get [2,5,8].
>
> Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or 'c'?
>
> args <- commandArgs(trailingOnly=T)
>
> adl1<-read.csv(file="test.csv",head=T,sep=",")
>
> adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
>
>
> All I'm really trying to do is generate a bunch of scatter plots of column
> 'x' vs. 'y' without writing a separate program for each pair.
>
>
> Thanks,
>
>
> Doug
>
>
>
> --
> http://www.dojopico.org 
>
> [[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.

__
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] find longest consecutive streak in double

2016-06-05 Thread Jim Lemon
Hi Nick,
I think you want to get the maximum run length:

jja<-runif(92,0,16)
med_jja<-median(jja)
med_jja
[1] 7.428935
# get a logical vector of values greater than the median
jja_plus_med<-jja > med_jja
# now get the length of runs
runs_jja_plus_med<-rle(jja_plus_med)
# finally find the maximum run length
max(runs_jja_plus_med$lengths)
[1] 5

Jim


On Mon, Jun 6, 2016 at 5:51 AM, Nick Tulli  wrote:
> Hey guys. Learning R after gaining a background in Python this year,
> and I'm translating my Python projects to R now. This is the first
> time I'm posting to the mailing list.
>
> Essentially, I have 92 data points in one double that I created from a
> netcdf file. Those 92 data points represent a measurement from a
> location over the course of three months. From there I've calculated
> the median value of the data, which is 8.534281. My goal here is to
> test each data point to see if it exceeds the median value, and, next,
> calculate the longest streak of days in the 92-day span in which the
> data exceeded the median value.
>
> To achieve this in Python, I've created the following function, where
> q is a vector of length 92 and q_JJA_median is, of course, the median
> value of the dataset.
> ##
> def consecutive(q, q_JJA_median):
> is_consecutive = 0
> n = 0
> array_exceed = []
> for i in range(len(q)):
> if q[i] > q_JJA_median:
> n+=1
> is_consecutive = 1
> else:
> if is_consecutive:
> array_exceed.append(n)
> n = 0
> is_consecutive = 0
> if q[i] > q_JJA_median:
> array_exceed.append(n)
> if len(array_exceed) == 0:
> array_exceed = 0
> if type(array_exceed) is int:
> array_exceed = [0]
> return array_exceed
> ###
>
> Here is my work thus far written for R:
> ###
> is_consecutive = 0
> n = 0
> array_exceed <- c()
> for (i in q) {
>   if (i > q_JJA_median) {
> n = n + 1
> is_consecutive = is_consecutive + 1
>   }
>   else {
> if (is_consecutive) {
>   append(array_exceed, n)
>   n <- 0
>   is_consecutive <- 0
> }
>   }
>   if (i > q_JJA_median) {
> append(array_exceed,n)
>   }
> }
> ###
>
> My code written for R has been changed and manipulated many times, and
> none of my attempts have been successful. I'm still new to the syntax
> of the R language, so my problem very well could be a product of my
> lack of experience.
>
> Additionally, I read on a forum post that using the append function
> can be slower than many other options. Is this true? If so, how can I
> circumvent that issue?
>
> Thank you!
>
> __
> 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.


Re: [R] Reading and converting time data via read.table

2016-06-05 Thread Ek Esawi
Thanks Jim!



The problem is not date data. The problem is time. I figured out that I can
use chron library and then use this expression times(paste0(t2, ":00"))
because my time data are not h:m:s, only h;m.

The problem I am trying to figure out is how to implement [times(paste0(t2,
":00"))] on colclasses so that the whole table is converted to correct time
format which later can be manipulated


Thanks again--EK.

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


[R] Estimating gumbel copula parameter

2016-06-05 Thread Sachin Kuruvithadam
Hi all. I'm a R newbie, so please bear with me.

I'm fitting nine log losses series (log returns multiplied by -1, so that I 
have losses on the right and returns on the left) using copulas. My project in 
more detail:
1) I've filtered the residuals using univariate ARMA-GARCH models, and 
standardized them dividing by sigma, so that now I have standardized residuals 
with mean 0 and variance 1 (rugarch package).
2) I modeled the residuals using the generalized pareto distribution family, 
for all nine series I'm using upper and lower thresholds equal to 0.95 and 0.05 
quantiles respectively (spd package).
3) After turning the standardized residuals to their uniform margins using 
pspd, I'm fitting a Gumbel copula which has upper tail dependence since I'm 
focusing on losses which are on the right (copula package).
Next part of the work: i) I want to estimate a time-varying Gumbel parameter; 
ii) given predicted parameter, simulate copula realizations; iii) using the 
inverse marginal distributions, turn these realizations into standardized 
residuals; iv) compute returns using ARMA-GARCH specification, convert into 
simple returns, compute return of equally weighted portfolio and 
estimate/backtest value at risk and expected shortfall.

Being done with first 2 steps, now I want to estimate a time varying Gumbel. So 
I'm trying to extract the parameter with a moving window of 500 observations to 
get an idea of how the time series looks like.

My code looks like this:
theta <- c()
for (i in 1:3345)
{
  theta[i]=summary(fitCopula(gumbel.cop,standardized[i:(i+499),], 
method="ml"))$coefficients[1]
}

where standardized is a matrix with 9 columns, each with the uniform margin 
between 0 and 1. So I want to estimate 3345 theta using a window of 500 
observations, from theta_1 (obs. 1 to 500) to theta_3345 (obs. 3345 to 4344). 
However, while running this code, I get:

Error in optim(start, loglikCopula, lower = lower, upper = upper, method = 
method,  :
  valore iniziale in 'vmmin' non finito

where the last sentence can be translated with "initial value in "vmmin" not 
finite".

What is this error due to? How can I fix this in the command? It runs for the 
first 1339 estimates (which I've obtained), but from i=1340 this message shows 
up and I have NA as estimated value.


Any help is appreciated. Thanks.

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


[R] find longest consecutive streak in double

2016-06-05 Thread Nick Tulli
Hey guys. Learning R after gaining a background in Python this year,
and I'm translating my Python projects to R now. This is the first
time I'm posting to the mailing list.

Essentially, I have 92 data points in one double that I created from a
netcdf file. Those 92 data points represent a measurement from a
location over the course of three months. From there I've calculated
the median value of the data, which is 8.534281. My goal here is to
test each data point to see if it exceeds the median value, and, next,
calculate the longest streak of days in the 92-day span in which the
data exceeded the median value.

To achieve this in Python, I've created the following function, where
q is a vector of length 92 and q_JJA_median is, of course, the median
value of the dataset.
##
def consecutive(q, q_JJA_median):
is_consecutive = 0
n = 0
array_exceed = []
for i in range(len(q)):
if q[i] > q_JJA_median:
n+=1
is_consecutive = 1
else:
if is_consecutive:
array_exceed.append(n)
n = 0
is_consecutive = 0
if q[i] > q_JJA_median:
array_exceed.append(n)
if len(array_exceed) == 0:
array_exceed = 0
if type(array_exceed) is int:
array_exceed = [0]
return array_exceed
###

Here is my work thus far written for R:
###
is_consecutive = 0
n = 0
array_exceed <- c()
for (i in q) {
  if (i > q_JJA_median) {
n = n + 1
is_consecutive = is_consecutive + 1
  }
  else {
if (is_consecutive) {
  append(array_exceed, n)
  n <- 0
  is_consecutive <- 0
}
  }
  if (i > q_JJA_median) {
append(array_exceed,n)
  }
}
###

My code written for R has been changed and manipulated many times, and
none of my attempts have been successful. I'm still new to the syntax
of the R language, so my problem very well could be a product of my
lack of experience.

Additionally, I read on a forum post that using the append function
can be slower than many other options. Is this true? If so, how can I
circumvent that issue?

Thank you!

__
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] How to specify a data frame column using command line arg?

2016-06-05 Thread Douglas Johnson
I'm guessing this is trivial but I've spent two hours searching and reading
FAQ, tutorial, introductory and 'idiom' documents and haven't found a
solution. All I want to do is select a data frame column at run time using
a command line arg. For example, the "test.csv" file contains:

a,b,c
1,2,3
4,5,6
7,8,9

If I run "test.r a", I get [1,4,7].
If I run "test.r b", I get [2,5,8].

Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or 'c'?

args <- commandArgs(trailingOnly=T)

adl1<-read.csv(file="test.csv",head=T,sep=",")

adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])


All I'm really trying to do is generate a bunch of scatter plots of column
'x' vs. 'y' without writing a separate program for each pair.


Thanks,


Doug



-- 
http://www.dojopico.org 

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


[R] dissolve tiny polygons to others with unionSpatialPolygons{maptools}

2016-06-05 Thread Kumar Mainali
I am trying to use unionSpatialPolygons() of maptools to eliminate sliver
in species range. I want to dissolve tiny sliver polygons in a shapefile to
bigger polygons as "Eliminate (Data Management)" of ArcMap does. Whereas I
can dissolve polygons that have identical features in the argument "IDs", I
cannot dissolve tiny polygons based on some threshold in area. In fact, the
argument "threshold" has no effect in the output.

​Input data is available here: ​
https://www.dropbox.com/sh/a0x5bbo9u60y7is/AAB6RjXHFQKZv-i-t4JclF3ba?dl=0

p.ranges <- shapefile{raster}
(IDs <- p.ranges$style_id)
library(maptools)
unionSpatialPolygons(p.ranges, IDs = IDs, threshold = 1.5)

​-- Kumar Mainali
Postdoctoral Associate
Department of Biology
University of Maryland, College Park


ᐧ

[[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] Reading and converting time data via read.table

2016-06-05 Thread Jim Lemon
Hi EKE,
Your problem may be that the date strings are being read as a factor.
Try using stringsAsFactors=FALSE when you read the data in. Another
way is to convert your dates to strings when passing to as.Date:

as.Date(as.character(mydf$Date),"%m/%d/%Y")

Jim


On Sun, Jun 5, 2016 at 10:53 PM, Ek Esawi  wrote:
> Hi All--
>
>
>
> I am relatively new to R. I am reading a csv file via read.table (MyFile).
> The data types in the file are date, string, integer, and time. I was able
> to read all the data and manipulated correctly except time, e.g., 12:30. I
> used as.Date to convert date and string and integer were easily done. I
> could not figure out how to convert the time data correctly. I tried chron
> but w/o success and I read that POSIXlt and POSIXct work only for date and
> time (e.g. 01/02/1999, 12:30:20). I did not try the lubridate package. Is
> there a way to read time data without date attached to it like mine?
>
>
>
> I am grateful for any help and thanks in advance—EKE
>
>
>
> Here is an example of my data when read into R via read.table
>
>
>
> AA  Date Name T1  T2
> N1
>
> 1  312171  7/1/1995   OF  13:37  1:43 123
>
> [[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.

__
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] system() command not working

2016-06-05 Thread J Payne

I forgot to add that setting the environment variable in .Renviron in my home 
directory *also* works.  I have updated my question on Stackoverflow to include 
these answers.

John

On 6/5/16, 1:17 PM, "J Payne"  wrote:

>Thanks Berend, that’s super useful.  In summary, here is what I found:
>
>The problem (if I understand correctly) is that R was not passing environment 
>variables to OSX El Capitan successfully.  In any case:
>
>These do not work:
>1. Setting the environment variable in my .bash_profile (for example 
>“MTR_DATA_DIR="/Applications/MRT/data"); or 
>2. Adding the same environment variable to .Rprofile in my home directory.
>
>These do work (hooray!):
>3. Changing the environment variable using 
>Sys.setenv(MRT_DATA_DIR="/Applications/MRT/data") inside R at the R command 
>line; or
>4. Typing “MRT_DATA_DIR="/Applications/MRT/data" open -a Rstudio” in the 
>Terminal.  This latter method is an effective workaround and wonderful to have 
>in my bag of tricks, but is slightly clumsier since I have to remember to open 
>RStudio this way  each time.  
>
>I’m very grateful to you and Roy for your help.
>
>John
>
>On 6/5/16, 3:28 AM, "Berend Hasselman"  wrote:
>
>>
>>> On 4 Jun 2016, at 22:12, Roy Mendelssohn - NOAA Federal 
>>>  wrote:
>>> 
>>> Hi John:
>>> 
>>> When El Capitan first came out there was a discussion in the  R-SIg-Mac  
>>> list about environmental variables not being passed down to applications  
>>> (not just R abut in general).  I believe a work around was suggested, but I 
>>> would search the archives for that.
>>> 
>>> So what is happening, when you run from the command line, the variables   
>>> MRT_DATA_DIR and MRTDATADIR which are defined somewhere in your environment 
>>> are found in the terminal, but when the same command is run from the 
>>> application they are not being found.  I would search the R-SIg-Mac archive 
>>> or post to that list, because i can’t remember what the work around was for 
>>> it.
>>> 
>>
>>I can't find the discussion on R-SIG-Mac list. But you can try this:
>>
>>MRT_DATA_DIR= open -a Rstudio
>>
>>or  
>>
>>MRT_DATA_DIR= open -a R
>>
>>Try it and see what happens.
>>It may even be possible to put something in .Rprofile  setting your 
>>environment variables.
>>
>>Berend Hasselman
>>
>>> HTH,
>>> 
>>> -Roy
>>> 
 On Jun 4, 2016, at 11:59 AM, J Payne  wrote:
 
 I’ve posted this question on StackExchange at 
 http://stackoverflow.com/questions/37604466/r-system-not-working-with-modis-reprojection-tool,
  but haven’t received any replies.  I’m hoping that someone who 
 understands the operation of the R system() command can help.  
 
 
 
 I have a command that works when typed into the Terminal on a Mac (OSX El 
 Cap), but exactly the same command fails when called from R using 
 `system()`. 
 
 
 
 I am trying to batch-process MODIS satellite files using a small program 
 called the MODIS Reprojection Tool 
 (https://lpdaac.usgs.gov/tools/modis_reprojection_tool).  My software is 
 all up to date.
 
 
 
 This is a simple example in which I mosaic two files.  The names of the 
 two files are in a text input file called `input.list`.  The command just 
 tells the `mrtmosaic` routine where to find the input list and where to 
 put the output. 
 
 
 
 This command works correctly in the Terminal:  
 
 
 
/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i 
 ~/temp/input.list -o ~/temp/output.hdf
 
 
 
 However, if I put exactly the same string into a variable and run it from 
 R (using RStudio), it fails:  
 
 
 
comstring<-"/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i 
 ~/temp/input.list -o ~/temp/output.hdf"  
 
system(comstring)
 
 
 
> Warning: gctp_call : Environmental Variable Not Found:   
 
MRT_DATA_DIR nor MRTDATADIR not defined
 
Error: GetInputGeoCornerMosaic : General Processing Error converting 
 lat/long coordinates to input projection coordinates.  
 
Fatal Error, Terminating...
 
 
 
 The strange thing is that the system knows what the environment variables 
 are.  In the terminal, the command
 
 `echo $MRT_DATA_DIR`
 
 shows the correct directory: /Applications/Modis_Reprojection_Tool/data
 
 
 
 I don't see why it would have trouble finding the variables from an `R 
 system()` call when it has no trouble in the Terminal.  I'm very stumped!  
 
 
 
 
 
 
 
 
[[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 

Re: [R] system() command not working

2016-06-05 Thread J Payne
Thanks Berend, that’s super useful.  In summary, here is what I found:

The problem (if I understand correctly) is that R was not passing environment 
variables to OSX El Capitan successfully.  In any case:

These do not work:
1. Setting the environment variable in my .bash_profile (for example 
“MTR_DATA_DIR="/Applications/MRT/data"); or 
2. Adding the same environment variable to .Rprofile in my home directory.

These do work (hooray!):
3. Changing the environment variable using 
Sys.setenv(MRT_DATA_DIR="/Applications/MRT/data") inside R at the R command 
line; or
4. Typing “MRT_DATA_DIR="/Applications/MRT/data" open -a Rstudio” in the 
Terminal.  This latter method is an effective workaround and wonderful to have 
in my bag of tricks, but is slightly clumsier since I have to remember to open 
RStudio this way  each time.  

I’m very grateful to you and Roy for your help.

John

On 6/5/16, 3:28 AM, "Berend Hasselman"  wrote:

>
>> On 4 Jun 2016, at 22:12, Roy Mendelssohn - NOAA Federal 
>>  wrote:
>> 
>> Hi John:
>> 
>> When El Capitan first came out there was a discussion in the  R-SIg-Mac  
>> list about environmental variables not being passed down to applications  
>> (not just R abut in general).  I believe a work around was suggested, but I 
>> would search the archives for that.
>> 
>> So what is happening, when you run from the command line, the variables   
>> MRT_DATA_DIR and MRTDATADIR which are defined somewhere in your environment 
>> are found in the terminal, but when the same command is run from the 
>> application they are not being found.  I would search the R-SIg-Mac archive 
>> or post to that list, because i can’t remember what the work around was for 
>> it.
>> 
>
>I can't find the discussion on R-SIG-Mac list. But you can try this:
>
>MRT_DATA_DIR= open -a Rstudio
>
>or  
>
>MRT_DATA_DIR= open -a R
>
>Try it and see what happens.
>It may even be possible to put something in .Rprofile  setting your 
>environment variables.
>
>Berend Hasselman
>
>> HTH,
>> 
>> -Roy
>> 
>>> On Jun 4, 2016, at 11:59 AM, J Payne  wrote:
>>> 
>>> I’ve posted this question on StackExchange at 
>>> http://stackoverflow.com/questions/37604466/r-system-not-working-with-modis-reprojection-tool,
>>>  but haven’t received any replies.  I’m hoping that someone who understands 
>>> the operation of the R system() command can help.  
>>> 
>>> 
>>> 
>>> I have a command that works when typed into the Terminal on a Mac (OSX El 
>>> Cap), but exactly the same command fails when called from R using 
>>> `system()`. 
>>> 
>>> 
>>> 
>>> I am trying to batch-process MODIS satellite files using a small program 
>>> called the MODIS Reprojection Tool 
>>> (https://lpdaac.usgs.gov/tools/modis_reprojection_tool).  My software is 
>>> all up to date.
>>> 
>>> 
>>> 
>>> This is a simple example in which I mosaic two files.  The names of the two 
>>> files are in a text input file called `input.list`.  The command just tells 
>>> the `mrtmosaic` routine where to find the input list and where to put the 
>>> output. 
>>> 
>>> 
>>> 
>>> This command works correctly in the Terminal:  
>>> 
>>> 
>>> 
>>>/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list 
>>> -o ~/temp/output.hdf
>>> 
>>> 
>>> 
>>> However, if I put exactly the same string into a variable and run it from R 
>>> (using RStudio), it fails:  
>>> 
>>> 
>>> 
>>>comstring<-"/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i 
>>> ~/temp/input.list -o ~/temp/output.hdf"  
>>> 
>>>system(comstring)
>>> 
>>> 
>>> 
 Warning: gctp_call : Environmental Variable Not Found:   
>>> 
>>>MRT_DATA_DIR nor MRTDATADIR not defined
>>> 
>>>Error: GetInputGeoCornerMosaic : General Processing Error converting 
>>> lat/long coordinates to input projection coordinates.  
>>> 
>>>Fatal Error, Terminating...
>>> 
>>> 
>>> 
>>> The strange thing is that the system knows what the environment variables 
>>> are.  In the terminal, the command
>>> 
>>> `echo $MRT_DATA_DIR`
>>> 
>>> shows the correct directory: /Applications/Modis_Reprojection_Tool/data
>>> 
>>> 
>>> 
>>> I don't see why it would have trouble finding the variables from an `R 
>>> system()` call when it has no trouble in the Terminal.  I'm very stumped!  
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> [[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.
>> 
>> **
>> "The contents of this message do not reflect any position of the U.S. 
>> Government or NOAA."
>> **
>> Roy Mendelssohn
>> Supervisory Operations Research Analyst
>> NOAA/NMFS
>> Environmental Research Division
>> 

Re: [R] detecting if a variable has changed

2016-06-05 Thread Ted Harding
Ah, perhaps I'm beginning to undertstand the question!
Presumably the issue is that evaluating X[X<=y] takes O(n) time,
where n = length(X), and similarly X[X>y]. 

So I suppose that one needs to be looking at some procedure for
a "bisecting" search for the largest r such that X[r] <= y, which
would then be O(log2(n)).

Perhaps not altogether straightforward to program, but straqightforward
in concept!

Apologies for misunderstanding.
Ted.

On 05-Jun-2016 18:13:15 Bert Gunter wrote:
> Nope, Ted. I asked for  a O(log(n)) solution, not an O(n) one.
> 
> I will check out the data.table package, as suggested.
> 
> -- Bert
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Sun, Jun 5, 2016 at 11:01 AM, Ted Harding 
> wrote:
>> Surely it is straightforward, since the vector (say 'X') is already sorted?
>>
>> Example (raw code with explicit example):
>>
>> set.seed(54321)
>> X <- sort(runif(10))
>> # X ## The initial sorted vector
>> # [1] 0.04941009 0.17669234 0.20913493 0.21651016 0.27439354
>> # [6] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110
>>
>> y <- runif(1)
>> # y ## The new value to be inserted
>> [1] 0.1366424
>>
>> Y <- c(X[X<=y],y,X[X>y]) ## Now insert y into X:
>> Y
>> [1] 0.04941009 0.13664239 0.17669234 0.20913493 0.21651016 0.27439354
>> [7] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110
>>
>> ## And there it is at Y[2]
>>
>> Easy to make such a function!
>> Best wishes to all,
>> Ted.
>>
>> On 05-Jun-2016 17:44:29 Neal H. Walfield wrote:
>>> On Sun, 05 Jun 2016 19:34:38 +0200,
>>> Bert Gunter wrote:
 This help thread suggested a question to me:

 Is there a function in some package that efficiently (I.e. O(log(n)) )
 inserts a single new element into the correct location in an
 already-sorted vector? My assumption here is that doing it via sort()
 is inefficient, but maybe that is incorrect. Please correct me if so.
>>>
>>> I think data.table will do this if the the column is marked
>>> appropriately.
>>>
 I realize that it would be straightforward to write such a function,
 but I just wondered if it already exists. My google & rseek searches
 did not succeed, but maybe I used the wrong keywords.

 Cheers,
 Bert


 Bert Gunter

 "The trouble with having an open mind is that people keep coming along
 and sticking things into it."
 -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


 On Sun, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
  wrote:
 > I don't know what you mean by "without having to use any special
 > interfaces", but "reference classes" will do what I think you want. 
 > E.g.,
 > the following makes a class called 'SortedNumeric' that only sorts the
 > vector when you want to get its value, not when you append values.  It
 > stores the sorted vector so it does not get resorted each time you ask
 > for
 > it.
 >
 > SortedNumeric <- setRefClass("sortedNumeric",
 > fields = list(
 > fData = "numeric",
 > fIsUnsorted = "logical"),
 > methods = list(
 > initialize = function(Data = numeric(), isUnsorted =
 > TRUE)
 > {
 > fData <<- Data
 > stopifnot(is.logical(isUnsorted),
 >   length(isUnsorted)==1,
 >   !is.na(isUnsorted))
 > fIsUnsorted <<- isUnsorted
 > },
 > getData = function() {
 > if (isUnsorted) {
 > fData <<- sort(fData)
 > fIsUnsorted <<- FALSE
 > }
 > fData
 > },
 > appendData = function(newEntries) {
 > fData <<- c(fData, newEntries)
 > fIsUnsorted <<- TRUE
 > }
 > ))
 >
 > Use it as:
 >
 >> x <- SortedNumeric$new()
 >> x$appendData(c(4,2,5))
 >> x$appendData(c(1,8,9))
 >> x
 > Reference class object of class "sortedNumeric"
 > Field "fData":
 > [1] 4 2 5 1 8 9
 > Field "fIsUnsorted":
 > [1] TRUE
 >> x$getData()
 > [1] 1 2 4 5 8 9
 >> x
 > Reference class object of class "sortedNumeric"
 > Field "fData":
 > [1] 1 2 4 5 8 9
 > Field "fIsUnsorted":
 > [1] FALSE
 >
 >
 > Outside of base R, I think the R6 package gives another approach to
 > this.
 >
 >
 > Bill Dunlap
 > TIBCO Software
 > wdunlap tibco.com
 >
 > On Sun, Jun 5, 2016 at 6:53 AM, 

Re: [R] system() command not working

2016-06-05 Thread J Payne
Thanks very much Roy!  I will post to R-Sig-Mac.  I browsed their archives but 
didn’t see anything about the issue – I might have missed something, though.  

John


On 6/4/16, 1:12 PM, "Roy Mendelssohn - NOAA Federal"  
wrote:

>Hi John:
>
>When El Capitan first came out there was a discussion in the  R-SIg-Mac  list 
>about environmental variables not being passed down to applications  (not just 
>R abut in general).  I believe a work around was suggested, but I would search 
>the archives for that.
>
>So what is happening, when you run from the command line, the variables   
>MRT_DATA_DIR and MRTDATADIR which are defined somewhere in your environment 
>are found in the terminal, but when the same command is run from the 
>application they are not being found.  I would search the R-SIg-Mac archive or 
>post to that list, because i can’t remember what the work around was for it.
>
>HTH,
>
>-Roy
>
>> On Jun 4, 2016, at 11:59 AM, J Payne  wrote:
>> 
>> I’ve posted this question on StackExchange at 
>> http://stackoverflow.com/questions/37604466/r-system-not-working-with-modis-reprojection-tool,
>>  but haven’t received any replies.  I’m hoping that someone who understands 
>> the operation of the R system() command can help.  
>> 
>> 
>> 
>> I have a command that works when typed into the Terminal on a Mac (OSX El 
>> Cap), but exactly the same command fails when called from R using 
>> `system()`. 
>> 
>>  
>> 
>> I am trying to batch-process MODIS satellite files using a small program 
>> called the MODIS Reprojection Tool 
>> (https://lpdaac.usgs.gov/tools/modis_reprojection_tool).  My software is all 
>> up to date.
>> 
>> 
>> 
>> This is a simple example in which I mosaic two files.  The names of the two 
>> files are in a text input file called `input.list`.  The command just tells 
>> the `mrtmosaic` routine where to find the input list and where to put the 
>> output. 
>> 
>> 
>> 
>> This command works correctly in the Terminal:  
>> 
>> 
>> 
>> /Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list 
>> -o ~/temp/output.hdf
>> 
>> 
>> 
>> However, if I put exactly the same string into a variable and run it from R 
>> (using RStudio), it fails:  
>> 
>> 
>> 
>> comstring<-"/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i 
>> ~/temp/input.list -o ~/temp/output.hdf"  
>> 
>> system(comstring)
>> 
>> 
>> 
>>> Warning: gctp_call : Environmental Variable Not Found:   
>> 
>> MRT_DATA_DIR nor MRTDATADIR not defined
>> 
>> Error: GetInputGeoCornerMosaic : General Processing Error converting 
>> lat/long coordinates to input projection coordinates.  
>> 
>> Fatal Error, Terminating...
>> 
>> 
>> 
>> The strange thing is that the system knows what the environment variables 
>> are.  In the terminal, the command
>> 
>> `echo $MRT_DATA_DIR`
>> 
>> shows the correct directory: /Applications/Modis_Reprojection_Tool/data
>> 
>> 
>> 
>> I don't see why it would have trouble finding the variables from an `R 
>> system()` call when it has no trouble in the Terminal.  I'm very stumped!  
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>  [[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.
>
>**
>"The contents of this message do not reflect any position of the U.S. 
>Government or NOAA."
>**
>Roy Mendelssohn
>Supervisory Operations Research Analyst
>NOAA/NMFS
>Environmental Research Division
>Southwest Fisheries Science Center
>***Note new address and phone***
>110 Shaffer Road
>Santa Cruz, CA 95060
>Phone: (831)-420-3666
>Fax: (831) 420-3980
>e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
>
>"Old age and treachery will overcome youth and skill."
>"From those who have been given much, much will be expected" 
>"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
>

__
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] Training set in Self organizing Map

2016-06-05 Thread ch.elahe via R-help
Hi Jeff,
Thanks for your reply. My df contains Protocols and their Parameters and I want 
to use SOM to see if I can find some clusters in customor's using protocols. 
Some of these Parameters are factors and some are numeric. I want to make a 
subset of some protocols and give them to SOM as training set and keep some for 
test set. is it possible to have also those factors in training set of SOM?



$ Protocol  : Factor w/ 132 levels "_unknown","A5. SAG TSE T2 
FS",..: 5
$ BR: int  320 320 384 384 384 320 256 320 384 38
$ BW: int  150 150 191 191 98 150 150 150 
$ COUNTRY   : Factor w/ 35 levels "AE","AT","AU",..: 10 10 
$ FSM   : Factor w/ 2 levels "strong","weak": 2 2 


On Wednesday, June 1, 2016 7:59 AM, Jeff Newmiller  
wrote:



You did not send  sample of your data, using dput. Before doing that,  I 
suggest peeling apart your troublesome line of code yourself:

str( as.matrix( scale( subdf ) ) )
str( scale( subdf ) )
str( subdf )

And then think about what the scale function does. Does it make sense to ask it 
to scale character or factor data? Could you perhaps exclude some of the 
columns that don't belong in the scaled data? 
-- 
Sent from my phone. Please excuse my brevity.


On June 1, 2016 7:39:30 AM PDT, "ch.elahe via R-help"  
wrote:
Hi all,
>I want to use Self Organizing Map in R for my data. I want my training set to 
>be the following subset of my data:
>
>
>subdf=subset(df,Country%in%c("US","FR"))
>next I should change this subset to a matrix but I get the following error:
>
>data_train_matrix=as.matrix(scale(subdf))
>error in colMeans(x,na.rm=TRUE):'x' must be numeric
>
>Can anyone help me to solve that?
>Thanks for any help
>Elahe
>
>
>
>
>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.


Re: [R] detecting if a variable has changed

2016-06-05 Thread Bert Gunter
Nope, Ted. I asked for  a O(log(n)) solution, not an O(n) one.

I will check out the data.table package, as suggested.

-- Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Jun 5, 2016 at 11:01 AM, Ted Harding  wrote:
> Surely it is straightforward, since the vector (say 'X') is already sorted?
>
> Example (raw code with explicit example):
>
> set.seed(54321)
> X <- sort(runif(10))
> # X ## The initial sorted vector
> # [1] 0.04941009 0.17669234 0.20913493 0.21651016 0.27439354
> # [6] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110
>
> y <- runif(1)
> # y ## The new value to be inserted
> [1] 0.1366424
>
> Y <- c(X[X<=y],y,X[X>y]) ## Now insert y into X:
> Y
> [1] 0.04941009 0.13664239 0.17669234 0.20913493 0.21651016 0.27439354
> [7] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110
>
> ## And there it is at Y[2]
>
> Easy to make such a function!
> Best wishes to all,
> Ted.
>
> On 05-Jun-2016 17:44:29 Neal H. Walfield wrote:
>> On Sun, 05 Jun 2016 19:34:38 +0200,
>> Bert Gunter wrote:
>>> This help thread suggested a question to me:
>>>
>>> Is there a function in some package that efficiently (I.e. O(log(n)) )
>>> inserts a single new element into the correct location in an
>>> already-sorted vector? My assumption here is that doing it via sort()
>>> is inefficient, but maybe that is incorrect. Please correct me if so.
>>
>> I think data.table will do this if the the column is marked
>> appropriately.
>>
>>> I realize that it would be straightforward to write such a function,
>>> but I just wondered if it already exists. My google & rseek searches
>>> did not succeed, but maybe I used the wrong keywords.
>>>
>>> Cheers,
>>> Bert
>>>
>>>
>>> Bert Gunter
>>>
>>> "The trouble with having an open mind is that people keep coming along
>>> and sticking things into it."
>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>>
>>>
>>> On Sun, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
>>>  wrote:
>>> > I don't know what you mean by "without having to use any special
>>> > interfaces", but "reference classes" will do what I think you want.  E.g.,
>>> > the following makes a class called 'SortedNumeric' that only sorts the
>>> > vector when you want to get its value, not when you append values.  It
>>> > stores the sorted vector so it does not get resorted each time you ask for
>>> > it.
>>> >
>>> > SortedNumeric <- setRefClass("sortedNumeric",
>>> > fields = list(
>>> > fData = "numeric",
>>> > fIsUnsorted = "logical"),
>>> > methods = list(
>>> > initialize = function(Data = numeric(), isUnsorted = TRUE)
>>> > {
>>> > fData <<- Data
>>> > stopifnot(is.logical(isUnsorted),
>>> >   length(isUnsorted)==1,
>>> >   !is.na(isUnsorted))
>>> > fIsUnsorted <<- isUnsorted
>>> > },
>>> > getData = function() {
>>> > if (isUnsorted) {
>>> > fData <<- sort(fData)
>>> > fIsUnsorted <<- FALSE
>>> > }
>>> > fData
>>> > },
>>> > appendData = function(newEntries) {
>>> > fData <<- c(fData, newEntries)
>>> > fIsUnsorted <<- TRUE
>>> > }
>>> > ))
>>> >
>>> > Use it as:
>>> >
>>> >> x <- SortedNumeric$new()
>>> >> x$appendData(c(4,2,5))
>>> >> x$appendData(c(1,8,9))
>>> >> x
>>> > Reference class object of class "sortedNumeric"
>>> > Field "fData":
>>> > [1] 4 2 5 1 8 9
>>> > Field "fIsUnsorted":
>>> > [1] TRUE
>>> >> x$getData()
>>> > [1] 1 2 4 5 8 9
>>> >> x
>>> > Reference class object of class "sortedNumeric"
>>> > Field "fData":
>>> > [1] 1 2 4 5 8 9
>>> > Field "fIsUnsorted":
>>> > [1] FALSE
>>> >
>>> >
>>> > Outside of base R, I think the R6 package gives another approach to this.
>>> >
>>> >
>>> > Bill Dunlap
>>> > TIBCO Software
>>> > wdunlap tibco.com
>>> >
>>> > On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield 
>>> > wrote:
>>> >
>>> >> Hi,
>>> >>
>>> >> I have a huge list.  Normally it is sorted, but I want to be able to
>>> >> add elements to it without having to use any special interfaces and
>>> >> then sort it on demand.  My idea is to use something like weak
>>> >> references combined with attributes.  Consider:
>>> >>
>>> >>   # Initialization.
>>> >>   l = as.list(1:10)
>>> >>   # Note that it is sorted.
>>> >>   attr(l, 'sorted') = weakref(l)
>>> >>
>>> >>   # Modify the list.
>>> >>   l = append(l, 1:3)
>>> >>
>>> >>   # Check if the list is still sorted.  (I use identical here, but it
>>> >>   # probably too heavy weight: I just 

Re: [R] detecting if a variable has changed

2016-06-05 Thread Ted Harding
Surely it is straightforward, since the vector (say 'X') is already sorted?

Example (raw code with explicit example):

set.seed(54321)
X <- sort(runif(10))
# X ## The initial sorted vector
# [1] 0.04941009 0.17669234 0.20913493 0.21651016 0.27439354
# [6] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110

y <- runif(1)
# y ## The new value to be inserted
[1] 0.1366424

Y <- c(X[X<=y],y,X[X>y]) ## Now insert y into X:
Y
[1] 0.04941009 0.13664239 0.17669234 0.20913493 0.21651016 0.27439354
[7] 0.34161241 0.37165878 0.42900782 0.49843042 0.86636110

## And there it is at Y[2]

Easy to make such a function!
Best wishes to all,
Ted.

On 05-Jun-2016 17:44:29 Neal H. Walfield wrote:
> On Sun, 05 Jun 2016 19:34:38 +0200,
> Bert Gunter wrote:
>> This help thread suggested a question to me:
>> 
>> Is there a function in some package that efficiently (I.e. O(log(n)) )
>> inserts a single new element into the correct location in an
>> already-sorted vector? My assumption here is that doing it via sort()
>> is inefficient, but maybe that is incorrect. Please correct me if so.
> 
> I think data.table will do this if the the column is marked
> appropriately.
> 
>> I realize that it would be straightforward to write such a function,
>> but I just wondered if it already exists. My google & rseek searches
>> did not succeed, but maybe I used the wrong keywords.
>> 
>> Cheers,
>> Bert
>> 
>> 
>> Bert Gunter
>> 
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>> 
>> 
>> On Sun, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
>>  wrote:
>> > I don't know what you mean by "without having to use any special
>> > interfaces", but "reference classes" will do what I think you want.  E.g.,
>> > the following makes a class called 'SortedNumeric' that only sorts the
>> > vector when you want to get its value, not when you append values.  It
>> > stores the sorted vector so it does not get resorted each time you ask for
>> > it.
>> >
>> > SortedNumeric <- setRefClass("sortedNumeric",
>> > fields = list(
>> > fData = "numeric",
>> > fIsUnsorted = "logical"),
>> > methods = list(
>> > initialize = function(Data = numeric(), isUnsorted = TRUE)
>> > {
>> > fData <<- Data
>> > stopifnot(is.logical(isUnsorted),
>> >   length(isUnsorted)==1,
>> >   !is.na(isUnsorted))
>> > fIsUnsorted <<- isUnsorted
>> > },
>> > getData = function() {
>> > if (isUnsorted) {
>> > fData <<- sort(fData)
>> > fIsUnsorted <<- FALSE
>> > }
>> > fData
>> > },
>> > appendData = function(newEntries) {
>> > fData <<- c(fData, newEntries)
>> > fIsUnsorted <<- TRUE
>> > }
>> > ))
>> >
>> > Use it as:
>> >
>> >> x <- SortedNumeric$new()
>> >> x$appendData(c(4,2,5))
>> >> x$appendData(c(1,8,9))
>> >> x
>> > Reference class object of class "sortedNumeric"
>> > Field "fData":
>> > [1] 4 2 5 1 8 9
>> > Field "fIsUnsorted":
>> > [1] TRUE
>> >> x$getData()
>> > [1] 1 2 4 5 8 9
>> >> x
>> > Reference class object of class "sortedNumeric"
>> > Field "fData":
>> > [1] 1 2 4 5 8 9
>> > Field "fIsUnsorted":
>> > [1] FALSE
>> >
>> >
>> > Outside of base R, I think the R6 package gives another approach to this.
>> >
>> >
>> > Bill Dunlap
>> > TIBCO Software
>> > wdunlap tibco.com
>> >
>> > On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield 
>> > wrote:
>> >
>> >> Hi,
>> >>
>> >> I have a huge list.  Normally it is sorted, but I want to be able to
>> >> add elements to it without having to use any special interfaces and
>> >> then sort it on demand.  My idea is to use something like weak
>> >> references combined with attributes.  Consider:
>> >>
>> >>   # Initialization.
>> >>   l = as.list(1:10)
>> >>   # Note that it is sorted.
>> >>   attr(l, 'sorted') = weakref(l)
>> >>
>> >>   # Modify the list.
>> >>   l = append(l, 1:3)
>> >>
>> >>   # Check if the list is still sorted.  (I use identical here, but it
>> >>   # probably too heavy weight: I just need to compare the addresses.)
>> >>   if (! identical(l, attr(l, 'sorted'))) {
>> >> l = sort(unlist(l))
>> >> attr(l, 'sorted') = weakref(l)
>> >>   }
>> >>   # Do operation that requires sorted list.
>> >>   ...
>> >>
>> >> This is obviously a toy example.  I'm not actually sorting integers
>> >> and I may use a matrix instead of a list.
>> >>
>> >> I've read:
>> >>
>> >>   http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
>> >>   http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
>> >>
>> >> As far 

Re: [R] detecting if a variable has changed

2016-06-05 Thread Neal H. Walfield
On Sun, 05 Jun 2016 19:34:38 +0200,
Bert Gunter wrote:
> This help thread suggested a question to me:
> 
> Is there a function in some package that efficiently (I.e. O(log(n)) )
> inserts a single new element into the correct location in an
> already-sorted vector? My assumption here is that doing it via sort()
> is inefficient, but maybe that is incorrect. Please correct me if so.

I think data.table will do this if the the column is marked
appropriately.

> I realize that it would be straightforward to write such a function,
> but I just wondered if it already exists. My google & rseek searches
> did not succeed, but maybe I used the wrong keywords.
> 
> Cheers,
> Bert
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Sun, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
>  wrote:
> > I don't know what you mean by "without having to use any special
> > interfaces", but "reference classes" will do what I think you want.  E.g.,
> > the following makes a class called 'SortedNumeric' that only sorts the
> > vector when you want to get its value, not when you append values.  It
> > stores the sorted vector so it does not get resorted each time you ask for
> > it.
> >
> > SortedNumeric <- setRefClass("sortedNumeric",
> > fields = list(
> > fData = "numeric",
> > fIsUnsorted = "logical"),
> > methods = list(
> > initialize = function(Data = numeric(), isUnsorted = TRUE) {
> > fData <<- Data
> > stopifnot(is.logical(isUnsorted),
> >   length(isUnsorted)==1,
> >   !is.na(isUnsorted))
> > fIsUnsorted <<- isUnsorted
> > },
> > getData = function() {
> > if (isUnsorted) {
> > fData <<- sort(fData)
> > fIsUnsorted <<- FALSE
> > }
> > fData
> > },
> > appendData = function(newEntries) {
> > fData <<- c(fData, newEntries)
> > fIsUnsorted <<- TRUE
> > }
> > ))
> >
> > Use it as:
> >
> >> x <- SortedNumeric$new()
> >> x$appendData(c(4,2,5))
> >> x$appendData(c(1,8,9))
> >> x
> > Reference class object of class "sortedNumeric"
> > Field "fData":
> > [1] 4 2 5 1 8 9
> > Field "fIsUnsorted":
> > [1] TRUE
> >> x$getData()
> > [1] 1 2 4 5 8 9
> >> x
> > Reference class object of class "sortedNumeric"
> > Field "fData":
> > [1] 1 2 4 5 8 9
> > Field "fIsUnsorted":
> > [1] FALSE
> >
> >
> > Outside of base R, I think the R6 package gives another approach to this.
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield  wrote:
> >
> >> Hi,
> >>
> >> I have a huge list.  Normally it is sorted, but I want to be able to
> >> add elements to it without having to use any special interfaces and
> >> then sort it on demand.  My idea is to use something like weak
> >> references combined with attributes.  Consider:
> >>
> >>   # Initialization.
> >>   l = as.list(1:10)
> >>   # Note that it is sorted.
> >>   attr(l, 'sorted') = weakref(l)
> >>
> >>   # Modify the list.
> >>   l = append(l, 1:3)
> >>
> >>   # Check if the list is still sorted.  (I use identical here, but it
> >>   # probably too heavy weight: I just need to compare the addresses.)
> >>   if (! identical(l, attr(l, 'sorted'))) {
> >> l = sort(unlist(l))
> >> attr(l, 'sorted') = weakref(l)
> >>   }
> >>   # Do operation that requires sorted list.
> >>   ...
> >>
> >> This is obviously a toy example.  I'm not actually sorting integers
> >> and I may use a matrix instead of a list.
> >>
> >> I've read:
> >>
> >>   http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
> >>   http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
> >>
> >> As far as I can tell, weakrefs are only available via the C API.  Is
> >> there a way to do what I want in R without resorting to C code?  Is
> >> what I want to do better achieved using something other than weakrefs?
> >>
> >> Thanks!
> >>
> >> :) Neal
> >>
> >> __
> >> 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 

Re: [R] detecting if a variable has changed

2016-06-05 Thread Bert Gunter
This help thread suggested a question to me:

Is there a function in some package that efficiently (I.e. O(log(n)) )
inserts a single new element into the correct location in an
already-sorted vector? My assumption here is that doing it via sort()
is inefficient, but maybe that is incorrect. Please correct me if so.

I realize that it would be straightforward to write such a function,
but I just wondered if it already exists. My google & rseek searches
did not succeed, but maybe I used the wrong keywords.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
 wrote:
> I don't know what you mean by "without having to use any special
> interfaces", but "reference classes" will do what I think you want.  E.g.,
> the following makes a class called 'SortedNumeric' that only sorts the
> vector when you want to get its value, not when you append values.  It
> stores the sorted vector so it does not get resorted each time you ask for
> it.
>
> SortedNumeric <- setRefClass("sortedNumeric",
> fields = list(
> fData = "numeric",
> fIsUnsorted = "logical"),
> methods = list(
> initialize = function(Data = numeric(), isUnsorted = TRUE) {
> fData <<- Data
> stopifnot(is.logical(isUnsorted),
>   length(isUnsorted)==1,
>   !is.na(isUnsorted))
> fIsUnsorted <<- isUnsorted
> },
> getData = function() {
> if (isUnsorted) {
> fData <<- sort(fData)
> fIsUnsorted <<- FALSE
> }
> fData
> },
> appendData = function(newEntries) {
> fData <<- c(fData, newEntries)
> fIsUnsorted <<- TRUE
> }
> ))
>
> Use it as:
>
>> x <- SortedNumeric$new()
>> x$appendData(c(4,2,5))
>> x$appendData(c(1,8,9))
>> x
> Reference class object of class "sortedNumeric"
> Field "fData":
> [1] 4 2 5 1 8 9
> Field "fIsUnsorted":
> [1] TRUE
>> x$getData()
> [1] 1 2 4 5 8 9
>> x
> Reference class object of class "sortedNumeric"
> Field "fData":
> [1] 1 2 4 5 8 9
> Field "fIsUnsorted":
> [1] FALSE
>
>
> Outside of base R, I think the R6 package gives another approach to this.
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield  wrote:
>
>> Hi,
>>
>> I have a huge list.  Normally it is sorted, but I want to be able to
>> add elements to it without having to use any special interfaces and
>> then sort it on demand.  My idea is to use something like weak
>> references combined with attributes.  Consider:
>>
>>   # Initialization.
>>   l = as.list(1:10)
>>   # Note that it is sorted.
>>   attr(l, 'sorted') = weakref(l)
>>
>>   # Modify the list.
>>   l = append(l, 1:3)
>>
>>   # Check if the list is still sorted.  (I use identical here, but it
>>   # probably too heavy weight: I just need to compare the addresses.)
>>   if (! identical(l, attr(l, 'sorted'))) {
>> l = sort(unlist(l))
>> attr(l, 'sorted') = weakref(l)
>>   }
>>   # Do operation that requires sorted list.
>>   ...
>>
>> This is obviously a toy example.  I'm not actually sorting integers
>> and I may use a matrix instead of a list.
>>
>> I've read:
>>
>>   http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
>>   http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
>>
>> As far as I can tell, weakrefs are only available via the C API.  Is
>> there a way to do what I want in R without resorting to C code?  Is
>> what I want to do better achieved using something other than weakrefs?
>>
>> Thanks!
>>
>> :) Neal
>>
>> __
>> 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.

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

Re: [R] detecting if a variable has changed

2016-06-05 Thread Neal H. Walfield
Hi,

This looks more or less what I'm looking for!  Thanks!

:) Neal

On Sun, 05 Jun 2016 18:47:11 +0200,
William Dunlap wrote:
> 
> [1  ]
> [2  ]
> I don't know what you mean by "without having to use any special
> interfaces", but "reference classes" will do what I think you want.
> E.g., the following makes a class called 'SortedNumeric' that only
> sorts the vector when you want to get its value, not when you append
> values. It stores the sorted vector so it does not get resorted each
> time you ask for it.
> 
> SortedNumeric <- setRefClass("sortedNumeric",
> fields = list(
> fData = "numeric",
> fIsUnsorted = "logical"),
> methods = list(
> initialize = function(Data = numeric(), isUnsorted = TRUE) {
> fData <<- Data
> stopifnot(is.logical(isUnsorted),
> length(isUnsorted)==1,
> !is.na(isUnsorted))
> fIsUnsorted <<- isUnsorted
> },
> getData = function() {
> if (isUnsorted) {
> fData <<- sort(fData)
> fIsUnsorted <<- FALSE
> }
> fData
> },
> appendData = function(newEntries) {
> fData <<- c(fData, newEntries)
> fIsUnsorted <<- TRUE
> }
> ))
> 
> Use it as:
> 
> 
> 
> > x <- SortedNumeric$new()
> 
> 
> > x$appendData(c(4,2,5))
> 
> 
> > x$appendData(c(1,8,9))
> 
> 
> > x
> 
> 
> Reference class object of class "sortedNumeric"
> 
> 
> Field "fData":
> 
> 
> [1] 4 2 5 1 8 9
> 
> 
> Field "fIsUnsorted":
> 
> 
> [1] TRUE
> 
> 
> > x$getData()
> 
> 
> [1] 1 2 4 5 8 9
> 
> 
> > x
> 
> 
> Reference class object of class "sortedNumeric"
> 
> 
> Field "fData":
> 
> 
> [1] 1 2 4 5 8 9
> 
> 
> Field "fIsUnsorted":
> 
> 
> [1] FALSE
> 
> Outside of base R, I think the R6 package gives another approach to
> this.
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield 
> wrote:
> 
> Hi,
> 
> I have a huge list. Normally it is sorted, but I want to be able
> to
> add elements to it without having to use any special interfaces
> and
> then sort it on demand. My idea is to use something like weak
> references combined with attributes. Consider:
> 
> # Initialization.
> l = as.list(1:10)
> # Note that it is sorted.
> attr(l, 'sorted') = weakref(l)
> 
> # Modify the list.
> l = append(l, 1:3)
> 
> # Check if the list is still sorted. (I use identical here, but it
> # probably too heavy weight: I just need to compare the
> addresses.)
> if (! identical(l, attr(l, 'sorted'))) {
> l = sort(unlist(l))
> attr(l, 'sorted') = weakref(l)
> }
> # Do operation that requires sorted list.
> ...
> 
> This is obviously a toy example. I'm not actually sorting integers
> and I may use a matrix instead of a list.
> 
> I've read:
> 
> http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
> http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
> 
> As far as I can tell, weakrefs are only available via the C API.
> Is
> there a way to do what I want in R without resorting to C code? Is
> what I want to do better achieved using something other than
> weakrefs?
> 
> Thanks!
> 
> :) Neal
> 
> __
> 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.


Re: [R] detecting if a variable has changed

2016-06-05 Thread William Dunlap via R-help
I don't know what you mean by "without having to use any special
interfaces", but "reference classes" will do what I think you want.  E.g.,
the following makes a class called 'SortedNumeric' that only sorts the
vector when you want to get its value, not when you append values.  It
stores the sorted vector so it does not get resorted each time you ask for
it.

SortedNumeric <- setRefClass("sortedNumeric",
fields = list(
fData = "numeric",
fIsUnsorted = "logical"),
methods = list(
initialize = function(Data = numeric(), isUnsorted = TRUE) {
fData <<- Data
stopifnot(is.logical(isUnsorted),
  length(isUnsorted)==1,
  !is.na(isUnsorted))
fIsUnsorted <<- isUnsorted
},
getData = function() {
if (isUnsorted) {
fData <<- sort(fData)
fIsUnsorted <<- FALSE
}
fData
},
appendData = function(newEntries) {
fData <<- c(fData, newEntries)
fIsUnsorted <<- TRUE
}
))

Use it as:

> x <- SortedNumeric$new()
> x$appendData(c(4,2,5))
> x$appendData(c(1,8,9))
> x
Reference class object of class "sortedNumeric"
Field "fData":
[1] 4 2 5 1 8 9
Field "fIsUnsorted":
[1] TRUE
> x$getData()
[1] 1 2 4 5 8 9
> x
Reference class object of class "sortedNumeric"
Field "fData":
[1] 1 2 4 5 8 9
Field "fIsUnsorted":
[1] FALSE


Outside of base R, I think the R6 package gives another approach to this.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield  wrote:

> Hi,
>
> I have a huge list.  Normally it is sorted, but I want to be able to
> add elements to it without having to use any special interfaces and
> then sort it on demand.  My idea is to use something like weak
> references combined with attributes.  Consider:
>
>   # Initialization.
>   l = as.list(1:10)
>   # Note that it is sorted.
>   attr(l, 'sorted') = weakref(l)
>
>   # Modify the list.
>   l = append(l, 1:3)
>
>   # Check if the list is still sorted.  (I use identical here, but it
>   # probably too heavy weight: I just need to compare the addresses.)
>   if (! identical(l, attr(l, 'sorted'))) {
> l = sort(unlist(l))
> attr(l, 'sorted') = weakref(l)
>   }
>   # Do operation that requires sorted list.
>   ...
>
> This is obviously a toy example.  I'm not actually sorting integers
> and I may use a matrix instead of a list.
>
> I've read:
>
>   http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
>   http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
>
> As far as I can tell, weakrefs are only available via the C API.  Is
> there a way to do what I want in R without resorting to C code?  Is
> what I want to do better achieved using something other than weakrefs?
>
> Thanks!
>
> :) Neal
>
> __
> 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.


[R] detecting if a variable has changed

2016-06-05 Thread Neal H. Walfield
Hi,

I have a huge list.  Normally it is sorted, but I want to be able to
add elements to it without having to use any special interfaces and
then sort it on demand.  My idea is to use something like weak
references combined with attributes.  Consider:

  # Initialization.
  l = as.list(1:10)
  # Note that it is sorted.
  attr(l, 'sorted') = weakref(l)

  # Modify the list.
  l = append(l, 1:3)

  # Check if the list is still sorted.  (I use identical here, but it
  # probably too heavy weight: I just need to compare the addresses.)
  if (! identical(l, attr(l, 'sorted'))) {
l = sort(unlist(l))
attr(l, 'sorted') = weakref(l)
  }
  # Do operation that requires sorted list.
  ...

This is obviously a toy example.  I'm not actually sorting integers
and I may use a matrix instead of a list.

I've read:

  http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
  http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html

As far as I can tell, weakrefs are only available via the C API.  Is
there a way to do what I want in R without resorting to C code?  Is
what I want to do better achieved using something other than weakrefs?

Thanks!

:) Neal

__
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] Fw: i need to install "Rclimdex"........Senior Scientists , BARI

2016-06-05 Thread Sarah Goslee
The RClimDex website has instructions for obtaining and installing
this package, and for obtaining and installing R if you also need to
do that:
http://etccdi.pacificclimate.org/software.shtml

You must follow those directions, as the necessary first step is
obtaining a download password from the maintainers.

Sarah

On Sun, Jun 5, 2016 at 5:23 AM, Ranjit Sen via R-help
 wrote:
>
>
>
> Dear Sir:
> This is Dr Ranjit Sen, Senior Scientific Officer, Soil Science Division, 
> Bangladesh Agricultural Research Institute(BARI). For my research on climatic 
> variability and extreme events of climate of Bangladesh, I need to install 
> the  Rclimdex software from R in my computer.
> I would be highly grateful to you if you help me in this regard such as how 
> to get it as well as how to install in my computer.
> With thank and very best regards.
>
> Sincerely,Dr Ranjit Sen, Senior Scientific Officer, Soil Science Division, 
> Bangladesh Agricultural Research Institute(BARI).
>
>

__
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] Fw: i need to install "Rclimdex"........Senior Scientists , BARI

2016-06-05 Thread Ranjit Sen via R-help


   
Dear Sir:
This is Dr Ranjit Sen, Senior Scientific Officer, Soil Science Division, 
Bangladesh Agricultural Research Institute(BARI). For my research on climatic 
variability and extreme events of climate of Bangladesh, I need to install the  
Rclimdex software from R in my computer. 
I would be highly grateful to you if you help me in this regard such as how to 
get it as well as how to install in my computer.
With thank and very best regards.

Sincerely,Dr Ranjit Sen, Senior Scientific Officer, Soil Science Division, 
Bangladesh Agricultural Research Institute(BARI).


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

[R] Reading and converting time data via read.table

2016-06-05 Thread Ek Esawi
Hi All--



I am relatively new to R. I am reading a csv file via read.table (MyFile).
The data types in the file are date, string, integer, and time. I was able
to read all the data and manipulated correctly except time, e.g., 12:30. I
used as.Date to convert date and string and integer were easily done. I
could not figure out how to convert the time data correctly. I tried chron
but w/o success and I read that POSIXlt and POSIXct work only for date and
time (e.g. 01/02/1999, 12:30:20). I did not try the lubridate package. Is
there a way to read time data without date attached to it like mine?



I am grateful for any help and thanks in advance—EKE



Here is an example of my data when read into R via read.table



AA  Date Name T1  T2
N1

1  312171  7/1/1995   OF  13:37  1:43 123

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

[R] Fw: i need to install "Rclimdex"........Senior Scientists , BARI

2016-06-05 Thread Ranjit Sen via R-help


 
- Forwarded Message -
 From: Ranjit Sen 
 To: "c...@r-project.org"  
 Sent: Sunday, June 5, 2016 2:24 PM
 Subject: i need to install "Rclimdex"Senior Scientists ,BARI
   
Dear Sir:This is Dr Ranjit Sen, Senior Scientific Officer, Soil Science 
Division, Bangladesh Agricultural Research Institute(BARI). For my research on 
climatic variability and extreme events of climate of Bangladesh, I need to 
install the  Rclimdex software from R in my computer. 
I would be highly grateful to you if you help me in this regard such as how to 
get it as well as how to install in my computer.
With thank and very best regards.

Sincerely,Dr Ranjit Sen, Senior Scientific Officer, Soil Science Division, 
Bangladesh Agricultural Research Institute(BARI).


  
[[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] system() command not working

2016-06-05 Thread Berend Hasselman

> On 4 Jun 2016, at 22:12, Roy Mendelssohn - NOAA Federal 
>  wrote:
> 
> Hi John:
> 
> When El Capitan first came out there was a discussion in the  R-SIg-Mac  list 
> about environmental variables not being passed down to applications  (not 
> just R abut in general).  I believe a work around was suggested, but I would 
> search the archives for that.
> 
> So what is happening, when you run from the command line, the variables   
> MRT_DATA_DIR and MRTDATADIR which are defined somewhere in your environment 
> are found in the terminal, but when the same command is run from the 
> application they are not being found.  I would search the R-SIg-Mac archive 
> or post to that list, because i can’t remember what the work around was for 
> it.
> 

I can't find the discussion on R-SIG-Mac list. But you can try this:

MRT_DATA_DIR= open -a Rstudio

or  

MRT_DATA_DIR= open -a R

Try it and see what happens.
It may even be possible to put something in .Rprofile  setting your environment 
variables.

Berend Hasselman

> HTH,
> 
> -Roy
> 
>> On Jun 4, 2016, at 11:59 AM, J Payne  wrote:
>> 
>> I’ve posted this question on StackExchange at 
>> http://stackoverflow.com/questions/37604466/r-system-not-working-with-modis-reprojection-tool,
>>  but haven’t received any replies.  I’m hoping that someone who understands 
>> the operation of the R system() command can help.  
>> 
>> 
>> 
>> I have a command that works when typed into the Terminal on a Mac (OSX El 
>> Cap), but exactly the same command fails when called from R using 
>> `system()`. 
>> 
>> 
>> 
>> I am trying to batch-process MODIS satellite files using a small program 
>> called the MODIS Reprojection Tool 
>> (https://lpdaac.usgs.gov/tools/modis_reprojection_tool).  My software is all 
>> up to date.
>> 
>> 
>> 
>> This is a simple example in which I mosaic two files.  The names of the two 
>> files are in a text input file called `input.list`.  The command just tells 
>> the `mrtmosaic` routine where to find the input list and where to put the 
>> output. 
>> 
>> 
>> 
>> This command works correctly in the Terminal:  
>> 
>> 
>> 
>>/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list 
>> -o ~/temp/output.hdf
>> 
>> 
>> 
>> However, if I put exactly the same string into a variable and run it from R 
>> (using RStudio), it fails:  
>> 
>> 
>> 
>>comstring<-"/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i 
>> ~/temp/input.list -o ~/temp/output.hdf"  
>> 
>>system(comstring)
>> 
>> 
>> 
>>> Warning: gctp_call : Environmental Variable Not Found:   
>> 
>>MRT_DATA_DIR nor MRTDATADIR not defined
>> 
>>Error: GetInputGeoCornerMosaic : General Processing Error converting 
>> lat/long coordinates to input projection coordinates.  
>> 
>>Fatal Error, Terminating...
>> 
>> 
>> 
>> The strange thing is that the system knows what the environment variables 
>> are.  In the terminal, the command
>> 
>> `echo $MRT_DATA_DIR`
>> 
>> shows the correct directory: /Applications/Modis_Reprojection_Tool/data
>> 
>> 
>> 
>> I don't see why it would have trouble finding the variables from an `R 
>> system()` call when it has no trouble in the Terminal.  I'm very stumped!  
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>  [[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.
> 
> **
> "The contents of this message do not reflect any position of the U.S. 
> Government or NOAA."
> **
> Roy Mendelssohn
> Supervisory Operations Research Analyst
> NOAA/NMFS
> Environmental Research Division
> Southwest Fisheries Science Center
> ***Note new address and phone***
> 110 Shaffer Road
> Santa Cruz, CA 95060
> Phone: (831)-420-3666
> Fax: (831) 420-3980
> e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
> 
> "Old age and treachery will overcome youth and skill."
> "From those who have been given much, much will be expected" 
> "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
> 
> __
> 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.