[R] tcltk table properties

2015-10-01 Thread Dan D
I have a tkwidget table (say, tbl1) that may be reconfigured at various times
depending on user input. Is there an easy way to later extract table
properties? Something like...

nrow<-tkgetproperties(tbl1, rows)

Muchas thanks in advance.

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/tcltk-table-properties-tp4713045.html
Sent from the R help mailing list archive at Nabble.com.

__
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] vector manipulations -- differences

2015-09-21 Thread Dan D
I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x
as:

c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1])

x is increasing with x[1] = 0. 

The following works but is not the greatest:
junk<-outer(x, x, '-')
junk[junk>0]

e.g., 
given
x<-c(0, 3, 7, 20)
junk<-outer(x, x, '-')
junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go
through 
junk
# [,1] [,2] [,3] [,4]
#[1,]0   -3   -7  -20
#[2,]30   -4  -17
#[3,]740  -13
#[4,]   20   17   130

Anyone have a better idea?

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html
Sent from the R help mailing list archive at Nabble.com.

__
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] c(1:n, 1:(n-1), 1:(n-2), ... , 1)

2015-09-17 Thread Dan D
Can anyone think of a slick way to create an array that looks like c(1:n,
1:(n-1), 1:(n-2), ... , 1)?

The following works, but it's inefficient and a little hard to follow:
n<-5
junk<-array(1:n,dim=c(n,n))
junk[((lower.tri(t(junk),diag=T)))[n:1,]]

Any help would be greatly appreciated!

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/c-1-n-1-n-1-1-n-2-1-tp4712390.html
Sent from the R help mailing list archive at Nabble.com.

__
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] c(1:n, 1:(n-1), 1:(n-2), ... , 1)

2015-09-17 Thread Dan D
Very nice variety of solutions to create c(1:n, 1:(n-1), 1:(n-2), ... , 1)

#Testing the methods with n=1000 (microbenchmark)
n<-1000

# by far the nicest-looking, easiest to follow, and fastest is Frank
Schwidom's:
# it also requires the minimum amount of memory (as do several of the
others)
# 2.73 milliseconds (1x)
sequence(n:1)  

# not nearly as nice-looking but almost as fast:
# 2.82 milliseconds (1.03x)
do.call(c, lapply(n:1, function(n1) 1:n1)) 

# an improvement on look but 5x slower than do.call is:
# 13.3 milliseconds  (4.9x)
unlist(lapply(n:1, seq))

## the others are uglier and way slower [uses a full (n+1) x (n+1) matrix]
# 60.8 milliseconds (22.3x)
outer( 1:(n+1), 1:(n+1), '-')[ outer( 1:n, 1:(n+1), '>')]

# 71.8 milliseconds (26.3x) [uses a full (n x n) matrix]
junk<-array(1:n,dim=c(n,n))
junk[((lower.tri(t(junk),diag=T)))[n:1,]]

# 421.3 milliseconds (154x)
Reduce( function(x,y){c( 1:y, x)}, 1:n)

# 3200 milliseconds (1170x)
cc<-0; # establish result as numeric
for(i in seq(n,1,-1)){ cc<-c(cc,seq(1,i)); str(cc); }; #generate array
cc<-cc[2:length(cc)]; #remove the leading 0
} # 

# crashes:
mklist <- function(n) { 
 if (n==1) return(1) else return( c(seq(1,n),mklist(n-1)) ) 
}



--
View this message in context: 
http://r.789695.n4.nabble.com/c-1-n-1-n-1-1-n-2-1-tp4712390p4712399.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Help with vectors!

2015-09-09 Thread Dan D
VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black")
c(factor(VAS)) # to give integer indexing to the colors

---
This is very nice, Frank. And it can be easily adjusted to match the
original criterion that the numbers match the order of appearance of the
colors in the original vector: 

c(factor(VAS,levels=unique(VAS)))




--
View this message in context: 
http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4712061.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Help with vectors!

2015-09-08 Thread Dan D
Great!



--
View this message in context: 
http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4712023.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Counting number of rain

2015-09-08 Thread Dan D
Try the following:

## step 1: write raw data to an array
junk<-scan('clipboard') 
# entering the numbers (not the 'year' etc. labels) into R as a vector after

junk<-t(array(junk,dim=c(4,length(junk)/4))) 
# convert the vector into a 2-d array with 4 columns (year, month, day,
amount)

## step 2: create a dataframe to store and display the results
nyr<-length(unique(junk[,1]))
ans<-data.frame(array(dim=c(nyr,12))) # a dataframe for storing the results
names(ans)<-c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
yrs<-sort(unique(junk[,1]))
row.names(ans)<-yrs

# step 3: calculate
for (yi in 1:nyr){ # loop through the years...
  for (mi in 1:12){ # ...and the months
 ans[yi,mi]<-sum(junk[junk[,1]==yrs[yi] & junk[,2]==mi,4]>0.01) #
count the rainy days by
 # first subsetting the junk array by rows that match the given year and
month and sum
  }
}

Does that help?

- Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/Counting-number-of-rain-tp4712007p4712011.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Help

2015-09-07 Thread Dan D
press ESC.

You may have entered a command that was missing a parenthesis or something
else that R needs before it can make sense out of your code (e.g., entering
"sum(X" without the closing paren will give you that pattern). ESC brings
back the command line and you can try again.

-Dan 



--
View this message in context: 
http://r.789695.n4.nabble.com/Help-tp4711952p4711960.html
Sent from the R help mailing list archive at Nabble.com.

__
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] extracting every nth character from a string...

2015-09-07 Thread Dan D
# or:
strsplit("junk",split=NULL)[[1]][(1:nchar("junk"))%%2==1]
strsplit("junk",split=NULL)[[1]][(1:nchar("junk"))%%2==0]





--
View this message in context: 
http://r.789695.n4.nabble.com/extracting-every-nth-character-from-a-string-tp4711908p4711962.html
Sent from the R help mailing list archive at Nabble.com.

__
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] extracting every nth character from a string...

2015-09-07 Thread Dan D
# as a complete function
StrSubset<-function(junk,n){
   ifelse(n==1,junk,
paste(strsplit(junk,split=NULL)[[1]][(1:nchar(junk))%%n==1],collapse=''))
}




--
View this message in context: 
http://r.789695.n4.nabble.com/extracting-every-nth-character-from-a-string-tp4711908p4711963.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Handling "NA" in summation

2015-09-06 Thread Dan D
# it's not clear what your question is, but here's a stab in the dark at a
solution!

ind<- !is.na(dataframe$A) & !is.na(dataframe$B)
dataframe$A[ind] + dataframe$B[ind] 

- Dan

P.S. I'm sure there are ways to do this using one of R's functions for
automatically removing NA's (na.rm = T), but unless you use them all the
time, their behavior is not always predictable (e.g., sometimes it ignores a
whole row with one NA in it; sometimes it treats NAs as zeros). Explicitly
defining  the indices that you want to exclude may make it easier to avoid
difficult-to-find errors.
 



--
View this message in context: 
http://r.789695.n4.nabble.com/Handling-NA-in-summation-tp4711923p4711932.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Plots Help

2015-09-05 Thread Dan D
length(post) is 1 (i.e., it is just a single function), but length(post(t))
== length(t)



--
View this message in context: 
http://r.789695.n4.nabble.com/Plots-Help-tp4711894p4711897.html
Sent from the R help mailing list archive at Nabble.com.

__
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] For Loops please help!!

2015-09-05 Thread Dan D
The code has an error so it won't run as written. 

Instead of:
infectrate[n]= (400)(1.1)^(n); 

try:
infectrate[n]= 400*1.1^n;

What I get after making this change looks right.



--
View this message in context: 
http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711884.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Help with vectors!

2015-09-05 Thread Dan D
# your data
VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black")

# declare the new vector
New_Vector<-numeric(length(VAS))

# brute force:
New_Vector[VAS=="White"]<-1
New_Vector[VAS=="Yellow"]<-2
New_Vector[VAS=="Green"]<-3
New_Vector[VAS=="Black"]<-4

# a little more subtle
cols<-c("White","Yellow","Green","Black")
for (i in 1:length(cols))  New_Vector[VAS==cols[i]]<-i

# and a general approach (that may give a different indexing, but can be
used for any array)
for (i in 1:length(unique(VAS))) New_Vector[VAS==unique(VAS)[i]]<-i
cbind(1:length(unique(VAS)),unique(VAS)) # a decoding key for the color
index




--
View this message in context: 
http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4711895.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Plots Help

2015-09-05 Thread Dan D
As written, the code does not run because you are trying to plot post vs. t.
Try instead:
plot(t, post(t)), or, more simply, plot(t, dbeta(t,1.05,30))

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/Plots-Help-tp4711894p4711896.html
Sent from the R help mailing list archive at Nabble.com.

__
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] For Loops please help!!

2015-09-05 Thread Dan D
Also, any time you write "for" in R, you pay a steep price in performance. In
a short, simple loop it may not be noticeable, but in a more challenging
problem it can be a huge issue.

A more efficient way to write your loop would be:
infectrate = 400*1.1^(1:30) # calculation
cbind(1:30,log(infectrate))# display

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711885.html
Sent from the R help mailing list archive at Nabble.com.

__
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] For Loops please help!!

2015-09-05 Thread Dan D
Yes, the cause is memory use patterns, but the price is steep nonetheless.

E.g.:

rate<-log(400*1.1^(1:30)) # runs about 27x times as fast as the following
(test via 'microbenchmark') 

rate<-numeric(30)
for (i in 1:30){
   rate[i]<-log(400*1.1^i)
}

When manipulating large arrays, the difference can easily be a few seconds
vs. an hour or more. And if many such arrays need to be run, the difference
is between "difficult" and "not feasible". 

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711887.html
Sent from the R help mailing list archive at Nabble.com.

__
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] [Bayesian Methods] Riemann Sums for Posterior expected loss

2015-09-05 Thread Dan D
Does this get you started?

f<-function(t,cx){
  (abs(t-cx)*(t >= cx)+10*abs(t-cx)*(t < cx))*dbeta(t,1.05,30)
}

integrate(f,lower=0,upper=1,cx=0.4)$val # e.g., for c = 0.4

The "integrate" function integrates over the first parameter. Other
parameters can be entered as needed.

[Note: I used cx as the parameter name rather than c because 'c' has a
special meaning in R.]

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/Bayesian-Methods-Riemann-Sums-for-Posterior-expected-loss-tp4711900p4711901.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Gamma count

2015-09-04 Thread Dan D
Pick the mean (mu) and variance (sig2) you want. Then, shape = mu^2/sig2 and
scale = sig2/mu. This should work fine if your mean is large enough so that
p(x = 0 or 1) is small.



--
View this message in context: 
http://r.789695.n4.nabble.com/Gamma-count-tp4711845p4711853.html
Sent from the R help mailing list archive at Nabble.com.

__
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] file.show cleanup

2015-09-03 Thread Dan D
The file.show() function seems to be exactly what I'm needing for displaying
file contents for users, but I need something like "file.close()" to close
the "R Information" window to clean up afterwards. Does anyone know if there
is such a thing?

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/file-show-cleanup-tp4711824.html
Sent from the R help mailing list archive at Nabble.com.

__
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] edit GUI preferences for current session?

2015-09-03 Thread Dan D
I'd like to edit the GUI preferences for the current (Windows) session via
command line rather than the Edit|GUI preferences menu. In particular, is
there a way to change the pagerstyle to singlewindow without using the menu
or editing the RConsole file?

Any ideas?

Thanks!

-Dan



--
View this message in context: 
http://r.789695.n4.nabble.com/edit-GUI-preferences-for-current-session-tp4711825.html
Sent from the R help mailing list archive at Nabble.com.

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