[R] source files in temp environment

2017-12-02 Thread Alexander Shenkin

Hi all,

I often keep code in separate files for organizational purposes, and 
source() that code from higher level scripts.  One problem is that those 
sourced files often create temporary variables that I don't want to keep 
around.  I could clean up after myself with lots of rm()'s, but that's a 
pain, and is messy.


I'm wondering if one solution might be to source the code in a temporary 
environment, assign outputs of interest to the .GlobalEnv with <<-, and 
then delete the environment afterwards.  One way to do this:


file.r:
temp1 = 1
temp2 = 2
desired_var <<- temp1 + temp2

console:
temp_e = new.env()
source("file.r", local = temp_e)
rm(temp_e)

It's a bit messy to create and delete environments, so I tried what 
others have referred to:


source("file.r", local = attach(NULL))

This, however, results in a persistent "NULL" environment in the search 
path.


> search()
".GlobalEnv""package:bindrcpp"  "NULL"
"tools:rstudio" "package:stats" "package:graphics" 
"package:grDevices" "package:utils" "package:datasets"

"package:methods"   "Autoloads" "package:base"

Of course, functions are built to encapsulate like this (and do so in 
their own temporary environment), but in many cases, turning the sourced 
code into functions is possible but clunky.


Any thoughts or suggestions would be much appreciated.

Thanks,
Allie

__
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] To implement OO or not in R package, and if so, how to structure it?

2017-09-14 Thread Alexander Shenkin

Did you read this?
https://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf

Maybe it could give you some insight in how to create package.


That resource is ~9 years old. There are more modern treatments available. You
can read mine at http://r-pkgs.had.co.nz.

Hadley



Thanks both.  I'm reading through your new book now Hadley... thanks for 
that.  I'll probably take a shot at building a class to hold one tree 
per object, and search for objects of a class (per 
https://stackoverflow.com/questions/5158830/identify-all-objects-of-given-clas-for-further-processing) 
to implement collections when necessary...


It does seem like there might be niche out there for a resource for 
folks deciding how to structure their package given what they're trying 
to provide; i.e. should they construct a collection of functions, or 
class defs, or...  Could well exist already, and I may just have missed 
it...


Thanks,
Allie

__
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] To implement OO or not in R package, and if so, how to structure it?

2017-09-14 Thread Alexander Shenkin

Hello all,

I am trying to decide how to structure an R package.  Specifically, do I 
use OO classes, or just provide functions?  If the former, how should I 
structure the objects in relation to the type of data the package is 
intended to manage?


I have searched for, but haven't found, resources that guide one in the 
*decision* about whether to implement OO frameworks or not in one's R 
package.  I suspect I should, but the utility of the package would be 
aided by *collections* of objects.  R, however, doesn't seem to 
implement collections.


Background: I am writing an R package that will provide a framework for 
analyzing structural models of trees (as in trees made of wood, not 
statistical trees).  These models are generated from laser scanning 
instruments and model fitting algorithms, and hence may have aspects 
that are data-heavy.  Furthermore, coputing metrics based on these 
structures can be computationally heavy.  Finally, as a result, each 
tree has a number of metrics associated with it (which may be expensive 
to calculate), along with the underlying data of that tree.  It will be 
important as well to perform calculations across many of these trees, as 
one would do in a dataframe.


This last point is important: if one organizes data across potentially 
thousands of objects, how easy or hard is it to massage properties of 
those objects into a dataframe for analysis?


Thank you in advance for thoughts and pointers.

Allie

__
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] Calculating cumulative lengths of hierarchical topology

2017-07-03 Thread Alexander Shenkin

Hello All,

I need to calculate cumulative lengths along a hierarchical network 
topology (it's a representation of a tree).  I can roll my own, but 
thought that there might be a package out there that would handle 
hierarchical network topology functions such as this nicely (and that 
might bring along other useful functions as well).  However, I haven't 
found one after searching.


I did find data.tree, which looks quite interesting, but it carries a 
large overhead that doesn't seem quite appropriate for the relatively 
simple structure I'm dealing with.


Could anyone point me to such a package if one does exist?

Thanks very much,
Allie


Example of the hierarchical topology:

 len parent_row daughter_row index_num
1 1.7053  02 1
2 1.1653  13 2
3 1.1980  24 3
4 1.1462  35 4
5 1.1346  46 5
6 1.1024  57 6
...

__
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] Maximum # of DLLs reached, or, how to clean up after yourself?

2016-09-14 Thread Alexander Shenkin

Hi Henrik,

Thanks for your reply.  I didn't realize that floating DLLs were an 
issue (good to know).  My query is actually a bit more basic.  I'm 
actually wondering how folks manage their loading and unloading of 
packages when calling scripts within scripts.


Quick example:
Script1:
library(package1)
source("script2.r")
# do stuff reliant on package1
detach("package:package1", unload=TRUE)

Script2:
library(package1)
library(package2)
# do stuff reliant on package1 and package2
detach("package:package1", unload=TRUE)
detach("package:package2", unload=TRUE)

Script2 breaks Script1 by unloading package1 (though unloading package2 
is ok).  I will have to test whether each package is loaded in Script2 
before loading it, and use that list when unloading at the end of the 
Script2.  *Unless there's a better way to do it* (which is my question - 
is there?).  I'm possibly just pushing the whole procedural scripting 
thing too far, but I also think that this likely isn't uncommon in R.


Any thoughts greatly appreciated!

Thanks,
Allie

On 9/13/2016 7:23 PM, Henrik Bengtsson wrote:

In R.utils (>= 2.4.0), which I hope to submitted to CRAN today or
tomorrow, you can simply call:

   R.utils::gcDLLs()

It will look at base::getLoadedDLLs() and its content and compare to
loadedNamespaces() and unregister any "stray" DLLs that remain after
corresponding packages have been unloaded.

Until the new version is on CRAN, you can install it via

source("http://callr.org/install#HenrikBengtsson/R.utils@develop";)

or alternatively just source() the source file:


source("https://raw.githubusercontent.com/HenrikBengtsson/R.utils/develop/R/gcDLLs.R";)


DISCUSSION:
(this might be better suited for R-devel; feel free to move it there)

As far as I understand the problem, running into this error / limit is
_not_ the fault of the user.  Instead, I'd argue that it is the
responsibility of package developers to make sure to unregister any
registered DLLs of theirs when the package is unloaded.  A developer
can do this by adding the following to their package:

.onUnload <- function(libpath) {
library.dynam.unload(utils::packageName(), libpath)
 }

That should be all - then the DLL will be unloaded as soon as the
package is unloaded.

I would like to suggest that 'R CMD check' would include a check that
asserts when a package is unloaded it does not leave any registered
DLLs behind, e.g.

* checking whether the namespace can be unloaded cleanly ... WARNING
  Unloading the namespace does not unload DLL
* checking loading without being on the library search path ... OK

For further details on my thoughts on this, see
https://github.com/HenrikBengtsson/Wishlist-for-R/issues/29.

Hope this helps

Henrik

On Tue, Sep 13, 2016 at 6:05 AM, Alexander Shenkin  wrote:

Hello all,

I have a number of analyses that call bunches of sub-scripts, and in the
end, I get the "maximal number of DLLs reached" error.  This has been asked
before (e.g.
http://stackoverflow.com/questions/36974206/r-maximal-number-of-dlls-reached),
and the general answer is, "just clean up after yourself".

Assuming there are no plans to raise this 100-DLL limit in the near future,
my question becomes, what is best practice for cleaning up (detaching)
loaded packages in scripts, when those scripts are sometimes called from
other scripts?  One can detach all packages at the end of a script that were
loaded at the beginning of the script.  However, if a package is required in
a calling script, one should really make sure it hadn't been loaded prior to
sub-script invocation before detaching it.

I could write a custom function that pushes and pops package names from a
global list, in order to keep track, but maybe there's a better way out
there...

Thanks for any thoughts.

Allie

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Maximum # of DLLs reached, or, how to clean up after yourself?

2016-09-13 Thread Alexander Shenkin

Hello all,

I have a number of analyses that call bunches of sub-scripts, and in the 
end, I get the "maximal number of DLLs reached" error.  This has been 
asked before (e.g. 
http://stackoverflow.com/questions/36974206/r-maximal-number-of-dlls-reached), 
and the general answer is, "just clean up after yourself".


Assuming there are no plans to raise this 100-DLL limit in the near 
future, my question becomes, what is best practice for cleaning up 
(detaching) loaded packages in scripts, when those scripts are sometimes 
called from other scripts?  One can detach all packages at the end of a 
script that were loaded at the beginning of the script.  However, if a 
package is required in a calling script, one should really make sure it 
hadn't been loaded prior to sub-script invocation before detaching it.


I could write a custom function that pushes and pops package names from 
a global list, in order to keep track, but maybe there's a better way 
out there...


Thanks for any thoughts.

Allie

__
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] Factors in nlme: singularity in backsolve error

2016-07-19 Thread Alexander Shenkin

Hello all,

I tried posting on r-sig-ME, but it's not going through.  So I thought I 
would try here.  Apologies if this ends up being a cross-post if it 
eventually goes through there


I am parameterizing exponential fits for some metabolic scaling models. 
I have done this in lmer already, without problem, using logged 
dependent and independent variables.  However, I would now like to 
incorporate other parameters that aren't necessarily exponentially 
related to the dependent variable.  Hence, I've turned to nlme, but I 
don't have much experience with it.  Apologies in advance for newbie 
mistakes.


With the code below, I am getting the following error.  I'm guessing 
that it has something to do with the 'site' factor being misspecified:


Error in nlme.formula(dep ~ scaling_fun(alpha, beta, ind, site), data = 
scale_df,  :

  Singularity in backsolve at level 0, block 1

When I fit a simpler function that does not involve 'site', the model 
seems to work correctly.


Any thoughts would be greatly appreciated!  I've added the dput object 
at the bottom of the email to avoid distraction...


Thanks,
Allie


> head(scale_df)
  depind spp site
2  0.28069471 -0.0322841 157A
3 -0.69719050 -1.2568901 183A
4  0.29252012  0.1592420 246A
5  0.72030740 -0.3282789 154A
6 -0.08601891  0.3623756 110A
7  0.30793594  0.2230840 154A


scaling_fun <- function(alpha, beta, ind, site) {
return(beta + ind^alpha + site*(ind^alpha))
}

# results in singularity in backsolve error

nlme(dep ~ trait_scaling_fun(alpha, beta, ind, site),
 data = scale_df,
 fixed = list(alpha + beta + site ~ 1), random = alpha ~ 1|spp,
 start = list(fixed = c(0.7, 0, 1)))


##
# simpler function converges #
##

scaling_fun <- function(alpha, beta, ind) {
return(beta + ind^alpha)
}

nlme(dep ~ scaling_fun(alpha, beta, ind),
 data = scale_df,
 fixed = list(alpha + beta ~ 1), random = alpha ~ 1|spp,
 start = list(fixed = c(0.7, 0)))


# dput for data

scale_df = structure(list(dep = structure(c(0.280694709349609, 
-0.697190504779316,

0.29252012378561, 0.720307403285465, -0.0860189064012942, 0.307935942585907,
0.862281389721553, -0.265859454130292, -0.662753116661401, 
-0.281590648203109,

1.17791119955669, 0.631729125553481, -0.556750637034717, -0.43297610538,
0.78495323332528, 0.355622302250686, -1.26351337875593, 0.343598303486811,
-1.17350365646135, 0.660405635460007, -0.0444581036277384, 
0.722773887452235,
-0.0522058979055465, 0.65594868736652, -0.471798559774723, 
0.580581816047275,
0.514408011465886, -0.736138172672309, 0.70774857676606, 
-0.0810467855114514,

2.87816317587089, -0.22475032263, -0.497119013269447, 0.031618943500335,
1.93466683745019, 1.0320275291612, 0.874248439252774, -0.327339718896358,
-0.854016320390115, -1.14806351731618, -0.380316794335449, 
0.298626272822636,
-0.600084009068698, 0.836832381353764, -1.02141473458357, 
-0.0626961141428343,
-1.28013982373819, -0.126310567347687, -0.998914167194147, 
-0.966356030272785,

-0.0311625866367497, -0.456212263484603, 2.22345644904786, 1.06808914728274,
2.04676618838053, 2.57057502230498, -1.1354565673953, -0.617861267279932,
-1.29433182429494, 0.0191424880072437, -0.31539529665173, 
-0.161941249027648,

-0.582100497448459, 1.62041284412996, 4.12468548155128, 0.181382553309611,
0.395970527486673, 0.949165316445473, 0.536508784640814, 1.88628468601066,
0.460108411105217, 1.50237201805536, -0.248693954731227, 0.604338152411222,
-0.511271387322619, -0.273625967472597, 2.54906943158448, 1.25797640582933,
-0.615499666866161, -0.83022966646613, -0.569798846726899, 1.41607035018281,
0.806876783115155, 3.03500743372039, 0.371311069097266, 0.605523223251687,
0.960528362908707, 0.536545286280021, -1.07783545300095, 0.145069857506269,
-0.390394315762374, -0.524314770428875, -0.205267560118326, 2.8197556390129,
0.548519073742583, 2.25991992849428, -0.274997856331803, -0.748248845217783,
1.27303941400619, 0.23884797601163, -0.582228991340883, -0.380185805876727,
-0.0858712197617979, -0.181441660791883, -0.122600427784157,
1.44111920784939, 0.163470634515893, -0.322754741867578, -1.08355296391174,
-0.27943230825155, -0.367498411981987, -1.25029544932326, 1.96800896885018,
0.741382953698137, 0.525448033325593, 0.58486584900894, -1.39293857038504,
0.0401527810688047, -0.428292900882259, -0.384773565802791, 
-0.347537789997483,

0.259461537066455, 1.04953062690743, 0.905652281045827, 0.921052829745329,
0.52450347921673, 1.71595979136317, -0.474652211889487, 0.986966207710898,
-0.256654313404628, 2.99291703386829, -0.874752003654369, 1.01178420482112,
5.77381325416311, -0.167933435302975, -0.746365585369451, 2.80467857407177,
1.95866740218257, -0.503053147492697, -0.0160574809046825, 
0.0121332489691898,

0.155574669334708, 0.916506565829058, -0.448074058381429, 0.316834873904141,
1.04981848557227, -0.824609748750358, 1.06744876285501, 0.063863664699137,
-

[R] remove fixed effect in predict.merMod

2016-05-19 Thread Alexander Shenkin

Hello all,

I've run a model, and now would like to extract residuals.  However, I 
have set sum-to-zero contrasts for the categorical fixed effect 
(contr.sum).  Because I am interested in looking at the variation in the 
residuals associated with that fixed effect (along with other levels), I 
need to calculate residuals setting that fixed effect to zero.  Any 
thoughts on how to do this?


thanks,
allie



contr.sum.keepnames <- function(...) {
# make deviation contrasts that don't lose the names of the factors 
in the model results
# from 
https://stackoverflow.com/questions/10808853/why-does-changing-contrast-type-change-row-labels-in-r-lm-summary

conS <- contr.sum(...)
colnames(conS) = rownames(conS)[-length(rownames(conS))]
conS
}

test_df = data.frame(site = rep(LETTERS[1:10], 10), resp = runif(100), 
pred = runif(100))


contrasts(test_df$site) = contr.sum.keepnames(levels(test_df$site))

mod = lmer(resp ~ (1 + pred|site) + pred, data = test_df)

residuals = test_df$resp - predict(mod, REform=NA) # I would like the 
site effect here to be set to zero


__
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] Fit a smooth closed shape through 4 points

2016-03-21 Thread Alexander Shenkin
Thanks for your reply, Charles.  spline() doesn't seem to fit a closed 
shape; rather, it's producing a parabola.  Perhaps I'm missing an 
argument I should include?


grid.xspline() seems to get close to what I need, but it returns a grob 
object - not sure how to work with those as shapes per se.


My goal is to produce a 2D shape from which I can calculate area, 
average widths, and other such things.  The context is that we have 
measured tree crowns in a manner that has produced 4 points such as 
these from two offset axes.  We want to use the resulting shapes for our 
calculations.


(incidentally, my original points were off - here are the correct ones)

shapepoints = structure(list(x = c(8.9, 0, -7.7, 0, 8.9), y = c(0, 2, 0, 
-3.8,

0)), .Names = c("x", "y"), row.names = c(NA, -5L), class = "data.frame")

plot(spline(shapepoints))

Thanks,
Allie

On 3/21/2016 1:10 PM, Charles Determan wrote:

Hi Allie,

What is you goal here?  Do you just want to plot a curve to the data?
Do you want a function to approximate the data?

You may find the functions spline() and splinefun() useful.

Quick point though, with so few points you are only going to get a very
rough approximation no matter the method used.

Regards,
Charles


On Mon, Mar 21, 2016 at 7:59 AM, Alexander Shenkin mailto:ashen...@ufl.edu>> wrote:

Hello all,

I have sets of 4 x/y points through which I would like to fit
closed, smoothed shapes that go through those 4 points exactly.
smooth.spline doesn't like my data, since there are only 3 unique x
points, and even then, i'm not sure smooth.spline likes making
closed shapes.

Might anyone else have suggestions for fitting algorithms I could
employ?

Thanks,
Allie


shapepoints = structure(c(8.9, 0, -7.7, 0, 0, 2, 0, 3.8), .Dim = c(4L,
2L), .Dimnames = list(NULL, c("x", "y")))

smooth.spline(shapepoints)

# repeat the first point to close the shape
shapepoints = rbind(shapepoints, shapepoints[1,])

smooth.spline(shapepoints)

__
R-help@r-project.org <mailto:R-help@r-project.org> mailing list --
To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Fit a smooth closed shape through 4 points

2016-03-21 Thread Alexander Shenkin

Hello all,

I have sets of 4 x/y points through which I would like to fit closed, 
smoothed shapes that go through those 4 points exactly.  smooth.spline 
doesn't like my data, since there are only 3 unique x points, and even 
then, i'm not sure smooth.spline likes making closed shapes.


Might anyone else have suggestions for fitting algorithms I could employ?

Thanks,
Allie


shapepoints = structure(c(8.9, 0, -7.7, 0, 0, 2, 0, 3.8), .Dim = c(4L,
2L), .Dimnames = list(NULL, c("x", "y")))

smooth.spline(shapepoints)

# repeat the first point to close the shape
shapepoints = rbind(shapepoints, shapepoints[1,])

smooth.spline(shapepoints)

__
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] ggplot: geom_bar not respecting factor order when stacking

2016-02-04 Thread Alexander Shenkin

Hi all,

ggplot2 (v2.0.0) does not seem to respect factor order when stacking 
bars in geom_bar().


##
> dput(temp)
structure(list(phylo_sig = c(0.148740270638472, 0.148740270638472,
0.148740270638472, 0.148740270638472, 0.148740270638472), trait = 
c("p_corrected_percent",

"p_corrected_percent", "p_corrected_percent", "p_corrected_percent",
"p_corrected_percent"), value = c(0.031015586683513, 0.0553330412480036,
0.73674222087599, 0.114517508485538, 0.0623916427069555), varlevel = 
structure(c(3L,

2L, 4L, 5L, 1L), .Label = c("species", "genus", "family", "plot_code",
"Residual"), class = "factor")), .Names = c("phylo_sig", "trait",
"value", "varlevel"), row.names = c(28L, 62L, 96L, 130L, 164L
), class = "data.frame")
> str(temp)
'data.frame':5 obs. of  4 variables:
 $ phylo_sig: num  0.149 0.149 0.149 0.149 0.149
 $ trait: chr  "p_corrected_percent" "p_corrected_percent" 
"p_corrected_percent" "p_corrected_percent" ...

 $ value: num  0.031 0.0553 0.7367 0.1145 0.0624
 $ varlevel : Factor w/ 5 levels "species","genus",..: 3 2 4 5 1
> ggplot(temp, aes(x=trait, y = value)) + geom_bar(stat = "identity", 
aes(fill=varlevel, order = varlevel))

##

The code above results in the correct legend order, but incorrect order 
of stacking (https://i.imgur.com/lgAxTFh.png).  I have to change the 
order of the actual dataframe rows in order to coerce the stacking to work:


##
> temp = temp[c(5,2,1,3,4),]
> ggplot(temp, aes(x=trait, y = value)) + geom_bar(stat = "identity", 
aes(fill=varlevel, order = varlevel))

##

This results in correct stacking (https://i.imgur.com/baww43x.png).  Any 
assistance in getting ggplot to pay attention to the factor ordering 
when stacking bars would be much appreciated!


Thanks,
Allie

__
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] apply function across dataframe columns for non-exclusive groups

2015-10-21 Thread Alexander Shenkin

Hello all,

I've been banging my head over what must be a simple solution.  I would 
like to apply a function across columns of a dataframe for rows grouped 
across different columns.  These groups are not exclusive.  See below 
for an example.  Happy to use dplyr, data.table, or whatever.  Any 
guidance appreciated!


Thanks,
Allie


desired algorithm: calculate a/(a+b) for each TRUE and FALSE grouping of 
columns grp1 and grp2.


this_df = data.frame(a = c(1,2,3,4,5), b = c(7,8,9,10,11), grp1 = 
c(T,T,F,F,F), grp2 = c(F,T,F,T,F))


desired output (doesn't have to be exactly this format, but something 
along these lines):


grp1 T 0.166
grp1 F 0.286
grp2 T 0.25
grp2 F 0.25

__
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] Quantifying widths of polygons

2015-09-25 Thread Alexander Shenkin

Hello all,

I am working with data on tree crowns, and this data describes points 
(verticies) around the polyhedron of the crown volume (think of the 
crown as a single volume with vertices and faces).  I can calculate 
maximum z distance between any 2 points (maximum depth) and maximum x/y 
distance (maximum width).  These are useful metrics.  I would also like 
to quantify an "average" width of the polygon in 2D space (x/y only), as 
well as a metric that would describe the "eccentricity" of the polygon. 
 But, I'm not sure how to go about doing that.


In general, I've made the polyhedrons and polygons into convex shapes.

I have considered getting a centroid, intersecting lines every 10 
degrees (for example) going through the centroid with the x/y polygon 
owin in spatstat, and then analyzing those line lengths.  But, I'm not 
sure that's the right way to go, and maybe there are already tools out 
there to do this.  Any thoughts anyone might have would be very welcome!


Thanks,
Allie


library(rgl)
library(spatstat)
library(geometry)

x = 
c(1.9,-1.4,1.5,1.8,2.2,0.2,0.6,-0.9,-3.7,1.3,-1.9,-3.4,3.7,2.1,-2.0,-1.9)
y = 
c(-3.1,3.0,1.1,-1.3,1.0,0.0,1.4,1.6,2.3,-3.6,-1.5,-1.3,0.3,-2.1,0.2,-0.3)

z = c(5.5,4.5,4.3,4.8,6.7,5.8,7.4,6.2,3.5,2.9,4.0,3.7,3.2,3.0,3.1,8.4)
depth = max(z) - min(z)
width_max = max(dist(matrix(c(x,y),ncol=2)))

xy_win = owin(poly=list(x=x,y=y))
conv_win = convexhull(xy_win)
# from here, maybe draw lines every 10 degrees through a centroid?
# avg_width = ??
# eccentricity = ??

# 3D plot of crown polyhedron (convex)
ps = data.frame(x=x,y=y,z=z)
crown.surf = t(convhulln(matrix(c(x,y,z),ncol=3)))
open3d()
rgl.triangles(ps[crown.surf,1],ps[crown.surf,2],ps[crown.surf,3],col=heat.colors(nrow(ps)),alpha=.2)
axes3d()

__
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] return named list from foreach

2015-02-20 Thread Alexander Shenkin
Hello all,

I've been trying to figure out how to return a named list from foreach.  
Given that the order of the returned list is guaranteed to be in the 
order in which the object is passed to foreach, list members can be 
named afterwards.  However, I'm wondering if there's a better way to do 
it, perhaps with some sort of combine function?

library(doParallel)
library(foreach)

cl <- makeCluster(4)
registerDoParallel(cl)

df = data.frame(nm = letters[11:20], a = 1:10, b=11:20)

out = foreach(i=1:nrow(df)) %dopar% {
 a = list(j = sqrt(df[i,]$a), k = sqrt(df[i,]$b))
 a
}

How do I name the elements of "out" using the corresponding values df$nm?

thanks,
allie

[[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] expected arguments for rgl.triangles

2014-11-14 Thread Alexander Shenkin

Hello all,

I have a set of points in 3D space that represent vertices of a 
non-convex polyhedron.  I would like to plot this polyhedron, and have 
been trying to do so with rgl.triangles, but to no avail.  I imagine I 
don't understand what rgl.triangles expects for arguments.  I have 
constructed the triangles myself, and have the three vertices of each 
triangle.  I am then passing these to rgl.triangles in the x,y & z 
arguments.


I end up with unexpected results (see http://i.imgur.com/BaKfzn7.png). 
Might someone advise me about the structure of arguments that 
rgl.triangles expects (or perhaps a better way to go about this)? 
?rgl.triangles doesn't help much with this particular issue...


Thanks,
Allie


The code, where rows 2,3,4 are one triangle, 5,6,7 the next, etc 
(apologies - don't know how to serialize a dataframe to make it easy to 
suck into one's environment):


rgl.triangles(tri_df[,1],tri_df[,2],tri_df[,3],col=heat.colors(nrow(tri_df)),alpha=.2)

> tri_df
x  y   z
2  -2.9970624 -0.1327280 8.0
3  -1.3358202  3.8762849 4.9
4   3.5380065  0.6652143 9.1
5  -1.3358202  3.8762849 4.9
6   1.4644874  3.6145922 5.3
7   3.5380065  0.6652143 9.1
8   1.4644874  3.6145922 5.3
9   1.9606671 -3.8269811 4.5
10  3.5380065  0.6652143 9.1
11  1.9606671 -3.8269811 4.5
12 -3.5337687  3.6772923 3.9
13  3.5380065  0.6652143 9.1
14 -3.5337687  3.6772923 3.9
15 -5.6586500  1.2726665 3.5
16  3.5380065  0.6652143 9.1
17 -5.6586500  1.2726665 3.5
18  3.8327817 -2.8895994 3.4
19  3.5380065  0.6652143 9.1
20  3.8327817 -2.8895994 3.4
21  6.0806691 -0.4852461 2.2
22  3.5380065  0.6652143 9.1
23  6.0806691 -0.4852461 2.2
24 -0.5109254 -6.7807784 0.5
25  3.5380065  0.6652143 9.1
26 -0.5109254 -6.7807784 0.5
27  2.3503422 -6.2742244 1.8
28  3.5380065  0.6652143 9.1
29  2.3503422 -6.2742244 1.8
30  5.4741865 -1.1803740 0.3
31  3.5380065  0.6652143 9.1
32  5.4741865 -1.1803740 0.3
33  5.712 -0.3589635 2.7
34  3.5380065  0.6652143 9.1
35  5.712 -0.3589635 2.7
36 -3.1939122 -2.4080958 5.4
37  3.5380065  0.6652143 9.1
38 -3.1939122 -2.4080958 5.4
39 -1.4732303 -3.9331403 4.9
40  3.5380065  0.6652143 9.1
41 -1.4732303 -3.9331403 4.9
42 -3.4863074  0.3092904 6.8
43  3.5380065  0.6652143 9.1
44 -3.4863074  0.3092904 6.8
45  0.8019969 -2.2620347 7.2
46  3.5380065  0.6652143 9.1
47  0.8019969 -2.2620347 7.2
48 -2.9970624 -0.1327280 8.0
49  3.5380065  0.6652143 9.1
50 -1.3358202  3.8762849 4.9
51 -2.9970624 -0.1327280 8.0
52  6.4508128 -2.4488802 0.3
53  1.4644874  3.6145922 5.3
54 -1.3358202  3.8762849 4.9
55  6.4508128 -2.4488802 0.3
56  1.9606671 -3.8269811 4.5
57  1.4644874  3.6145922 5.3
58  6.4508128 -2.4488802 0.3
59 -3.5337687  3.6772923 3.9
60  1.9606671 -3.8269811 4.5
61  6.4508128 -2.4488802 0.3
62 -5.6586500  1.2726665 3.5
63 -3.5337687  3.6772923 3.9
64  6.4508128 -2.4488802 0.3
65  3.8327817 -2.8895994 3.4
66 -5.6586500  1.2726665 3.5
67  6.4508128 -2.4488802 0.3
68  6.0806691 -0.4852461 2.2
69  3.8327817 -2.8895994 3.4
70  6.4508128 -2.4488802 0.3
71 -0.5109254 -6.7807784 0.5
72  6.0806691 -0.4852461 2.2
73  6.4508128 -2.4488802 0.3
74  2.3503422 -6.2742244 1.8
75 -0.5109254 -6.7807784 0.5
76  6.4508128 -2.4488802 0.3
77  5.4741865 -1.1803740 0.3
78  2.3503422 -6.2742244 1.8
79  6.4508128 -2.4488802 0.3
80  5.712 -0.3589635 2.7
81  5.4741865 -1.1803740 0.3
82  6.4508128 -2.4488802 0.3
83 -3.1939122 -2.4080958 5.4
84  5.712 -0.3589635 2.7
85  6.4508128 -2.4488802 0.3
86 -1.4732303 -3.9331403 4.9
87 -3.1939122 -2.4080958 5.4
88  6.4508128 -2.4488802 0.3
89 -3.4863074  0.3092904 6.8
90 -1.4732303 -3.9331403 4.9
91  6.4508128 -2.4488802 0.3
92  0.8019969 -2.2620347 7.2
93 -3.4863074  0.3092904 6.8
94  6.4508128 -2.4488802 0.3
95 -2.9970624 -0.1327280 8.0
96  0.8019969 -2.2620347 7.2
97  6.4508128 -2.4488802 0.3

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] dimensions of polyhedrons

2014-06-10 Thread Alexander Shenkin

Hello all,

I have sets of points in (x,y,z) that represent polyhedron vertices 
(actually tree crowns).  I have used convhulln to estimate volumes (the 
shapes are not necessarily convex, but good enough for now). Now I would 
like to extract various dimensions of these shapes - horizontal width 
(max, mean), vertical depth (max, mean), and other metrics.


Are there any packages/tools in R that might help me do this?  A look 
around didn't come up with anything.


Thanks in advance,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to define and calculate volume of 3D polygon?

2014-05-19 Thread Alexander Shenkin

Hi Folks,

I have a set of x,y,z points in 3D space that defines the outline of a 
tree crown (5 - 15 perimeter points + 1 top & bottom point). I would 
like to calculate the volume of the corresponding 3D polygon based on 
those points.  I have been able to use geometry::convhulln, but I think 
this isn't the right algorithm, since the polygon needs to pass through 
each point in order, and not just snap a rubber-band around the outer 
points.


Any suggestions for alternative algorithms and packages would be most 
welcome!


Thanks,
Allie

   seq  x  yz
11 -1.7310065  2.4502278 11.1
22 -1.9048260 -0.6096212  9.0
33  2.8652209  0.8891057 11.0
44  2.3929514 -3.4516349 11.2
55 -2.6436343  5.2745803 11.7
66 -4.3521504  4.6924180 11.7
77  6.1441732 -4.8051156 11.6
88 -6.2157823 -3.2193244  8.0
99  1.0024961 -2.8275434  9.0
10  10 -3.9656821  3.0452201  8.0
11  11  4.2070518 -1.5970958 11.0
12  12 -0.9461915  1.2902409  9.8
13  13  2.2792265  0.7517491 11.4

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Finding files matching full path regex

2014-02-27 Thread Alexander Shenkin
Hi folks,

I'm interested in finding files by matching both filenames and 
directories via regex.  If I have:

dir1_pat1/file1.csv
dir2_pat1/file2.csv
dir2_pat1/file3.txt
dir3_pat2/file4.csv

I would like to find, for example, all csv files in directories that 
have "pat1" in their name:

dir1_pat1/file1.csv
dir2_pat1/file2.csv

 > list.files(path = ".", pattern = ".*pat1/.*\\.csv", recursive = T)
character(0)
 > list.files(path = ".", pattern = ".*pat1/.*\\.csv", recursive = T, 
full.names=T)
character(0)
 > list.files(path = ".", pattern = ".*\\.csv", recursive = T, full.names=T)
[1] "./dir1_pat1/file1.csv" "./dir2_pat1/file2.csv" "./dir3_pat2/file4.csv"
 > list.files(path = ".", pattern = "pat1", recursive = T, full.names=T)
character(0)

I think list.files just runs the regex pattern against the file names, 
not the full path.  I tried full.names=T, but it still matches against 
the file name only.

Suggestions are greatly appreciated.

Thank you,
Allie

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to vectorize plot graphic?

2013-12-01 Thread Alexander Shenkin
Thanks Jeff.  I'm not sure to which manual you are referring.  Nothing
like what you mention is found here:
http://cran.r-project.org/manuals.html .  Perhaps you're referring to
The Data Import/Export Manual, but that wouldn't make sense.  Googling
"R Input/Output manual" doesn't help.

Regarding the device, I'm in R-Studio, plotting to the standard device
there (I suppose).  In any case, I'm not totally sure what the interface
between R-Studio's plot window and the R instance is.  I think that, as
you say, what I'm getting is as expected.  I zoom and the plot zooms as
expected.  So, I guess that part is solved.  However, the writing to
.wmf as a bitmap instead of a vector is still a mystery to me.

Thanks,
Allie

On 12/1/2013 12:29 PM, Jeff Newmiller wrote:
> You have mentioned nothing about the device you are writing the plot to. If 
> to the default and you are copying it from there as a bitmap, then what you 
> are describing sounds as expected. Read the R Input/Output manual (again) for 
> other output options.
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> ----------- 
> Sent from my phone. Please excuse my brevity.
> 
> Alexander Shenkin  wrote:
>> Hi Folks,
>>
>> Using ggplot, I've produced the following graphic:
>> http://i.imgur.com/39a139C.png
>>
>> The graphics in the plot seem to be bitmapped and not vectorized.  That
>> is, the vertical and horizontal lines jump rows of pixels instead of
>> having just nice, angled lines.  Any thoughts about how to get these
>> graphics vectorized?  Or am I misunderstanding something?
>>
>> Another example:
>>
>> The code:
>>
>>require(ggplot2)
>> df = data.frame(x = c(1:360), y = sin(seq(0,2*pi*3,length.out = 360)))
>>ggplot(df, aes(x=x, y=y)) + geom_line()
>>
>> produces http://i.imgur.com/mjjSKih.png
>>
>> Perhaps what I'm dealing with here is my screen resolution.  However, I
>> use ggsave() to save .wmf files, and those also turn out to be bitmaps
>> and not vectors.
>>
>> Thanks,
>> Allie
>>
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to vectorize plot graphic?

2013-12-01 Thread Alexander Shenkin
Thanks Carl.  Yeah, I can get vectorized graphics out via
ggsave("temp.pdf") too.  But I need to add the graphic to a word doc on
windows.  Hence the desire to use wmf, which should induce a vector
format (shouldn't it?).

On 12/1/2013 10:39 AM, Carl Witthoft wrote:
> Off the top of my head,  I'd suggest trying ggsave()  with the extension
> ".svg" .  I realize that SVG files are not recognized by some image display
> apps (Microsoft Windows I'm looking at YOU), but IMHO it's the best choice 
> for vectorized images.
> 
> 
> Alexander Shenkin wrote
>> Hi Folks,
>>
>> Using ggplot, I've produced the following graphic:
>> http://i.imgur.com/39a139C.png
>>
>> The graphics in the plot seem to be bitmapped and not vectorized.  That
>> is, the vertical and horizontal lines jump rows of pixels instead of
>> having just nice, angled lines.  Any thoughts about how to get these
>> graphics vectorized?  Or am I misunderstanding something?
>>
>> Another example:
>>
>> The code:
>>
>> require(ggplot2)
>> df = data.frame(x = c(1:360), y = sin(seq(0,2*pi*3,length.out = 360)))
>> ggplot(df, aes(x=x, y=y)) + geom_line()
>>
>> produces http://i.imgur.com/mjjSKih.png
>>
>> Perhaps what I'm dealing with here is my screen resolution.  However, I
>> use ggsave() to save .wmf files, and those also turn out to be bitmaps
>> and not vectors.
>>
>> Thanks,
>> Allie
>>
>>  [[alternative HTML version deleted]]
>>
>> __
> 
>> R-help@
> 
>>  mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/How-to-vectorize-plot-graphic-tp4681424p4681427.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to vectorize plot graphic?

2013-12-01 Thread Alexander Shenkin
Hi Folks,

Using ggplot, I've produced the following graphic:
http://i.imgur.com/39a139C.png

The graphics in the plot seem to be bitmapped and not vectorized.  That
is, the vertical and horizontal lines jump rows of pixels instead of
having just nice, angled lines.  Any thoughts about how to get these
graphics vectorized?  Or am I misunderstanding something?

Another example:

The code:

require(ggplot2)
df = data.frame(x = c(1:360), y = sin(seq(0,2*pi*3,length.out = 360)))
ggplot(df, aes(x=x, y=y)) + geom_line()

produces http://i.imgur.com/mjjSKih.png

Perhaps what I'm dealing with here is my screen resolution.  However, I
use ggsave() to save .wmf files, and those also turn out to be bitmaps
and not vectors.

Thanks,
Allie

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] devtools: rtools not compatible with 3.0.1

2013-06-11 Thread Alexander Shenkin
Hi Folks,

I'm trying to load devtools in R 3.0.1 in order to run the dev version
of lme4.  I've updated devtools, and just installed Rtools30.exe. 
However, I get the following warning (in R-Studio, RGui, and R.exe, both
x64 and i386):

-
WARNING: Rtools is required to build R packages, but no version of
Rtools compatible with R 3.0.1 was found. (Only the following
incompatible version(s) of Rtools were found:3.0)

Please download and install the appropriate version of Rtools from
http://cran.r-project.org/bin/windows/Rtools/ and then run find_rtools().
-

Any ideas?  Rtools30.exe is the most recent that I can find.

Thanks,
Allie

R 3.0.1
Win 7 Pro x64 SP1

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] exporting tables to word

2013-04-02 Thread Alexander Shenkin
Thanks Carlos,

ftable has some internal routines, it seems, that allow it to print out
a flat contigency table in a nice format.  Converting to data.frame ends
up messing up the display format, unfortunately.

thanks,
allie

On 4/2/2013 12:41 PM, Carlos Valenzuela wrote:
> You  may also want to consider the "rtf" package. It works okay. You would
> have to save your results as a dataframe and you would be able to write
> that object out to an .rtf (or even use an .doc extension, as I have seen
> it done).
>
> For example,
>
> library(rtf)
>
> # Your regression formula
> lmobj<-lm(dv~iv1 + iv2 + iv3, data=dat)
>
> lmresults<-as.data.frame(summary(lmobj)$coefficients)
>
> # Output location for .rtf file
> output<-"filepath/nameoffile.rtf"
>
> # Specify the width, height, font size, etc and margins
> rtfdoc<-RTF(output,width=8.5,height=11,font.size=10,omi=c(1,1,1,1))
>
> addHeader(rtf,title="Title of Table, subtitle="Subtitle")
> addTable(rtfdoc, lmresults, font.size=10, row.names=T, NA.string="-")
> done(rtf)
>
> This is a very crude example so I would suggest looking at the help files
> associated with the "rtf" package to make any modifications, such as adding
> other regression results/tables.
>
> Thanks and hope this helps,
>
> Carlos
>
>
> On Tue, Apr 2, 2013 at 12:13 PM, Alexander Shenkin  wrote:
>
>> Thanks Frans,
>>
>> odfTable seems not to like ftable objects.  any thoughts?
>>
>> Error:  chunk 1 (label = damageTable)
>> Error in UseMethod("odfTable") :
>>   no applicable method for 'odfTable' applied to an object of class
>> "ftable"
>>
>>
>> thanks,
>> allie
>>
>> On 4/2/2013 8:09 AM, Frans Marcelissen wrote:
>>> Hi Allie,
>>> My preferred method is by means of odfWeave. Odfweave generates a
>>> libreoffice file, which can directly be transferred to word.
>>> A second way is run R from within spss. Spss has a much better output
>> than
>>> R, but you can use the output system of spss (by means of
>>> spsspivottable.Display())
>>> Frans
>>>
>>>
>>> 2013/4/2 Alexander Shenkin 
>>>
>>>> Hello all,
>>>>
>>>> What is your preferred method to export tables (from ftable()) to Word?
>>>> Do you just export to a text file and then copy/paste?  Or perhaps via
>>>> HTML output?
>>>>
>>>> Thanks,
>>>> Allie
>>>>
>>>> __
>>>> R-help@r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] exporting tables to word

2013-04-02 Thread Alexander Shenkin
Thanks Frans,

odfTable seems not to like ftable objects.  any thoughts?

Error:  chunk 1 (label = damageTable) 
Error in UseMethod("odfTable") : 
  no applicable method for 'odfTable' applied to an object of class "ftable"


thanks,
allie

On 4/2/2013 8:09 AM, Frans Marcelissen wrote:
> Hi Allie,
> My preferred method is by means of odfWeave. Odfweave generates a
> libreoffice file, which can directly be transferred to word.
> A second way is run R from within spss. Spss has a much better output than
> R, but you can use the output system of spss (by means of
> spsspivottable.Display())
> Frans
>
>
> 2013/4/2 Alexander Shenkin 
>
>> Hello all,
>>
>> What is your preferred method to export tables (from ftable()) to Word?
>> Do you just export to a text file and then copy/paste?  Or perhaps via
>> HTML output?
>>
>> Thanks,
>> Allie
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] exporting tables to word

2013-04-02 Thread Alexander Shenkin
Hello all,

What is your preferred method to export tables (from ftable()) to Word? 
Do you just export to a text file and then copy/paste?  Or perhaps via
HTML output?

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] summarize dataframe based on multiple cols, not their combinations

2013-03-20 Thread Alexander Shenkin
Thanks, John.  Your solution gives me:

> ddply(my_df, .(a), summarize, mm = mean(dat), number = length(dat))
  a mm number
1 0 14  3
2 1 11  3

I'm looking for (and Ista found a way):

>>   a b c mean n
>> 1 1 * *   11 3
>> 2 * 1 *   14 3
>> 3 * * 1   12 3

thanks,
allie


On 3/20/2013 3:24 PM, John Kane wrote:
> Will this do?
> 
> library(plyr)
>   
>   ddply(my_df, .(a), summarize, mm = mean(dat), number = length(dat))
> 
> John Kane
> Kingston ON Canada
> 
> 
>> -Original Message-
>> From: ashen...@ufl.edu
>> Sent: Wed, 20 Mar 2013 14:57:36 -0500
>> To: r-help@r-project.org
>> Subject: [R] summarize dataframe based on multiple cols, not their
>> combinations
>>
>> Hi folks,
>>
>> I'm trying to figure out how to get summarized data based on multiple
>> columns.  However, instead of giving summaries for every combination of
>> categorical columns, I want it for each value of each categorical column
>> regardless of the other columns.  I could do this with three different
>> commands, but i'm wondering if there's a more elegant way that I'm
>> missing.  Thanks!
>>
>> allie
>>
>>> my_df = data.frame(a = c(1,1,1,0,0,0), b=c(0,0,0,1,1,1),
>> c=c(1,0,1,0,1,0), dat=c(10,11,12,13,14,15))
>>
>>> my_df
>>   a b c dat
>> 1 1 0 1  10
>> 2 1 0 0  11
>> 3 1 0 1  12
>> 4 0 1 0  13
>> 5 0 1 1  14
>> 6 0 1 0  15
>>
>>> # not what I want
>>> ddply(my_df, .(a,b,c), function(x) c("mean"=mean(x$dat), "n"=nrow(x)))
>>   a b c mean n
>> 1 0 1 0   14 2
>> 2 0 1 1   14 1
>> 3 1 0 0   11 1
>> 4 1 0 1   11 2
>>
>> What I want:
>>   a b c mean n
>> 1 1 * *   11 3
>> 2 * 1 *   14 3
>> 3 * * 1   12 3
>>
>> where "*" refers to any value of the other columns.
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
> Check it out at http://www.inbox.com/earth
> 
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] summarize dataframe based on multiple cols, not their combinations

2013-03-20 Thread Alexander Shenkin
Nice, thanks Ista!

On 3/20/2013 3:18 PM, Ista Zahn wrote:
> How about
> 
> library(reshape2)
> mdf.m <- melt(my_df, measure.vars=c("a", "b", "c"))
> mdf.m <- mdf.m[mdf.m$value > 0, ]
> 
> ddply(mdf.m, "variable", function(x) c("mean"=mean(x$dat), "n"=nrow(x)))
> 
> ?
> 
> Best,
> Ista
> 
> On Wed, Mar 20, 2013 at 3:57 PM, Alexander Shenkin  wrote:
>> Hi folks,
>>
>> I'm trying to figure out how to get summarized data based on multiple
>> columns.  However, instead of giving summaries for every combination of
>> categorical columns, I want it for each value of each categorical column
>> regardless of the other columns.  I could do this with three different
>> commands, but i'm wondering if there's a more elegant way that I'm
>> missing.  Thanks!
>>
>> allie
>>
>>> my_df = data.frame(a = c(1,1,1,0,0,0), b=c(0,0,0,1,1,1),
>> c=c(1,0,1,0,1,0), dat=c(10,11,12,13,14,15))
>>
>>> my_df
>>   a b c dat
>> 1 1 0 1  10
>> 2 1 0 0  11
>> 3 1 0 1  12
>> 4 0 1 0  13
>> 5 0 1 1  14
>> 6 0 1 0  15
>>
>>> # not what I want
>>> ddply(my_df, .(a,b,c), function(x) c("mean"=mean(x$dat), "n"=nrow(x)))
>>   a b c mean n
>> 1 0 1 0   14 2
>> 2 0 1 1   14 1
>> 3 1 0 0   11 1
>> 4 1 0 1   11 2
>>
>> What I want:
>>   a b c mean n
>> 1 1 * *   11 3
>> 2 * 1 *   14 3
>> 3 * * 1   12 3
>>
>> where "*" refers to any value of the other columns.
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] summarize dataframe based on multiple cols, not their combinations

2013-03-20 Thread Alexander Shenkin
Hi folks,

I'm trying to figure out how to get summarized data based on multiple
columns.  However, instead of giving summaries for every combination of
categorical columns, I want it for each value of each categorical column
regardless of the other columns.  I could do this with three different
commands, but i'm wondering if there's a more elegant way that I'm
missing.  Thanks!

allie

> my_df = data.frame(a = c(1,1,1,0,0,0), b=c(0,0,0,1,1,1),
c=c(1,0,1,0,1,0), dat=c(10,11,12,13,14,15))

> my_df
  a b c dat
1 1 0 1  10
2 1 0 0  11
3 1 0 1  12
4 0 1 0  13
5 0 1 1  14
6 0 1 0  15

> # not what I want
> ddply(my_df, .(a,b,c), function(x) c("mean"=mean(x$dat), "n"=nrow(x)))
  a b c mean n
1 0 1 0   14 2
2 0 1 1   14 1
3 1 0 0   11 1
4 1 0 1   11 2

What I want:
  a b c mean n
1 1 * *   11 3
2 * 1 *   14 3
3 * * 1   12 3

where "*" refers to any value of the other columns.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] extract indep vars from formula

2012-11-07 Thread Alexander Shenkin
Hello,

I'm trying to extract the independent variables from a formula.  The
closest I've been able to come, aside from rolling my own, is the following:

> a = y ~ b * x
> attr(terms(formula(a)),"variables")

The reason I'm doing this is that I'm building a grid of points that I
use to construct a 3-d model prediction surface in rgl.  If there are
more than two independent variables, I need to set the other ones to 0
in the dataframe for the predict() routine.  In order to do that, I need
to know what those variables are.

I suspect there's a better way to go about doing what I'm doing (perhaps
using the model.* family of functions).  But, in the meantime, just
extracting the independent variables will move me forward.

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] backreferences in gregexpr

2012-11-03 Thread Alexander Shenkin
On 11/2/2012 5:14 PM, Gabor Grothendieck wrote:
> On Fri, Nov 2, 2012 at 6:02 PM, Alexander Shenkin  wrote:
>> Hi Folks,
>>
>> I'm trying to extract just the backreferences from a regex.
>>
>>> temp = "abcd1234abcd1234"
>>> regmatches(temp, gregexpr("(?:abcd)(1234)", temp))
>> [[1]]
>> [1] "abcd1234" "abcd1234"
>>
>> What I would like is:
>> [1] "1234" "1234"
>>
>> Note: I know I can just match 1234 here, but the actual example is
>> complicated enough that I have to match a larger string, and just want
>> to pass out the backreferenced portion.
>>
>> Any help greatly appreciated!
>>
> 
> Try this:
> 
>> library(gsubfn)
>> strapplyc(temp, "abcd(1234)")
> [[1]]
> [1] "1234" "1234"
> 

Thanks Gabor.  Didn't find strapplyc in package gsubfun, but did find
strapply, and that worked well.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] backreferences in gregexpr

2012-11-02 Thread Alexander Shenkin
Hi Folks,

I'm trying to extract just the backreferences from a regex.

> temp = "abcd1234abcd1234"
> regmatches(temp, gregexpr("(?:abcd)(1234)", temp))
[[1]]
[1] "abcd1234" "abcd1234"

What I would like is:
[1] "1234" "1234"

Note: I know I can just match 1234 here, but the actual example is
complicated enough that I have to match a larger string, and just want
to pass out the backreferenced portion.

Any help greatly appreciated!

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Which packages are incompatible with 64-bit R?

2012-10-19 Thread Alexander Shenkin
Thanks Duncan.  Here's what I come up with.  Anything obviously missing?
 I'd think there'd be more incompatibilities than just these few...

In 32-bit: > pkgs32 = available.packages(type="win.binary")
In 64-bit: > pkgs64 = available.packages(type="win.binary")

> pkgnames32 = pkgs32[,"Package"]
> pkgnames64 = pkgs64[,"Package"]

> as.character(pkgnames32[which(is.na(match(pkgnames32, pkgnames64)))])
[1] "RSVGTipsDevice" "RSvgDevice" "eco""rcqp"
"sparsenet"  "hdf5"


On 10/19/2012 8:22 AM, Duncan Murdoch wrote:
> On 19/10/2012 8:10 AM, Alexander Shenkin wrote:
>> Hi folks,
>>
>> Despite the pain of migrating to 64-bit R (I have to install 64-bit
>> Office also due to RODBC), I'm considering making the leap due to
>> memory issues. Is there any place that lists packages that are 64-bit
>> incompatible? Or, will I just have to march through all my packages
>> and check them one-by-one on CRAN?
> 
> An approximation to that is to call
> available.packages(type="win.binary") on both versions, and compare the
> lists.  Packages may fail to appear for reasons other than
> incompatibility, but incompatible packages won't be available in binary
> versions.
> 
> Duncan Murdoch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Which packages are incompatible with 64-bit R?

2012-10-19 Thread Alexander Shenkin
Hi folks, 

Despite the pain of migrating to 64-bit R (I have to install 64-bit Office also 
due to RODBC), I'm considering making the leap due to memory issues. Is there 
any place that lists packages that are 64-bit incompatible? Or, will I just 
have to march through all my packages and check them one-by-one on CRAN?

Thanks,
Allie



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bigmemory for dataframes?

2012-10-18 Thread Alexander Shenkin
System Info:
R 2.14.2
Windows 7 Pro x64 SP1
8GB RAM

On 10/18/2012 3:42 PM, Alexander Shenkin wrote:
> Hi Folks,
> 
> I've been bumping my head against the 4GB limit for 32-bit R.  I can't
> go to 64-bit R due to package compatibility issues (ROBDC - possible but
> painful, xlsReadWrite - not possible, and others).  I have a number of
> big dataframes whose columns all sorts of data types - factor,
> character, integer, etc.  I run and save models that keep copies of the
> modeled data inside the model objects as well (mle2 objects, to be
> specific).
> 
> I'm searching for a way to cache some of these dataframes and objects to
> virtual memory (I think I'm using the right terminology...).  I've read
> around, and while bigmemory and ff and the like would likely suit my
> purposes if I were just dealing with numeric matricies, I'm dealing with
> dataframes and objects.
> 
> Any thoughts would be greatly appreciated!
> 
> Thanks,
> Allie
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] bigmemory for dataframes?

2012-10-18 Thread Alexander Shenkin
Hi Folks,

I've been bumping my head against the 4GB limit for 32-bit R.  I can't
go to 64-bit R due to package compatibility issues (ROBDC - possible but
painful, xlsReadWrite - not possible, and others).  I have a number of
big dataframes whose columns all sorts of data types - factor,
character, integer, etc.  I run and save models that keep copies of the
modeled data inside the model objects as well (mle2 objects, to be
specific).

I'm searching for a way to cache some of these dataframes and objects to
virtual memory (I think I'm using the right terminology...).  I've read
around, and while bigmemory and ff and the like would likely suit my
purposes if I were just dealing with numeric matricies, I'm dealing with
dataframes and objects.

Any thoughts would be greatly appreciated!

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to know where you've been sourced from

2012-09-26 Thread Alexander Shenkin
Hello All,

I need a script to perform different actions depending on the file from
which it was source()'d.  Thus, my script needs a way to figure out from
whence it was sourced.

There seems to be some relevant information in sys.calls() (see below).
 However, I need to get at the name of the file that directly source the
file in question.  That is, just one level above.  For example, in the
example below, test.r needs to know that it was called by source_test.r.
 In this example, the information is in sys.calls()[[4]].  But, if there
are other calls in the stack, I don't imagine it will always be in
sys.calls()[[4]].  Is there a consistent place in sys.calls() or
elsewhere I can look for the sourcing file name?

thanks,
allie


> cat("print(sys.calls())",file="test.r")
> cat("source(\"test.r\")",file="source_test.r")
> cat("source(\"source_test.r\")",file="source_test_parent.r")
> source("source_test_parent.r")

[[1]]
source("source_test_parent.r")

[[2]]
eval.with.vis(ei, envir)

[[3]]
eval.with.vis(expr, envir, enclos)

[[4]]
source("source_test.r")

[[5]]
eval.with.vis(ei, envir)

[[6]]
eval.with.vis(expr, envir, enclos)

[[7]]
source("test.r")

[[8]]
eval.with.vis(ei, envir)

[[9]]
eval.with.vis(expr, envir, enclos)

[[10]]
print(sys.calls())

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] remove all terms with interaction factor in formula

2012-09-13 Thread Alexander Shenkin
Hi Folks,

I'm trying to find a way to remove all terms in a formula that contain a
particular interaction.

For example, in the formula below, I'd like to remove all terms that
contain the b:c interaction.

> attributes(terms( ~ a*b*c*d))$term.labels
 [1] "a"   "b"   "c"   "d"   "a:b" "a:c"
 [7] "b:c" "a:d" "b:d" "c:d" "a:b:c"   "a:b:d"
[13] "a:c:d"   "b:c:d"   "a:b:c:d"

My eventual use is to fit models with the reduced formulas.

For example:
> my_df = data.frame( iv = runif(100), a=runif(100), b=runif(100),
c=runif(100), d=runif(100))
> lm(iv ~ a*b*c*d, data=my_df)

I can remove particular terms with update(), but I don't see a way to
remove all terms that contain a given combination of factors.

Any help would be greatly appreciated.  Thanks!

Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] POSIXct-coerced NA's not considered NA by is.na()

2012-08-24 Thread Alexander Shenkin
Thanks for your reply, Jim.

On 8/24/2012 12:14 PM, jim holtman wrote:
> I think your first problem is the coersion done by 'c' when you are
> mixing objects of various types: you have POSIXct and character.

Yes, that's something I may have confounded. Still, the warning I'm
getting is "In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion".
 I'm not sure how c()'s coercion works - the warning seems to indicate
that c() is finding as.POSIXct.  That's strange though, since I would
expect to get an error in that case, not just a warning:

> as.POSIXct("b")
Error in as.POSIXlt.character(x, tz, ...) :
  character string is not in a standard unambiguous format

> What were your expections?

I was expecting that the NA resulting from the coercion would result in
a TRUE value when being operated on by is.na().  Instead, I got:

> is.na(date_vec[4])
[1] FALSE

>> x <- Sys.time()
>> str(x)
>  POSIXct[1:1], format: "2012-08-24 13:12:31"
>> y <- c(x, 'b')
>> str(y)
>  POSIXct[1:2], format: "2012-08-24 13:12:31" NA
> Warning message:
> In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
>> dput(y)
> structure(c("1345828351.75", "b"), class = c("POSIXct", "POSIXt"
> ))
>>
> 
> Look at the 'dput' and see that what it is trying to do is to use the
> numeric value changed to a character string as a POSIXct value.  So I
> am not surprised by the error since it is probably not what you
> wanted.  Did you intend to use 'list' instead of 'c'?

I'm a bit confused about how you get that from dput.  Here's what I see:

> dput(date_vec)
structure(c("1345831833", "1345831834", NA, "b"), class = c("POSIXct",
"POSIXt"))

Regardless, I do get the same strange is.na() behavior from the following:

> bad_date = "b"
> class(bad_date) = "POSIXct"
> bad_date
[1] NA
Warning message:
In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
> is.na(bad_date) # shouldn't this be TRUE?
[1] FALSE

As nasty as it may be, shouldn't something showing up as "NA" result in
TRUE when being tested by is.na()?

Just to put some context around this, I was investigating this issue as
I was thinking about converting dataframe columns to dates, and
detecting errors in that conversion.

thanks,
Allie

> 
> On Fri, Aug 24, 2012 at 9:47 AM, Alexander Shenkin  wrote:
>> Hello folks,
>>
>> I found a strangeness while experimenting with POSIXct vectors and
>> lists.  It seems that coerced NA's aren't "real" NAs, at least as
>> considered by is.na()?
>>
>>> date_vec = c(as.POSIXct(now()), as.POSIXct(now()+1),NA,"b")
>>> date_vec
>> [1] "2012-08-22 15:00:46 COT" "2012-08-22 15:00:47 COT" NA
>>
>> [4] NA
>> Warning message:
>> In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
>>> date_vec[4]
>> [1] NA
>> Warning message:
>> In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
>>> is.na(date_vec[4])
>> [1] FALSE
>>> is.na(date_vec[3])
>> [1] TRUE
>>> is.POSIXct(date_vec[4])
>> [1] TRUE

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] POSIXct-coerced NA's not considered NA by is.na()

2012-08-24 Thread Alexander Shenkin
Hello folks,

I found a strangeness while experimenting with POSIXct vectors and
lists.  It seems that coerced NA's aren't "real" NAs, at least as
considered by is.na()?

> date_vec = c(as.POSIXct(now()), as.POSIXct(now()+1),NA,"b")
> date_vec
[1] "2012-08-22 15:00:46 COT" "2012-08-22 15:00:47 COT" NA

[4] NA
Warning message:
In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
> date_vec[4]
[1] NA
Warning message:
In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
> is.na(date_vec[4])
[1] FALSE
> is.na(date_vec[3])
[1] TRUE
> is.POSIXct(date_vec[4])
[1] TRUE

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Sweave: R chunk inside caption?

2012-08-21 Thread Alexander Shenkin
Hi Folks,

I'm surprised, but I didn't find this question addressed anywhere.  I'd
like to generate a LaTeX caption with R code.  I've tried the code
below, but I get the following TeX error:

! Argument of \@caption has an extra }.

\par
l.21 }

Any thoughts?  Perhaps I'll have to write the "\caption{}" text with R?

thanks!


Sweave document:

\documentclass{article}
\title {test}
\author {me}
\usepackage{Sweave}
\begin {document}
\maketitle
\DeclareGraphicsExtensions{.pdf,.png}

\begin {figure}
<>=
plot(runif(100), runif(100))
@
\caption {
This is the caption with some r-code
<<>>=
2*2
@
}
\label {fig:1}
\end {figure}

\end{document}


TeX document:

\documentclass{article}
\title {test}
\author {me}
\usepackage{Sweave}
\begin {document}
\maketitle
\DeclareGraphicsExtensions{.pdf,.png}
\begin {figure}
\includegraphics{test-fig1}
\caption {
This is the caption with some r-code
\begin{Schunk}
\begin{Sinput}
> 2*2
\end{Sinput}
\begin{Soutput}
[1] 4
\end{Soutput}
\end{Schunk}
}
\label {fig:1}
\end {figure}
\end{document}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] export table in separate file

2012-08-10 Thread Alexander Shenkin
Hi Folks,

I'm using Sweave to generate png & pdf graphics that I then "Import &
Link" in a Word document.  This let's me create sharable and editable
dynamic documents.  They are dynamic in that I can regenerate figures
when the data changes, and have those figures automatically updated in
my Word doc since they're "linked", and not just "imported" in Word.

I want to do the same for tables.  Right now, I've rolled my own
function to turn tables into a png.  However, I would like a more robust
solution.  Is there any way I can turn tables into, say, an HTML table
in a separate file that could be linked in Word?  I know one can create
HTML tables in Sweave and otherwise.  The difference is that I need
those tables to exist each in their separate file.

Any thoughts would be much appreciated.

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] r-forge down?

2012-08-07 Thread Alexander Shenkin
Thanks Roy,

That's one "R-forge" site, but my impression is that the
r-forge.r-project.org site is a different one (and it's the one that
hosts the package I'm interested in).  Please correct me if those two
sites are somehow related.

thanks,
allie

On 8/7/2012 5:29 PM, Roy Mendelssohn wrote:
> http://www.rforge.net
>
> -Roy
>
> On Aug 7, 2012, at 3:25 PM, Alexander Shenkin wrote:
>
>> Hi Folks,
>>
>> I've looked around, haven't found anything, and I'm not sure where else
>> to check.  I haven't visited R-forge (http://r-forge.r-project.org) in a
>> long time.  Now that I'm trying, it seems to be down.  Anyone know if
>> this is a temporary condition, and if so, when it's expected to rise again?
>>
>> Thanks,
>> Allie
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> **
> "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
> 1352 Lighthouse Avenue
> Pacific Grove, CA 93950-2097
>
> e-mail: roy.mendelss...@noaa.gov (Note new e-mail address)
> voice: (831)-648-9029
> fax: (831)-648-8440
> 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
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] r-forge down?

2012-08-07 Thread Alexander Shenkin
Hi Folks,

I've looked around, haven't found anything, and I'm not sure where else
to check.  I haven't visited R-forge (http://r-forge.r-project.org) in a
long time.  Now that I'm trying, it seems to be down.  Anyone know if
this is a temporary condition, and if so, when it's expected to rise again?

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] sweave tables as images?

2012-05-25 Thread Alexander Shenkin
this works - thanks baptiste!  i'm working in Sweave right now - perhaps
it will be tough in knitr as you mention.

On 5/25/2012 4:31 PM, baptiste auguie wrote:
> you can open a device that has the exact dimensions of the table,
> 
> g = tableGrob(head(iris, 4))
> png("test.png", width=convertWidth(grobWidth(g), "in", value=TRUE),
> height=convertHeight(grobHeight(g), "in",
> value=TRUE),units="in", res=150)
> grid.draw(g)
> dev.off()
> 
> Doing this with knitr might be tricky though, since the unit
> conversion opens a blank device window, and you'd want to define some
> hook instead of manually creating the png file.
> 
> I have another version of grid.table where you can specify the width
> and height manually [*], e.g to span the full window, but it's not
> necessarily a desirable thing (the spacing between rows and columns
> can become too large).
> 
> 
> HTH,
> 
> baptiste
> 
> 
> [*] experimental code at https://gist.github.com/2013903
> 
> On 26 May 2012 09:16, Alexander Shenkin  wrote:
>> Thanks Yihui,
>>
>> That's a great idea, and comes close to the mark, except that I have to
>> use png's in order to "Insert & Link" them as pictures in Word (and
>> hence make the doc both shareable and update when new figures are
>> generated).
>>
>> thanks,
>> allie
>>
>> On 5/25/2012 2:57 PM, Yihui Xie wrote:
>>> You may take a look at knitr's graphics manual which tells you how you
>>> can automatically crop the white margins:
>>> https://github.com/downloads/yihui/knitr/knitr-graphics.pdf ("Cropping
>>> PDF Graphics").
>>>
>>> I'm not sure if pdfcrop works in this case, though.
>>>
>>> Regards,
>>> Yihui
>>> --
>>> Yihui Xie 
>>> Phone: 515-294-2465 Web: http://yihui.name
>>> Department of Statistics, Iowa State University
>>> 2215 Snedecor Hall, Ames, IA
>>>
>>>
>>> On Fri, May 25, 2012 at 2:44 PM, Alexander Shenkin  wrote:
>>>> grid.table() works well, but using it in sweave creates graphics with
>>>> very wide margins.  I'm sure this has something to do with grid, and not
>>>> just grid.table.  Any idea how I can clip the graphic to the edges of
>>>> the table graphic?  I've looked into viewports, etc, but I can't seem to
>>>> find anything that will clip a graphic to its edges, perhaps with some
>>>> defined margin.
>>>>
>>>> any help greatly appreciated!
>>>>
>>>> thanks,
>>>> allie
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] sweave tables as images?

2012-05-25 Thread Alexander Shenkin
Thanks Yihui,

That's a great idea, and comes close to the mark, except that I have to
use png's in order to "Insert & Link" them as pictures in Word (and
hence make the doc both shareable and update when new figures are
generated).

thanks,
allie

On 5/25/2012 2:57 PM, Yihui Xie wrote:
> You may take a look at knitr's graphics manual which tells you how you
> can automatically crop the white margins:
> https://github.com/downloads/yihui/knitr/knitr-graphics.pdf ("Cropping
> PDF Graphics").
> 
> I'm not sure if pdfcrop works in this case, though.
> 
> Regards,
> Yihui
> --
> Yihui Xie 
> Phone: 515-294-2465 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
> 
> 
> On Fri, May 25, 2012 at 2:44 PM, Alexander Shenkin  wrote:
>> grid.table() works well, but using it in sweave creates graphics with
>> very wide margins.  I'm sure this has something to do with grid, and not
>> just grid.table.  Any idea how I can clip the graphic to the edges of
>> the table graphic?  I've looked into viewports, etc, but I can't seem to
>> find anything that will clip a graphic to its edges, perhaps with some
>> defined margin.
>>
>> any help greatly appreciated!
>>
>> thanks,
>> allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] sweave tables as images?

2012-05-25 Thread Alexander Shenkin
grid.table() works well, but using it in sweave creates graphics with
very wide margins.  I'm sure this has something to do with grid, and not
just grid.table.  Any idea how I can clip the graphic to the edges of
the table graphic?  I've looked into viewports, etc, but I can't seem to
find anything that will clip a graphic to its edges, perhaps with some
defined margin.

any help greatly appreciated!

thanks,
allie

On 5/21/2012 3:33 PM, R. Michael Weylandt wrote:
> Take a look at addtable2plot in plotrix or grid.table / tableGrob in
> gridExtras.
> 
> Michael
> 
> On Mon, May 21, 2012 at 4:29 PM, Alexander Shenkin  wrote:
>> Hello folks,
>>
>> I've been on a journey trying to figure out how to manage documents that
>> are amenable to sharing and editing, but that contain dynamic content
>> generated by R.  I've come to the following solution: I use Sweave to
>> generate labeled png & pdf figures, and I "Insert & Link" those figures
>> as "Pictures" in a Word 2010 doc.  Thus, when data or code changes, I
>> regenerate the figures with Sweave, open the Word doc and hit "F9", and
>> all the figures are automatically updated.  I can send the file around,
>> folks can comment and edit, track changes, etc.
>>
>> Now, however, I'm trying to do the same for tables that I did for
>> figures, and it seems a bit more difficult.  I can spit out tex tables
>> into separate files using the "split=TRUE" chunk option, and I can even
>> make those tables into html with xtable(type="html").  Word, however,
>> doesn't have the "Insert & Link" option for external text files (which
>> makes it such that, if the external file isn't found, Word uses a copy
>> stored in the doc).
>>
>> So, I think it will be better if I can somehow generate the tables as
>> images.  Is there any way to generate tables as images in separate files
>> in Sweave?  Or, is there another tree up which I should be barking?
>>
>> Thanks,
>> Allie
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] use list as function arguments

2012-05-24 Thread Alexander Shenkin
Hello Folks,

Is there any way to pass a list into a function such that the function
will use the list as its arguments?  I haven't been able to figure that out.

The background: I'm trying to build a function that will apply another
function multiple times, each time with a different set of specified
arguments.  I'm trying to figure out how to pass in a list of argument
lists, then loop through the top-level list, passing in the lower-level
list as function arguments.

pseudocode:

b = list( list( arg1 = 1, arg2 = 2 ),
  list( arg1 = 3, arg2 = 4 )
)

a <- apply_function(arglist) {
for (i in length(arglist)) {
b(arglist[i])
}
}


Specifically, the actual use I'm trying to implement is a function to
format columns of data frames and matrices independently.  What I have
so far is below, but it's not working.  Perhaps I'm going about this the
wrong way?


format_cols <- function(x, format_list = list()) {
# usage: length(format_list) must equal ncol(x)
#format list should be a list of lists of key=value pairs
corresponding to format settings for each column

if (is.data.frame(x)) {
newout = data.frame()
} else if (is.matrix(x)) {
newout = matrix()
}

for (i in 1:ncol(x)){
newout = cbind(newout, format(x,format_list[[i]]))
x[,i] = format(x,format_list[[i]])
}
return(newout)
}

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] sweave tables as images?

2012-05-22 Thread Alexander Shenkin
Thanks Liviu,

I've looked into SWord, and while it's an impressive project, I'm
concerned that it doesn't quite give me the fine-grain control I'd like
over the R environment where it's being executed.  It's still in the
back of my mind though, and I may indeed go with it at some point.

If I understand it correctly, odfWeave doesn't have a path backwards
from the odf doc back to the original odfWeave doc (which is the main
restriction of all these Sweave/knitr/etc solutions in my use case). 
Please correct me if I'm wrong about that!

Thanks,
Allie

On 5/22/2012 4:36 AM, Liviu Andronic wrote:
> On Mon, May 21, 2012 at 10:29 PM, Alexander Shenkin  wrote:
>> So, I think it will be better if I can somehow generate the tables as
>> images.  Is there any way to generate tables as images in separate files
>> in Sweave?  Or, is there another tree up which I should be barking?
>>
> Hmm, there are Sweave equivalents for LibreOffice [1] and MS Word [2].
> Have you looked into those? You may find other suggestions in the
> ReproducibleResearch Task View [3].
>
> Liviu
>
> [1] http://crantastic.org/packages/odfWeave
> [2] http://crantastic.org/packages/SWordInstaller
> [3] http://crantastic.org/task_views/ReproducibleResearch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] sweave tables as images?

2012-05-21 Thread Alexander Shenkin
Thanks Michael - I think grid.table does the trick.

On 5/21/2012 3:33 PM, R. Michael Weylandt wrote:
> Take a look at addtable2plot in plotrix or grid.table / tableGrob in
> gridExtras.
>
> Michael
>
> On Mon, May 21, 2012 at 4:29 PM, Alexander Shenkin  wrote:
>> Hello folks,
>>
>> I've been on a journey trying to figure out how to manage documents that
>> are amenable to sharing and editing, but that contain dynamic content
>> generated by R.  I've come to the following solution: I use Sweave to
>> generate labeled png & pdf figures, and I "Insert & Link" those figures
>> as "Pictures" in a Word 2010 doc.  Thus, when data or code changes, I
>> regenerate the figures with Sweave, open the Word doc and hit "F9", and
>> all the figures are automatically updated.  I can send the file around,
>> folks can comment and edit, track changes, etc.
>>
>> Now, however, I'm trying to do the same for tables that I did for
>> figures, and it seems a bit more difficult.  I can spit out tex tables
>> into separate files using the "split=TRUE" chunk option, and I can even
>> make those tables into html with xtable(type="html").  Word, however,
>> doesn't have the "Insert & Link" option for external text files (which
>> makes it such that, if the external file isn't found, Word uses a copy
>> stored in the doc).
>>
>> So, I think it will be better if I can somehow generate the tables as
>> images.  Is there any way to generate tables as images in separate files
>> in Sweave?  Or, is there another tree up which I should be barking?
>>
>> Thanks,
>> Allie
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] sweave tables as images?

2012-05-21 Thread Alexander Shenkin
Hello folks,

I've been on a journey trying to figure out how to manage documents that
are amenable to sharing and editing, but that contain dynamic content
generated by R.  I've come to the following solution: I use Sweave to
generate labeled png & pdf figures, and I "Insert & Link" those figures
as "Pictures" in a Word 2010 doc.  Thus, when data or code changes, I
regenerate the figures with Sweave, open the Word doc and hit "F9", and
all the figures are automatically updated.  I can send the file around,
folks can comment and edit, track changes, etc.

Now, however, I'm trying to do the same for tables that I did for
figures, and it seems a bit more difficult.  I can spit out tex tables
into separate files using the "split=TRUE" chunk option, and I can even
make those tables into html with xtable(type="html").  Word, however,
doesn't have the "Insert & Link" option for external text files (which
makes it such that, if the external file isn't found, Word uses a copy
stored in the doc).

So, I think it will be better if I can somehow generate the tables as
images.  Is there any way to generate tables as images in separate files
in Sweave?  Or, is there another tree up which I should be barking?

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] auditing R's memory usage

2012-05-04 Thread Alexander Shenkin
Hi Folks,

I'm running 32-bit R 2.14 in RStudio on my Win 7 x64 system with 8GB
RAM.  I'm getting memory problems as R wants to swallow more than the
4GB limit.

I think I'm stuck at 4GB as I have to use 32-bit R for a number of
packages (ODBC, etc).  However, I doubt I really need to be using that
much memory - I'm probably being very sloppy in my memory management,
leaving lots of temporary dataframes around, etc.

I'm looking for suggestions about how to audit R's memory usage.  Yes, I
could go around my code and tie up every loose end.  But in the interest
of efficiency, I'm wondering about ways I might more intelligently audit
R's memory usage.  Are there any tools that can tell me what objects are
swallowing what amounts of memory?  Armed with that information, I can
go track down the worst culprits.

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rgl.Sweave not producing transparency in pdf plots with alpha

2012-05-02 Thread Alexander Shenkin


On 5/2/2012 11:40 AM, Duncan Murdoch wrote:
> On 02/05/2012 12:26 PM, Duncan Murdoch wrote:
>> On 02/05/2012 11:00 AM, Alexander Shenkin wrote:
>> >  Hi Folks,
>> >
>> >  I'm trying to get rgl.Sweave to produce plots with transparency.
>> >  However, it just seems to produce opaque plots when pdf is the output
>> >  type.  Perhaps this is a known issue?  I'll just use .png in the
>> >  meantime, but wanted to see about this, as I didn't see it in the
>> >  documentation (though it's possible I missed it).
>> That uses the rgl.postscript() function, which uses the GL2PS library to
>> convert to PDF.  It may be that it doesn't support transparency in your
>> scene.  Or perhaps it just hasn't been turned on:  GL2PS didn't support
>> transparency when rgl.postscript was written.
> 
> I just took a look:  transparency is turned off, and when it's turned
> on, it doesn't look very good.
> 
> It's possible that there are tuning parameters that could make it look
> good, but I don't know what they are.
> 
> Duncan Murdoch

Thanks Duncan, I'll just stick with png for now then.

> 
>>
>> Duncan Murdoch
>>
>> >
>> >  Thanks,
>> >  Allie
>> >
>> >
>> >
>> >
>> >  \documentclass{article}
>> >  \title {rgl test}
>> >  \usepackage{Sweave}
>> >  \begin {document}
>> >
>> >  This is a test of rgl.sweave's alpha capability.
>> >
>> >  \begin{figure}
>> >  % uncomment line below for png output (correct transparency)
>> >  %<> >  resolution=100>>=
>> >  <> >  resolution=100, outputtype=pdf, pdf=TRUE>>=
>> >  library(rgl)
>> >  data(volcano)
>> >  z<- 2 * volcano# Exaggerate the relief
>> >  x<- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
>> >  y<- 10 * (1:ncol(z))   # 10 meter spacing (E to W)
>> >  zlim<- range(y)
>> >  zlen<- zlim[2] - zlim[1] + 1
>> >  colorlut<- terrain.colors(zlen) # height color lookup table
>> >  col<- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each
>> point
>> >  #open3d()
>> >  surface3d(x, y, z, color=col, back="lines", alpha=0.75)
>> >  @
>> >  \end{figure}
>> >
>> >  \end{document}
>> >
>> >  __
>> >  R-help@r-project.org mailing list
>> >  https://stat.ethz.ch/mailman/listinfo/r-help
>> >  PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> >  and provide commented, minimal, self-contained, reproducible code.
>>
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] rgl.Sweave not producing transparency in pdf plots with alpha

2012-05-02 Thread Alexander Shenkin
Hi Folks,

I'm trying to get rgl.Sweave to produce plots with transparency.
However, it just seems to produce opaque plots when pdf is the output
type.  Perhaps this is a known issue?  I'll just use .png in the
meantime, but wanted to see about this, as I didn't see it in the
documentation (though it's possible I missed it).

Thanks,
Allie




\documentclass{article}
\title {rgl test}
\usepackage{Sweave}
\begin {document}

This is a test of rgl.sweave's alpha capability.

\begin{figure}
% uncomment line below for png output (correct transparency)
% <>=
<>=
library(rgl)
data(volcano)
z <- 2 * volcano# Exaggerate the relief
x <- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z))   # 10 meter spacing (E to W)
zlim <- range(y)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
#open3d()
surface3d(x, y, z, color=col, back="lines", alpha=0.75)
@
\end{figure}

\end{document}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Coding columns for survival analysis

2012-04-16 Thread Alexander Shenkin
Jim,

This was very helpful - thank you!  I really like the use of diff and
cumsum - those haven't been in my toolkit until now.  Your solution came
close, but I needed to keep "NAs" when the tree hadn't been found yet,
or when it had already died.  So, for posterity, here's the code I ended
up with:

x <- read.table(text = "   tree live1 live2 live3 live4 live5
1 tree1 0 0 0 1 1
2 tree2 0 0 1 1 0
3 tree3 0 1 1 0 0
4 tree4 1 1 0 0 0
5 tree4 1 1 1 1 0  # another test condition
6 tree5 1 0 0 0 0", header = TRUE)


# get matrix of data columns
z <- as.matrix(x[, -1])
# process each row
a <- t( apply(z, 1, function(.row) {
.row[is.na(.row)] = 0 # replace NAs with 0's so that
diff works correctly - not a problem in this example, but it is in the
real data
diffs = diff(c(0, .row))
alive <- .row
found <- diffs == 1
die <- diffs == -1
statevec <- rep(1,length(.row))
statevec <- statevec + alive # 2 where alive
statevec <- statevec + found # 3 where found
statevec <- statevec + die * 3 # 4 where dead
c(NA, "alive", "found", "mort")[statevec]
})
   )

a


  [,1][,2][,3][,4][,5]
1 NA  NA  NA  "found" "alive"
2 NA  NA  "found" "alive" "mort"
3 NA  "found" "alive" "mort"  NA
4 "found" "alive" "mort"  NA  NA
5 "found" "alive" "alive" "alive" "mort"
6 "found" "mort"  NA  NA  NA


Best,
Allie

On 4/13/2012 7:01 PM, jim holtman wrote:
> try this:
> 
>> x <- read.table(text = "   tree live1 live2 live3 live4 live5
> +1 tree1 0 0 0 1 1
> +2 tree2 0 0 1 1 0
> +3 tree3 0 1 1 0 0
> +4 tree4 1 1 0 0 0
> +6 tree4 1 1 1 1 0  # another test condition
> +5 tree5 1 0 0 0 0", header = TRUE)
>>
>> # get matrix of data columns
>> z <- as.matrix(x[, -1])
>> # process each row
>> a <- apply(z, 1, function(.row){
> + # determine where found (will be a 2)
> + found <- pmin(cumsum(.row) + 1, 3) # cannot be greater than 3
> + # determined where it died
> + die <- cumsum(diff(c(0, .row)) != 0)
> + # replace value at die == 2 with 4
> + found[die == 2] <- 4
> + c(NA, "found", "alive", "mort")[found]
> + })
>> t(a)  # result
>   [,1][,2][,3][,4][,5]
> 1 NA  NA  NA  "found" "alive"
> 2 NA  NA  "found" "alive" "mort"
> 3 NA  "found" "alive" "mort"  "mort"
> 4 "found" "alive" "mort"  "mort"  "mort"
> 6 "found" "alive" "alive" "alive" "mort"
> 5 "found" "mort"  "mort"  "mort"  "mort"
>>
> 
> 
> On Fri, Apr 13, 2012 at 4:53 PM, Alexander Shenkin  wrote:
>> Hello Folks,
>>
>> I have 5 columns for thousands of tree records that record whether that
>> tree was alive or dead.  I want to recode the columns such that the cell
>> reads "found" when a live tree is first observed, "alive" for when a
>> tree is found alive and is not just found, and "mort" when it was
>> previously alive but is now dead.
>>
>> Given the following:
>>
>>> tree_live = data.frame(tree =
>> c("tree1","tree2","tree3","tree4","tree5"), live1 = c(0,0,0,1,1), live2
>> = c(0,0,1,1,0), live3 = c(0,1,1,0,0), live4 = c(1,1,0,0,0), live5 = c(1,
>> 0, 0, 0, 0))
>>
>>   tree live1 live2 live3 live4 live5
>>1 tree1 0 0 0 1 1
>>2 tree2 0 0 1 1 0
>>3 tree3 0 1 1 0 0
>>4 tree4 1 1 0 0 0
>>5 tree5 1 0 0 0 0
>>
>> I would like to end up with the following:
>>
>>> tree_live_recode
>>
>>  live1 live2 live3 live4 live5
>>1NANANA found alive
>>2NANA found aliv

[R] Coding columns for survival analysis

2012-04-13 Thread Alexander Shenkin
Hello Folks,

I have 5 columns for thousands of tree records that record whether that
tree was alive or dead.  I want to recode the columns such that the cell
reads "found" when a live tree is first observed, "alive" for when a
tree is found alive and is not just found, and "mort" when it was
previously alive but is now dead.

Given the following:

> tree_live = data.frame(tree =
c("tree1","tree2","tree3","tree4","tree5"), live1 = c(0,0,0,1,1), live2
= c(0,0,1,1,0), live3 = c(0,1,1,0,0), live4 = c(1,1,0,0,0), live5 = c(1,
0, 0, 0, 0))

   tree live1 live2 live3 live4 live5
1 tree1 0 0 0 1 1
2 tree2 0 0 1 1 0
3 tree3 0 1 1 0 0
4 tree4 1 1 0 0 0
5 tree5 1 0 0 0 0

I would like to end up with the following:

> tree_live_recode

  live1 live2 live3 live4 live5
1NANANA found alive
2NANA found alive  mort
3NA found alive  mort 0
4 found alive  mort 0 0
5 found  mort 0 0 0

I've accomplished the recode in the past, but only by going over the
dataset multiple times in messy and inefficient fashion.  I'm wondering
if there are concise and efficient ways of going about it?

(I haven't been using the Survival package for my analyses, but I'm
starting to look into it.)

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How do Sweave users collaborate with Word users?

2012-04-09 Thread Alexander Shenkin
Thanks for the heads up, Paul.  That's good to know.  I happen to be in
academics, as are my collaborators, but I could imagine running into
problems down the road.  The good thing seems to be that it's just the
users who want to interact with R who need the software.  If
collaborators are just touching the text, or are inserting graphics of
their own, then sounds like things are still good.  But, that's just my
impression from the brief description I've seen online.

Thanks,
Allie

On 4/9/2012 7:42 AM, Paul Bivand wrote:
> If you're considering SWord, you should remember that the licence is
> not the normal R licence and in commercial use will require a
> commercial licence. While some academic disciplines use Word etc, the
> issue may be more common outside academia.
>
> For those of us where such requirements involve a procurement process,
> the need to purchase something when other users (and the
> administration) are happy with their Word/Excel solutions may be an
> insurmountable barrier.
>
> Good luck
>
> Paul Bivand
> Centre for Economic and Social Inclusion (a non-profit organisation)
> London
>
>
>
> On 9 April 2012 13:23, Richard M. Heiberger  wrote:
>> You might want to consider SWord, which provides similar facilities for the
>> Word and R
>> user.  Word-oriented co-authors can modify the Word part of the document
>> without
>> impacting the R part of the document.
>>
>> SWord is by Thomas Baier tho...@statconn.com, author of the statconnDCOM
>> interface
>> that is underneath RExcel.  See rcom.univie.ac.at for information and
>> download and to
>> sign up on the rcom email list.
>>
>> Rich
>>
>> On Sat, Apr 7, 2012 at 4:54 PM, Alexander Shenkin  wrote:
>>
>>> Hello All,
>>>
>>> I'm getting my workflow switched over to Sweave, which is very cool.
>>> However, I collaborate with folks (as many of you must as well) who use
>>> Word to Track Changes amongst a group while crafting a paper.  In the
>>> simplest case, there will just be two people (one Sweave user and one
>>> Word user) editing a paper.
>>>
>>> I'm wondering, how do Sweave users go about this?  I could convert a
>>> sweave file to a .docx easily enough via an intermediary pdf, rtf, html
>>> or otherwise.  However, once the file has been marked up with changes,
>>> the challenge is to migrate those (accepted) changes back to the sweave
>>> document.  Perhaps the most straightforward way is to manually
>>> back-propagate changes, but I imagine that could be a painstaking process.
>>>
>>> Ideally, I imagine a tool that puts invisible tags in the word document
>>> when it is originally produced from Sweave, and is then able to
>>> propagate changes back to that sweave file after markup.  I'd be
>>> pleasantly surprised if such a tool existed.
>>>
>>> Perhaps there are other ways of making this work.  Any thoughts are
>>> kindly appreciated!
>>>
>>> Thanks,
>>> Allie
>>>
>>> __
>>> R-help@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>[[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How do Sweave users collaborate with Word users?

2012-04-09 Thread Alexander Shenkin
Thanks Rich,

While it doesn't tickle me the way sweave/knitr does, SWord sounds more
or less like the thing I'm looking for.  However, poking around
http://rcom.univie.ac.at/ and http://www.statconn.com doesn't reveal any
download links.  It seems as though SWord has been pulled from their
lineup?  Not sure - I've shot them an email to inquire.

Thanks,
Allie

On 4/9/2012 7:23 AM, Richard M. Heiberger wrote:
> You might want to consider SWord, which provides similar facilities
> for the Word and R
> user.  Word-oriented co-authors can modify the Word part of the
> document without
> impacting the R part of the document.
>  
> SWord is by Thomas Baier tho...@statconn.com
> <mailto:tho...@statconn.com>, author of the statconnDCOM interface
> that is underneath RExcel.  See rcom.univie.ac.at
> <http://rcom.univie.ac.at> for information and download and to
> sign up on the rcom email list.
>  
> Rich
>
> On Sat, Apr 7, 2012 at 4:54 PM, Alexander Shenkin  <mailto:ashen...@ufl.edu>> wrote:
>
> Hello All,
>
> I'm getting my workflow switched over to Sweave, which is very cool.
> However, I collaborate with folks (as many of you must as well)
> who use
> Word to Track Changes amongst a group while crafting a paper.  In the
> simplest case, there will just be two people (one Sweave user and one
> Word user) editing a paper.
>
> I'm wondering, how do Sweave users go about this?  I could convert a
> sweave file to a .docx easily enough via an intermediary pdf, rtf,
> html
> or otherwise.  However, once the file has been marked up with changes,
> the challenge is to migrate those (accepted) changes back to the
> sweave
> document.  Perhaps the most straightforward way is to manually
> back-propagate changes, but I imagine that could be a painstaking
> process.
>
> Ideally, I imagine a tool that puts invisible tags in the word
> document
> when it is originally produced from Sweave, and is then able to
> propagate changes back to that sweave file after markup.  I'd be
> pleasantly surprised if such a tool existed.
>
> Perhaps there are other ways of making this work.  Any thoughts are
> kindly appreciated!
>
> Thanks,
> Allie
>
> __
> R-help@r-project.org <mailto:R-help@r-project.org> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How do Sweave users collaborate with Word users?

2012-04-08 Thread Alexander Shenkin
This sounds neat, and I wish you the best of luck with it.  While I'm
sure it'll be great for folks who are already curious enough to dive
into Sweave or knitr, my guess is that coaxing collaborators into using
markdown with SVN or GIT instead of passing around a Word file with
Track Changes is going to be a tough sell, to put it lightly.

I'll keep my eyes out for emerging sweave/knitr <-> word solutions (note
the double arrow), but sounds like manual change-synchronization is the
only game in town right now, alas.

Thanks,
Allie

On 4/7/2012 4:29 PM, Yihui Xie wrote:
> I cannot reply in detail at the moment, but we have a proposal to the
> Google Summer of Code this year which will address the collaboration
> issue. The basic idea is to write everything in plain text with
> markdown (including R code), and compile the file with the knitr
> package, then convert it to Word or whatever other formats with
> pandoc, which is an extremely powerful document converter. You can see
> a simple example here for how knitr works with markdown:
> http://yihui.name/knitr/demo/upload/ Since everything is in plain
> text, it is easy to collaborate through a version control system like
> SVN or GIT. Markdown is easy to write and cleaner compared to Word.
> 
> Regards,
> Yihui
> --
> Yihui Xie 
> Phone: 515-294-2465 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
> 
> 
> 
> On Sat, Apr 7, 2012 at 3:54 PM, Alexander Shenkin  wrote:
>> Hello All,
>>
>> I'm getting my workflow switched over to Sweave, which is very cool.
>> However, I collaborate with folks (as many of you must as well) who use
>> Word to Track Changes amongst a group while crafting a paper.  In the
>> simplest case, there will just be two people (one Sweave user and one
>> Word user) editing a paper.
>>
>> I'm wondering, how do Sweave users go about this?  I could convert a
>> sweave file to a .docx easily enough via an intermediary pdf, rtf, html
>> or otherwise.  However, once the file has been marked up with changes,
>> the challenge is to migrate those (accepted) changes back to the sweave
>> document.  Perhaps the most straightforward way is to manually
>> back-propagate changes, but I imagine that could be a painstaking process.
>>
>> Ideally, I imagine a tool that puts invisible tags in the word document
>> when it is originally produced from Sweave, and is then able to
>> propagate changes back to that sweave file after markup.  I'd be
>> pleasantly surprised if such a tool existed.
>>
>> Perhaps there are other ways of making this work.  Any thoughts are
>> kindly appreciated!
>>
>> Thanks,
>> Allie
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How do Sweave users collaborate with Word users?

2012-04-07 Thread Alexander Shenkin
Hello All,

I'm getting my workflow switched over to Sweave, which is very cool.
However, I collaborate with folks (as many of you must as well) who use
Word to Track Changes amongst a group while crafting a paper.  In the
simplest case, there will just be two people (one Sweave user and one
Word user) editing a paper.

I'm wondering, how do Sweave users go about this?  I could convert a
sweave file to a .docx easily enough via an intermediary pdf, rtf, html
or otherwise.  However, once the file has been marked up with changes,
the challenge is to migrate those (accepted) changes back to the sweave
document.  Perhaps the most straightforward way is to manually
back-propagate changes, but I imagine that could be a painstaking process.

Ideally, I imagine a tool that puts invisible tags in the word document
when it is originally produced from Sweave, and is then able to
propagate changes back to that sweave file after markup.  I'd be
pleasantly surprised if such a tool existed.

Perhaps there are other ways of making this work.  Any thoughts are
kindly appreciated!

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Rgui maintains open file handles after Sweave error

2012-04-05 Thread Alexander Shenkin
Reproducibility is important, and as I mentioned in a previous email,
there are probably ways I could avoid running the entire script over and
over again with each sweave compilation.  Still, relying on saved
workspaces, temporary files or caches still has some of the issues that
working in the main environment does.  Specifically, you're not working
with the base data all the way through the final analyses each time you
run the sweave doc.  To produce those workspace, files or caches
requires a run of scripts.  If those scripts have changed, or if your
data have changed, then your workspace, files and/or cache is then just
as out of date as your workspace.  Saving workspaces, files and/or
caches still requires care that they're not saved after having been
modified by the command line, etc.  I think that working with saved
workspaces, files and/or caches is probably less prone to "pollution"
than working in the main environment, but it's far from failsafe.

As long as the final sweave doc is run with scripts from beginning to
end, getting the sweave doc up to snuff by working more quickly in the
main environment is acceptable IMHO.  So is working with the other
methods above.

Best,
Allie

On 4/5/2012 12:34 PM, Yihui Xie wrote:
> Well, I do not think it is a good practice (in terms of reproducible
> research) to keep on running Sweave in the same R session, because
> your previous run and your current workspace could "pollute" your next
> run. To make sure a document compiles on its own, it is better always
> to start a new clean R session. If you run into speed issues (e.g. you
> have chunks which require intensive computing), you can consider using
> cache, which the knitr package has reasonably good support.
>
> Regards,
> Yihui
> --
> Yihui Xie 
> Phone: 515-294-2465 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
>
>
>
> On Thu, Apr 5, 2012 at 12:26 PM, Alexander Shenkin  wrote:
>> Yep, I'm using RStudio, and have used Tinn-R in the past.  RStudio does
>> start a new R session when processing a sweave document via the RStudio
>> GUI.  In my case, this presented a problem for the reasons I stated
>> before (i.e. that I need to run sweave in the main environment, not a
>> new one).  Hence, I'm running Sweave from the command line, and
>> processing the tex doc via TeXworks (from MiKTeX).  I sent the RStudio
>> team a feature request to add an option to be able to run Sweave in the
>> main environment (perhaps while maintaining the default of running it in
>> a separate environment).
>>
>> Allie
>>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Rgui maintains open file handles after Sweave error

2012-04-05 Thread Alexander Shenkin
Yep, I'm using RStudio, and have used Tinn-R in the past.  RStudio does
start a new R session when processing a sweave document via the RStudio
GUI.  In my case, this presented a problem for the reasons I stated
before (i.e. that I need to run sweave in the main environment, not a
new one).  Hence, I'm running Sweave from the command line, and
processing the tex doc via TeXworks (from MiKTeX).  I sent the RStudio
team a feature request to add an option to be able to run Sweave in the
main environment (perhaps while maintaining the default of running it in
a separate environment).

Allie

On 4/5/2012 12:12 PM, Yihui Xie wrote:
> In terms of editors, I think RStudio is pretty good
> (http://www.rstudio.org/download/preview). Or LyX
> (http://yihui.name/knitr/demo/lyx/), or TeXmaker, WinEdit
> (http://yihui.name/knitr/demo/editors/)... All of them start a new R
> session when weaving the document, and all support one-click
> compilation. In all, anything but Windows Notepad.
> 
> Regards,
> Yihui
> --
> Yihui Xie 
> Phone: 515-294-2465 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
> 
> 
> 
> On Thu, Apr 5, 2012 at 11:22 AM, Duncan Murdoch
>  wrote:
>> On 04/04/2012 3:25 PM, Alexander Shenkin wrote:
>>>
>>> Hello Folks,
>>>
>>> When I run the document below through sweave, rgui.exe/rsession.exe
>>> leaves a file handle open to the sweave-001.pdf graphic (as verified by
>>> process explorer).  Pdflatex.exe then crashes (with a Permission Denied
>>> error) because the graphic file is locked.
>>>
>>> This only seems to happen when there is an error in the sweave document.
>>>  When there are no errors, no file handles are left open.  However, once
>>> a file handle is stuck open, I can find no other way of closing it save
>>> for quitting out of R.
>>>
>>> Any help would be greatly appreciated!  It would be nice to be able to
>>> write flawless sweave every time, but flawed as I am, I am having to
>>> restart R continuously.
>>
>>
>> I'd suggest a different workflow, in which you run a new copy of R every
>> time you want to Sweave a document.  The files will be closed when that copy
>> dies, and the results are less likely to be affected by the current state of
>> your workspace (assuming you don't load an old workspace in the new copy).
>>
>> For example, when I'm working on a Sweave document, I spend my time in my
>> text editor, and get it to run R to process the file whenever I want to see
>> what the output looks like.
>>
>> The only real disadvantages to this approach that I can think of are that
>> you need to figure out how to tell your text editor to run R (and that might
>> be hard if you're using a poor editor like Windows Notebook, but is usually
>> easy), and it will run a tiny bit slower because you need to start up R
>> every time.
>>
>> Duncan Murdoch
>>
>>
>>> Thanks,
>>> Allie
>>>
>>>
>>> OS: Windows 7 Pro x64 SP1
>>>
>>>
>>>>  sessionInfo()
>>> R version 2.14.2 (2012-02-29)
>>> Platform: i386-pc-mingw32/i386 (32-bit)
>>>
>>>
>>> test.Rnw:
>>>
>>> \documentclass{article}
>>> \title {file handle test}
>>> \author{test author}
>>> \usepackage{Sweave}
>>> \begin {document}
>>> \maketitle
>>>
>>> \SweaveOpts{prefix.string=sweave}
>>>
>>> \begin{figure}
>>> \begin{center}
>>>
>>> <>=
>>> df = data.frame(a=rnorm(100), b=rnorm(100), group = c("g1",
>>> "g2", "g3", "g4"))
>>> plot(df$a, df$y, foo)
>>> @
>>>
>>> \caption{test figure one}
>>> \label{fig:one}
>>> \end{center}
>>> \end{figure}
>>> \end{document}
>>>
>>>
>>>
>>> Sweave command run:
>>>
>>> Sweave("test.Rnw", syntax="SweaveSyntaxNoweb")
>>>
>>>
>>>
>>> Sweave.sty:
>>>
>>> \NeedsTeXFormat{LaTeX2e}
>>> \ProvidesPackage{Sweave}{}
>>>
>>> \RequirePackage{ifthen}
>>> \newboolean{Sweave@gin}
>>> \setboolean{Sweave@gin}{true}
>>> \newboolean{Sweave@ae}
>>> \setboolean{Sweave@ae}{true}
>>>
>>> \DeclareOption{nogin}{\setboolean{Sweave@gin}{fa

Re: [R] Rgui maintains open file handles after Sweave error

2012-04-05 Thread Alexander Shenkin
Thanks for the nice ideas, Duncan.  I think that would work nicely in
most cases.  The major issue with that workflow in my case is that the
scripts to set up my workspace take around a half-hour to run (I really
wish CUDA was working with my setup!), so running R each time in that
case is time-consuming.

Perhaps I should be working more with intermediate files, or perhaps
writing the workspace out to an .Rdata file and reading that in the
sweave document instead of running the entire data-prep script.

Thanks,
Allie

On 4/5/2012 11:22 AM, Duncan Murdoch wrote:
> On 04/04/2012 3:25 PM, Alexander Shenkin wrote:
>> Hello Folks,
>>
>> When I run the document below through sweave, rgui.exe/rsession.exe
>> leaves a file handle open to the sweave-001.pdf graphic (as verified by
>> process explorer).  Pdflatex.exe then crashes (with a Permission Denied
>> error) because the graphic file is locked.
>>
>> This only seems to happen when there is an error in the sweave document.
>>   When there are no errors, no file handles are left open.  However, once
>> a file handle is stuck open, I can find no other way of closing it save
>> for quitting out of R.
>>
>> Any help would be greatly appreciated!  It would be nice to be able to
>> write flawless sweave every time, but flawed as I am, I am having to
>> restart R continuously.
> 
> I'd suggest a different workflow, in which you run a new copy of R every
> time you want to Sweave a document.  The files will be closed when that
> copy dies, and the results are less likely to be affected by the current
> state of your workspace (assuming you don't load an old workspace in the
> new copy).
> 
> For example, when I'm working on a Sweave document, I spend my time in
> my text editor, and get it to run R to process the file whenever I want
> to see what the output looks like.
> 
> The only real disadvantages to this approach that I can think of are
> that you need to figure out how to tell your text editor to run R (and
> that might be hard if you're using a poor editor like Windows Notebook,
> but is usually easy), and it will run a tiny bit slower because you need
> to start up R every time.
> 
> Duncan Murdoch
> 
>> Thanks,
>> Allie
>>
>>
>> OS: Windows 7 Pro x64 SP1
>>
>>
>> >  sessionInfo()
>> R version 2.14.2 (2012-02-29)
>> Platform: i386-pc-mingw32/i386 (32-bit)
>>
>>
>> test.Rnw:
>>
>>  \documentclass{article}
>>  \title {file handle test}
>>  \author{test author}
>>  \usepackage{Sweave}
>>  \begin {document}
>>  \maketitle
>>
>>  \SweaveOpts{prefix.string=sweave}
>>
>>  \begin{figure}
>>  \begin{center}
>>
>>  <>=
>>  df = data.frame(a=rnorm(100), b=rnorm(100), group = c("g1",
>> "g2", "g3", "g4"))
>>  plot(df$a, df$y, foo)
>>  @
>>
>>  \caption{test figure one}
>>  \label{fig:one}
>>  \end{center}
>>  \end{figure}
>>  \end{document}
>>
>>
>>
>> Sweave command run:
>>
>>  Sweave("test.Rnw", syntax="SweaveSyntaxNoweb")
>>
>>
>>
>> Sweave.sty:
>>
>>  \NeedsTeXFormat{LaTeX2e}
>>  \ProvidesPackage{Sweave}{}
>>
>>  \RequirePackage{ifthen}
>>  \newboolean{Sweave@gin}
>>  \setboolean{Sweave@gin}{true}
>>  \newboolean{Sweave@ae}
>>  \setboolean{Sweave@ae}{true}
>>
>>  \DeclareOption{nogin}{\setboolean{Sweave@gin}{false}}
>>  \DeclareOption{noae}{\setboolean{Sweave@ae}{false}}
>>  \ProcessOptions
>>
>>  \RequirePackage{graphicx,fancyvrb}
>>  \IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
>>
>> 
>> \ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
>>  \ifthenelse{\boolean{Sweave@ae}}{%
>>\RequirePackage[T1]{fontenc}
>>\RequirePackage{ae}
>>  }{}%
>>
>>  \DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
>>  \DefineVerbatimEnvironment{Soutput}{Verbatim}{}
>>  \DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
>>
>>  \newenvironment{Schunk}{}{}
>>
>>  \newcommand{\Sconcordance}[1]{%
>>\ifx\pdfoutput\undefined%
>>\csname newcount\endcsname\pdfoutput\fi%
>>\ifcase\pdfoutput\special{#1}%
>>\else\immediate\pdfobj{#1}\fi}
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Rgui maintains open file handles after Sweave error

2012-04-05 Thread Alexander Shenkin
Here is Dr. Leisch's advice for dealing with open handles (and it works):

> On 4/5/2012 4:22 AM, Friedrich Leisch wrote:
> ...
> You need to close the pdf device, not an open connection:
>
> R> Sweave("test.Rnw")
> Writing to file test.tex
> Processing code chunks with options ...
>  1 : keep.source term verbatim pdf
>
> Error:  chunk 1
> Error in plot.xy(xy, type, ...) : object 'foo' not found
> R> ls()
> [1] "col.yellowbg" "df"
> R> dev.list()
> pdf
>   2
> R> dev.off()
> null device
>   1
> Best,
> Fritz



On 4/4/2012 2:52 PM, Alexander Shenkin wrote:
> Thanks for the reply, Henrik.  Process Explorer still shows the file
> handle as being open, but R only shows the following:
> 
>> showConnections(all=TRUE)
>   description class  mode text   isopen   can read can write
> 0 "stdin" "terminal" "r"  "text" "opened" "yes""no"
> 1 "stdout""terminal" "w"  "text" "opened" "no" "yes"
> 2 "stderr""terminal" "w"  "text" "opened" "no" "yes"
>>
> 
> On 4/4/2012 2:45 PM, Henrik Bengtsson wrote:
>> See ?closeAllConnections
>>
>> Suggestion to the maintainer of Sweave: "atomify" the figure
>> generation, e.g. use { pdf(); on.exit(dev.off()); {...}; } or similar,
>> instead of { pdf(); {...}; dev.off(); } possibly by leaving a copy of
>> the fault figure file for troubleshooting.
>>
>> /Henrik
>>
>> On Wed, Apr 4, 2012 at 12:25 PM, Alexander Shenkin  wrote:
>>> Hello Folks,
>>>
>>> When I run the document below through sweave, rgui.exe/rsession.exe
>>> leaves a file handle open to the sweave-001.pdf graphic (as verified by
>>> process explorer).  Pdflatex.exe then crashes (with a Permission Denied
>>> error) because the graphic file is locked.
>>>
>>> This only seems to happen when there is an error in the sweave document.
>>>  When there are no errors, no file handles are left open.  However, once
>>> a file handle is stuck open, I can find no other way of closing it save
>>> for quitting out of R.
>>>
>>> Any help would be greatly appreciated!  It would be nice to be able to
>>> write flawless sweave every time, but flawed as I am, I am having to
>>> restart R continuously.
>>>
>>> Thanks,
>>> Allie
>>>
>>>
>>> OS: Windows 7 Pro x64 SP1
>>>
>>>
>>>> sessionInfo()
>>> R version 2.14.2 (2012-02-29)
>>> Platform: i386-pc-mingw32/i386 (32-bit)
>>>
>>>
>>> test.Rnw:
>>>
>>>\documentclass{article}
>>>\title {file handle test}
>>>\author{test author}
>>>\usepackage{Sweave}
>>>\begin {document}
>>>\maketitle
>>>
>>>\SweaveOpts{prefix.string=sweave}
>>>
>>>\begin{figure}
>>>\begin{center}
>>>
>>><>=
>>>df = data.frame(a=rnorm(100), b=rnorm(100), group = c("g1",
>>> "g2", "g3", "g4"))
>>>plot(df$a, df$y, foo)
>>>@
>>>
>>>\caption{test figure one}
>>>\label{fig:one}
>>>\end{center}
>>>\end{figure}
>>>\end{document}
>>>
>>>
>>>
>>> Sweave command run:
>>>
>>>Sweave("test.Rnw", syntax="SweaveSyntaxNoweb")
>>>
>>>
>>>
>>> Sweave.sty:
>>>
>>>\NeedsTeXFormat{LaTeX2e}
>>>\ProvidesPackage{Sweave}{}
>>>
>>>\RequirePackage{ifthen}
>>>\newboolean{Sweave@gin}
>>>\setboolean{Sweave@gin}{true}
>>>\newboolean{Sweave@ae}
>>>\setboolean{Sweave@ae}{true}
>>>
>>>\DeclareOption{nogin}{\setboolean{Sweave@gin}{false}}
>>>\DeclareOption{noae}{\setboolean{Sweave@ae}{false}}
>>>\ProcessOptions
>>>
>>>\RequirePackage{graphicx,fancyvrb}
>>>\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
>>>
>>>\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
>>>\ifthenelse{\boolean{Sweave@ae}}{%
>>>  \RequirePackage[T1]{fontenc}
>>>  \RequirePackage{ae}
>>>}{}%
>>>
>>>\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
>>>\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
>>>\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
>>>
>>>\newenvironment{Schunk}{}{}
>>>
>>>\newcommand{\Sconcordance}[1]{%
>>>  \ifx\pdfoutput\undefined%
>>>  \csname newcount\endcsname\pdfoutput\fi%
>>>  \ifcase\pdfoutput\special{#1}%
>>>  \else\immediate\pdfobj{#1}\fi}
>>>
>>> __
>>> R-help@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Rgui maintains open file handles after Sweave error

2012-04-04 Thread Alexander Shenkin
Thanks for the reply, Henrik.  Process Explorer still shows the file
handle as being open, but R only shows the following:

> showConnections(all=TRUE)
  description class  mode text   isopen   can read can write
0 "stdin" "terminal" "r"  "text" "opened" "yes""no"
1 "stdout""terminal" "w"  "text" "opened" "no" "yes"
2 "stderr""terminal" "w"  "text" "opened" "no" "yes"
>

On 4/4/2012 2:45 PM, Henrik Bengtsson wrote:
> See ?closeAllConnections
> 
> Suggestion to the maintainer of Sweave: "atomify" the figure
> generation, e.g. use { pdf(); on.exit(dev.off()); {...}; } or similar,
> instead of { pdf(); {...}; dev.off(); } possibly by leaving a copy of
> the fault figure file for troubleshooting.
> 
> /Henrik
> 
> On Wed, Apr 4, 2012 at 12:25 PM, Alexander Shenkin  wrote:
>> Hello Folks,
>>
>> When I run the document below through sweave, rgui.exe/rsession.exe
>> leaves a file handle open to the sweave-001.pdf graphic (as verified by
>> process explorer).  Pdflatex.exe then crashes (with a Permission Denied
>> error) because the graphic file is locked.
>>
>> This only seems to happen when there is an error in the sweave document.
>>  When there are no errors, no file handles are left open.  However, once
>> a file handle is stuck open, I can find no other way of closing it save
>> for quitting out of R.
>>
>> Any help would be greatly appreciated!  It would be nice to be able to
>> write flawless sweave every time, but flawed as I am, I am having to
>> restart R continuously.
>>
>> Thanks,
>> Allie
>>
>>
>> OS: Windows 7 Pro x64 SP1
>>
>>
>>> sessionInfo()
>> R version 2.14.2 (2012-02-29)
>> Platform: i386-pc-mingw32/i386 (32-bit)
>>
>>
>> test.Rnw:
>>
>>\documentclass{article}
>>\title {file handle test}
>>\author{test author}
>>\usepackage{Sweave}
>>\begin {document}
>>\maketitle
>>
>>\SweaveOpts{prefix.string=sweave}
>>
>>\begin{figure}
>>\begin{center}
>>
>><>=
>>df = data.frame(a=rnorm(100), b=rnorm(100), group = c("g1",
>> "g2", "g3", "g4"))
>>plot(df$a, df$y, foo)
>>@
>>
>>\caption{test figure one}
>>\label{fig:one}
>>\end{center}
>>\end{figure}
>>\end{document}
>>
>>
>>
>> Sweave command run:
>>
>>Sweave("test.Rnw", syntax="SweaveSyntaxNoweb")
>>
>>
>>
>> Sweave.sty:
>>
>>\NeedsTeXFormat{LaTeX2e}
>>\ProvidesPackage{Sweave}{}
>>
>>\RequirePackage{ifthen}
>>\newboolean{Sweave@gin}
>>\setboolean{Sweave@gin}{true}
>>\newboolean{Sweave@ae}
>>\setboolean{Sweave@ae}{true}
>>
>>\DeclareOption{nogin}{\setboolean{Sweave@gin}{false}}
>>\DeclareOption{noae}{\setboolean{Sweave@ae}{false}}
>>\ProcessOptions
>>
>>\RequirePackage{graphicx,fancyvrb}
>>\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
>>
>>\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
>>\ifthenelse{\boolean{Sweave@ae}}{%
>>  \RequirePackage[T1]{fontenc}
>>  \RequirePackage{ae}
>>}{}%
>>
>>\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
>>\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
>>\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
>>
>>\newenvironment{Schunk}{}{}
>>
>>\newcommand{\Sconcordance}[1]{%
>>  \ifx\pdfoutput\undefined%
>>  \csname newcount\endcsname\pdfoutput\fi%
>>  \ifcase\pdfoutput\special{#1}%
>>  \else\immediate\pdfobj{#1}\fi}
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Rgui maintains open file handles after Sweave error

2012-04-04 Thread Alexander Shenkin
Hello Folks,

When I run the document below through sweave, rgui.exe/rsession.exe
leaves a file handle open to the sweave-001.pdf graphic (as verified by
process explorer).  Pdflatex.exe then crashes (with a Permission Denied
error) because the graphic file is locked.

This only seems to happen when there is an error in the sweave document.
 When there are no errors, no file handles are left open.  However, once
a file handle is stuck open, I can find no other way of closing it save
for quitting out of R.

Any help would be greatly appreciated!  It would be nice to be able to
write flawless sweave every time, but flawed as I am, I am having to
restart R continuously.

Thanks,
Allie


OS: Windows 7 Pro x64 SP1


> sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: i386-pc-mingw32/i386 (32-bit)


test.Rnw:

\documentclass{article}
\title {file handle test}
\author{test author}
\usepackage{Sweave}
\begin {document}
\maketitle

\SweaveOpts{prefix.string=sweave}

\begin{figure}
\begin{center}

<>=
df = data.frame(a=rnorm(100), b=rnorm(100), group = c("g1",
"g2", "g3", "g4"))
plot(df$a, df$y, foo)
@

\caption{test figure one}
\label{fig:one}
\end{center}
\end{figure}
\end{document}



Sweave command run:

Sweave("test.Rnw", syntax="SweaveSyntaxNoweb")



Sweave.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{Sweave}{}

\RequirePackage{ifthen}
\newboolean{Sweave@gin}
\setboolean{Sweave@gin}{true}
\newboolean{Sweave@ae}
\setboolean{Sweave@ae}{true}

\DeclareOption{nogin}{\setboolean{Sweave@gin}{false}}
\DeclareOption{noae}{\setboolean{Sweave@ae}{false}}
\ProcessOptions

\RequirePackage{graphicx,fancyvrb}
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}

\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
\ifthenelse{\boolean{Sweave@ae}}{%
  \RequirePackage[T1]{fontenc}
  \RequirePackage{ae}
}{}%

\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}

\newenvironment{Schunk}{}{}

\newcommand{\Sconcordance}[1]{%
  \ifx\pdfoutput\undefined%
  \csname newcount\endcsname\pdfoutput\fi%
  \ifcase\pdfoutput\special{#1}%
  \else\immediate\pdfobj{#1}\fi}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] summaryBy: transformed variable on RHS of formula?

2012-04-02 Thread Alexander Shenkin
Hi Folks,

I'm trying to cut my data inside the summaryBy function.  Perhaps
formulas don't work that way?  I'd like to avoid adding another column
if possible, but if I have to, I have to.  Any ideas?

Thanks,
Allie

require(doBy)
df = dataframe(a <- rnorm(100), b <-rnorm(100))
summaryBy(a ~ cut(b,c(-100,-1,1,100)), data=df) # preferred
solution, but it throws an error
# Error in
`[.data.frame`(data, , rh.var, drop = FALSE) : undefined columns selected

df$c = cut(b,c(-100,-1,1,100))
summaryBy(a ~ c, data=df)   # works fine

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] dot products

2012-03-07 Thread Alexander Shenkin
Hello,

I need to take a dot product of each row of a dataframe and a vector.
The number of columns will be dynamic.  The way I've been doing it so
far is contorted.  Is there a better way?

dotproduct <- function(dataf, v2) {
apply(t(t(as.matrix(a)) * v2),1,sum) #contorted!
}

df = data.frame(a=c(1,2,3),b=c(4,5,6))
vec = c(4,5)
dotproduct(df, vec)


thanks,
allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Vectorizing a loop

2012-02-07 Thread Alexander Shenkin
Hello Folks,

I'm trying to vectorize a loop that processes rows of a dataframe.  It
involves lots of conditionals, such as "If column 10 == 3, and if column
3 is True, and both column 5 and 6 are False, then set column 4 to True".

So, for example, any ideas about vectorizing the following?

df = data.frame( list(a=c(1,2,3,4), b=c("a","b","c","d"), c=c(T,F,T,F),
d=NA, e=c(F,F,T,T)) )

for (i in 1:nrow(df)) {

if (df[i,3] %in% c(FALSE,NA) & (df[i,1] > 2 | df[i,5]) ) {
df[i,4] = 1
}

if (df[i,5] %in% c(TRUE, NA) & df[i,2] == "b") {
df[i,4] = 2
df[i,5] = T
}

}

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] strange date problem - May 3, 1992 is NA

2011-06-22 Thread Alexander Shenkin
On 6/22/2011 4:09 PM, Brian Diggs wrote:
> On 6/22/2011 1:37 PM, Alexander Shenkin wrote:
>> On 6/22/2011 3:34 PM, Brian Diggs wrote:
>>> On 6/22/2011 12:09 PM, Luke Miller wrote:
>>>> For what it's worth, I cannot reproduce this problem under a nearly
>>>> identical instance of R (R 2.12.1, Win 7 Pro 64-bit). I also can't
>>>> reproduce the problem with R 2.13.0. You've got something truly weird
>>>> going on with your particular instance of R.
>>>>
>>>>
>>>>> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
>>>> [1] FALSE
>>>>> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
>>>> [1] FALSE
>>>>> sessionInfo()
>>>> R version 2.12.1 (2010-12-16)
>>>> Platform: i386-pc-mingw32/i386 (32-bit)
>>>>
>>>> locale:
>>>> [1] LC_COLLATE=English_United States.1252
>>>> [2] LC_CTYPE=English_United States.1252
>>>> [3] LC_MONETARY=English_United States.1252
>>>> [4] LC_NUMERIC=C
>>>> [5] LC_TIME=English_United States.1252
>>>>
>>>> attached base packages:
>>>> [1] stats graphics  grDevices utils datasets  methods   base
>>>>
>>>> other attached packages:
>>>> [1] rj_0.5.2-1  lattice_0.19-17
>>>>
>>>> loaded via a namespace (and not attached):
>>>> [1] grid_2.12.1  rJava_0.8-8  tools_2.12.1
>>>
>>> Like Luke, I can not reproduce what you see in (an old installation of)
>>> R 2.12.1 (and it also didn't have rj, lattice, grid, rJava, or tools
>>> attached or loaded in any way).
>>>
>>> My vague gut feeling is it might be a timezone/daylight savings time
>>> related issue (though usually times have to be involved).  At least,
>>> that is a common problem with weird things happening with dates.
>>>
>>> What do you get as output for the following?
>>>
>>> Sys.timezone()
>>> Sys.info()
>>> conflicts()
>>> dput(strptime("5/3/1992", format="%m/%d/%Y"))
>>> dput(as.POSIXct(strptime("5/3/1992", format="%m/%d/%Y")))
>>> dput(strptime("5/2/1992", format="%m/%d/%Y"))
>>> dput(as.POSIXct(strptime("5/2/1992", format="%m/%d/%Y")))
>>
>>> Sys.timezone()
>> [1] "COT"
>>> Sys.info()
>>   sysname  release
>> version nodename  machine
>> "Windows"  "7 x64" "build 7601,
>> Service Pack 1" "machine_name""x86"
>> login user
>>"username"   "username"
>>> conflicts()
>> [1] "untangle.specials" "body<-""format.pval"
>> "round.POSIXt"  "trunc.POSIXt"  "units"
>>> dput(strptime("5/3/1992", format="%m/%d/%Y"))
>> structure(list(sec = 0, min = 0L, hour = 0L, mday = 3L, mon = 4L,
>>  year = 92L, wday = 0L, yday = 123L, isdst = -1L), .Names = c("sec",
>> "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
>> ), class = c("POSIXlt", "POSIXt"))
>>> dput(as.POSIXct(strptime("5/3/1992", format="%m/%d/%Y")))
>> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "")
>>> dput(strptime("5/2/1992", format="%m/%d/%Y"))
>> structure(list(sec = 0, min = 0L, hour = 0L, mday = 2L, mon = 4L,
>>  year = 92L, wday = 6L, yday = 122L, isdst = 0L), .Names = c("sec",
>> "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
>> ), class = c("POSIXlt", "POSIXt"))
>>> dput(as.POSIXct(strptime("5/2/1992", format="%m/%d/%Y")))
>> structure(704782800, class = c("POSIXct", "POSIXt"), tzone = "")
>>
> 
> Fun :)
> 
> So, not being familiar with COT, I looked it up to see what/when the
> daylight savings times switch overs are/were.
> 
> http://www.timeanddate.com/worldclock/timezone.html?n=41&syear=1990
>

Re: [R] strange date problem - May 3, 1992 is NA

2011-06-22 Thread Alexander Shenkin
On 6/22/2011 3:34 PM, Brian Diggs wrote:
> On 6/22/2011 12:09 PM, Luke Miller wrote:
>> For what it's worth, I cannot reproduce this problem under a nearly
>> identical instance of R (R 2.12.1, Win 7 Pro 64-bit). I also can't
>> reproduce the problem with R 2.13.0. You've got something truly weird
>> going on with your particular instance of R.
>>
>>
>>> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
>> [1] FALSE
>>> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
>> [1] FALSE
>>> sessionInfo()
>> R version 2.12.1 (2010-12-16)
>> Platform: i386-pc-mingw32/i386 (32-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252
>> [2] LC_CTYPE=English_United States.1252
>> [3] LC_MONETARY=English_United States.1252
>> [4] LC_NUMERIC=C
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] stats graphics  grDevices utils datasets  methods   base
>>
>> other attached packages:
>> [1] rj_0.5.2-1  lattice_0.19-17
>>
>> loaded via a namespace (and not attached):
>> [1] grid_2.12.1  rJava_0.8-8  tools_2.12.1
> 
> Like Luke, I can not reproduce what you see in (an old installation of)
> R 2.12.1 (and it also didn't have rj, lattice, grid, rJava, or tools
> attached or loaded in any way).
> 
> My vague gut feeling is it might be a timezone/daylight savings time
> related issue (though usually times have to be involved).  At least,
> that is a common problem with weird things happening with dates.
> 
> What do you get as output for the following?
> 
> Sys.timezone()
> Sys.info()
> conflicts()
> dput(strptime("5/3/1992", format="%m/%d/%Y"))
> dput(as.POSIXct(strptime("5/3/1992", format="%m/%d/%Y")))
> dput(strptime("5/2/1992", format="%m/%d/%Y"))
> dput(as.POSIXct(strptime("5/2/1992", format="%m/%d/%Y")))

> Sys.timezone()
[1] "COT"
> Sys.info()
 sysname  release
   version nodename  machine
   "Windows"  "7 x64" "build 7601,
Service Pack 1" "machine_name""x86"
   login user
  "username"   "username"
> conflicts()
[1] "untangle.specials" "body<-""format.pval"
"round.POSIXt"  "trunc.POSIXt"  "units"
> dput(strptime("5/3/1992", format="%m/%d/%Y"))
structure(list(sec = 0, min = 0L, hour = 0L, mday = 3L, mon = 4L,
year = 92L, wday = 0L, yday = 123L, isdst = -1L), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"))
> dput(as.POSIXct(strptime("5/3/1992", format="%m/%d/%Y")))
structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "")
> dput(strptime("5/2/1992", format="%m/%d/%Y"))
structure(list(sec = 0, min = 0L, hour = 0L, mday = 2L, mon = 4L,
year = 92L, wday = 6L, yday = 122L, isdst = 0L), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"))
> dput(as.POSIXct(strptime("5/2/1992", format="%m/%d/%Y")))
structure(704782800, class = c("POSIXct", "POSIXt"), tzone = "")


> 
> 
>> On Wed, Jun 22, 2011 at 2:40 PM, Alexander Shenkin 
>> wrote:
>>> On 6/22/2011 1:34 PM, Sarah Goslee wrote:
>>>> On Wed, Jun 22, 2011 at 2:28 PM, David
>>>> Winsemius  wrote:
>>>>>
>>>>> On Jun 22, 2011, at 2:03 PM, Sarah Goslee wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> On Wed, Jun 22, 2011 at 11:40 AM, Alexander Shenkin
>>>>>> wrote:
>>>>>>>>
>>>>>>>> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
>>>>>>>
>>>>>>> [1] FALSE
>>>>>>>>
>>>>>>>> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
>>>>>>>
>>>>>>> [1] TRUE
>>>>>>
>>>>>> I c

Re: [R] strange date problem - May 3, 1992 is NA

2011-06-22 Thread Alexander Shenkin
Wow.  Setting my timezone to UTC -5 Bogota (where there is no daylight
savings time), I get the error:

> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
[1] TRUE

But setting it to UTC -5 East Coast Time (whether or not I tell windows
to adjust for DST) I get:

> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
[1] FALSE

Can others test this on their systems?  How strange.  I wonder how much
tropical forest carbon this will account for.  :-)

Thanks,
Allie

On 6/22/2011 3:23 PM, William Dunlap wrote:
> The isdst value -1 doesn't seem right.  Shouldn't it
> be either 0 (not daylight savings time) or 1 (yes dst)?
> I've only seen isdst==-1 when all the other entries
> were NA (that happens when the string doesn't match
> the format).
> 
> In the parts of the US where daylight savings time is
> used the switchover took place near the beginning of
> April in 1992, so you should have isdst=1 by May 3.
> Perhaps you have an odd timezone file on your machine.
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com  
> 
>> -Original Message-
>> From: Alexander Shenkin [mailto:ashen...@ufl.edu] 
>> Sent: Wednesday, June 22, 2011 1:07 PM
>> To: William Dunlap
>> Subject: Re: [R] strange date problem - May 3, 1992 is NA
>>
>> Hi Bill,
>>
>> Thanks for your reply.  The results look almost identical to my eyes,
>> except for the mysterious "TRUE"...
>>
>>> methods("is.na")
>> [1] is.na.data.frame  is.na.numeric_version is.na.POSIXlt
>>> trace(methods("is.na"), quote(cat(" is.na: class(x)=", 
>> class(x), "\n")))
>> Tracing function "is.na.data.frame" in package "base"
>> Tracing function "is.na.numeric_version" in package "base"
>> Tracing function "is.na.POSIXlt" in package "base"
>> [1] "is.na.data.frame"  "is.na.numeric_version" "is.na.POSIXlt"
>>> z <- strptime("5/3/1992", format="%m/%d/%Y")
>>> is.na(z)
>> Tracing is.na.POSIXlt(z) on entry
>>  is.na: class(x)= POSIXlt POSIXt
>> [1] TRUE
>>> str(unclass(z))
>> List of 9
>>  $ sec  : num 0
>>  $ min  : int 0
>>  $ hour : int 0
>>  $ mday : int 3
>>  $ mon  : int 4
>>  $ year : int 92
>>  $ wday : int 0
>>  $ yday : int 123
>>  $ isdst: int -1
>>
>>
>>> sessionInfo()
>> R version 2.12.1 (2010-12-16)
>> Platform: i386-pc-mingw32/i386 (32-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252
>> [2] LC_CTYPE=English_United States.1252
>> [3] LC_MONETARY=English_United States.1252
>> [4] LC_NUMERIC=C
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] stats graphics  grDevices utils datasets  methods   base
>>
>> loaded via a namespace (and not attached):
>> [1] tools_2.12.1
>>
>> thanks,
>> allie
>>
>> On 6/22/2011 1:50 PM, William Dunlap wrote:
>>> What do you get for the following commands, which
>>> show which is.na method is getting called and
>>> the internal structure of the dataset made
>>> by strptime?
>>>
>>>> methods("is.na")
>>> [1] is.na.data.frame  is.na.numeric_version is.na.POSIXlt
>>>> trace(methods("is.na"), quote(cat(" is.na: class(x)=", class(x),
>>> "\n")))
>>> Tracing function "is.na.data.frame" in package "base"
>>> Tracing function "is.na.numeric_version" in package "base"
>>> Tracing function "is.na.POSIXlt" in package "base"
>>> [1] "is.na.data.frame"  "is.na.numeric_version" "is.na.POSIXlt"
>>>> z <- strptime("5/3/1992", format="%m/%d/%Y")
>>>> is.na(z)
>>> Tracing is.na.POSIXlt(z) on entry
>>>  is.na: class(x)= POSIXlt POSIXt
>>> [1] FALSE
>>>> str(unclass(z))
>>> List of 9
>>>  $ sec  : num 0
>>>  $ min  : int 0
>>>  $ hour : int 0
>>>  $ mday : int 3
>>>  $ mon  : int 4
>>>  $ year : int 92
>>>  $ wday : int 0
>>>  $ yday : int 123
>>>  $ isdst: int 1
>>>
>>>
>>>
>>> Bill Dunlap
>>> Spotfire, TIBCO Software
>>> wdunlap tibco.com  
>>>
>>>> -Original Message-
>>>> From: r-help-boun...@r-project.org 
>>>> [mailt

Re: [R] strange date problem - May 3, 1992 is NA

2011-06-22 Thread Alexander Shenkin
On 6/22/2011 1:34 PM, Sarah Goslee wrote:
> On Wed, Jun 22, 2011 at 2:28 PM, David Winsemius  
> wrote:
>>
>> On Jun 22, 2011, at 2:03 PM, Sarah Goslee wrote:
>>
>>> Hi,
>>>
>>> On Wed, Jun 22, 2011 at 11:40 AM, Alexander Shenkin 
>>> wrote:
>>>>>
>>>>> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
>>>>
>>>> [1] FALSE
>>>>>
>>>>> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
>>>>
>>>> [1] TRUE
>>>
>>> I can't reproduce your problem on R 2.13.0 on linux:
>>
>> I also cannot reproduce it on a Mac with 2.13.0 beta
> 
> Which strongly suggests that you should start by upgrading your R
> installation if at all possible.
> 
> I'd also recommend trying it on a default R session, with no extra
> packages loaded, and no items in your workspace. It's possible that
> something else is interfering.
> 
> On linux, that's achieved by typing R --vanilla at the command line.
> I'm afraid I don't know how to do it for Windows, but should be
> similarly straightforward.
> 
Thanks Sarah.  Still getting the problem.  I should surely upgrade, but
still, not a bad idea to get to the bottom of this, or at least have it
documented as a known issue.  BTW, I'm on Windows 7 Pro x64.

(running Rgui.exe --vanilla):

> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
[1] TRUE

> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
[1] FALSE

> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] strange date problem - May 3, 1992 is NA

2011-06-22 Thread Alexander Shenkin
On 6/22/2011 1:03 PM, Sarah Goslee wrote:
> Hi,
> 
> On Wed, Jun 22, 2011 at 11:40 AM, Alexander Shenkin  wrote:
>>> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
>> [1] FALSE
>>> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
>> [1] TRUE
> 
> I can't reproduce your problem on R 2.13.0 on linux:
> 
>> strptime("5/2/1992", format="%m/%d/%Y")
> [1] "1992-05-02"
>> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
> [1] FALSE
>> strptime("5/3/1992", format="%m/%d/%Y")
> [1] "1992-05-03"
>> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
> [1] FALSE
> 
> My suspicion is that you imported the data from a spreadsheet and
> there's some formatting glitch that's not showing up. If you type the
> string "5/3/1992" into the command by hand, do you still get the same
> result?

Copy and pasting the above lines reproduces the problem on my setup.

> If so, then we need to know your OS and version of R, at least.

> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252LC_MONETARY=English_United States.1252 LC_NUMERIC=C

[5] LC_TIME=English_United States.1252

attached base packages:
 [1] grid  grDevices datasets  splines   graphics  stats tcltk
   utils methods   base

other attached packages:
[1] ggplot2_0.8.9   proto_0.3-8 reshape_0.8.4   plyr_1.4
svSocket_0.9-51 TinnR_1.0.3 R2HTML_2.2  Hmisc_3.8-3
survival_2.36-2

loaded via a namespace (and not attached):
[1] cluster_1.13.2  digest_0.4.2lattice_0.19-17 svMisc_0.9-61
tools_2.12.1

> 
>> Any idea what's going on with this?  Running strptime against all dates
>> from around 1946, only 5/3/1992 was converted as "NA".  Even stranger,
>> it still seems to have a value associated with it (even though is.na
>> thinks it's NA):
>>
>>> strptime("5/3/1992", format="%m/%d/%Y")
>> [1] "1992-05-03"
> 
> This makes no sense to me.
> 
> Sarah
> 
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] strange date problem - May 3, 1992 is NA

2011-06-22 Thread Alexander Shenkin
> is.na(strptime("5/2/1992", format="%m/%d/%Y"))
[1] FALSE
> is.na(strptime("5/3/1992", format="%m/%d/%Y"))
[1] TRUE

Any idea what's going on with this?  Running strptime against all dates
from around 1946, only 5/3/1992 was converted as "NA".  Even stranger,
it still seems to have a value associated with it (even though is.na
thinks it's NA):

> strptime("5/3/1992", format="%m/%d/%Y")
[1] "1992-05-03"

Thanks,
Allie

R

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] reproduction archives

2011-06-05 Thread Alexander Shenkin
Hello Folks,

As some of my old code broke when an updated package changed its
interface, I started thinking about reproduction of analyses.  It's not
good enough to save our code - we have to save the package versions
those analyses used as well as the R-core.  I saw a couple references to
"reproduction archives" around, but nothing specific.  Is there any good
way to package up code along with the relevant packages and R version in
order to guarantee that we can reproduce our results in the future?  I
suppose that one could make note of all package versions, etc, but
automating the process would mean that it would be followed more often. 
(also, tracking down those packages and installing them all just to
reproduce a result could be an undertaking).

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] holding objects in dataframes

2011-01-14 Thread Alexander Shenkin
Hello list,

I need to keep track of objects that are related to particular
observations.  In this case, I need to keep track of polygons that are
associated with observations.  What I would ideally have is one column
of a dataframe hold a "polygonal" object (from the spatstat package). 
My question: I seem to have managed to do it, but as I haven't read that
dataframes are supposed to be able to hold list elements or objects this
way, I wanted to ask if I'm violating things here in a way such that my
code may break in future R releases.  (that is, am i relying on
non-guaranteed behavior).  My other option is to created a named list or
something along those lines, and just match() the dataframe and named
list when i need access to the related objects.  Please see below for
the code.  Thoughts and suggestions are greatly appreciated!

By tinkering around, I've found that dataframe columns do seem to be
able to hold list elements:

a=data.frame(i=c(1,2),j=c(3,4))
z=list(x=c(1,2,3))
a$k=NA
a[1,]$k=z
> str(a)

'data.frame':   2 obs. of  3 variables:
 $ i: num  1 2
 $ j: num  3 4
 $ k:List of 2
  ..$ : num  1 2 3
  ..$ : logi NA


Extending that behavior, I can attach "polygonal" objects to dataframes
it seems:

library(spatstat)
a=data.frame( a=c(1,2), b=c(3,4) )
a$polygons=NA
a[1,]$polygons = list(disc(radius=50))
> str(a)

'data.frame':   2 obs. of  3 variables:
 $ a   : num  1 2
 $ b   : num  3 4
 $ polygons:List of 2
  ..$ :List of 5
  .. ..$ type  : chr "polygonal"
  .. ..$ xrange: num  -50 50
  .. ..$ yrange: num  -50 50
  .. ..$ bdry  :List of 1
  .. .. ..$ :List of 4
  .. .. .. ..$ x   : num  50 49.9 49.8 49.5 49 ...
  .. .. .. ..$ y   : num  0 2.45 4.9 7.34 9.75 ...
  .. .. .. ..$ area: num 7851
  .. .. .. ..$ hole: logi FALSE
  .. ..$ units :List of 3
  .. .. ..$ singular  : chr "unit"
  .. .. ..$ plural: chr "units"
  .. .. ..$ multiplier: num 1
  .. .. ..- attr(*, "class")= chr "units"
  .. ..- attr(*, "class")= chr "owin"
  ..$ : logi NA

> area.owin(a[1,]$polygons[[1]])
[1] 7850.828

thanks,
allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] RODBC for 64-bit R with 32-bit Access

2010-12-17 Thread Alexander Shenkin
Hello Folks,

I do hope this is the correct place to post (and not in R-SIG-DB).  I have
spent the better part of a day searching for an answer to this question, and
have yet to resolve it.

I am trying to query an .accdb Access database (with 32-bit Office 2007
currently residing on the machine) with 64-bit R via RODBC.  There seem to
be some conflicting accounts as to whether or not this is possible.
Ripley's ODBC Connectivity PDF and the odbcConnect docs indicate it is not
possible (copied below for reference).  However, this thread (
http://r.789695.n4.nabble.com/RODBC-Access-Excel-driver-location-for-64-bit-Win7-td2542550.html)
seems to indicate that it may be possible by using
c:\windows\sysWOW64\odbcad32.exe:
"For 64-bit WIn 7, I needed to use the ODBC in
c:\windows\sysWOW64\odbcad32.exe. I configured RODBC in the 'usual' way for
the 64-bit ODBC (i.e., per the R Import/Export manual) and everything is
fine."

I have tried creating DSN's using c:\windows\sysWOW64\odbcad32.exe and
referencing those with RODBC, but I come up with errors:

> library(RODBC);
> channel <- odbcConnect("myDSN");
Warning messages:
1: In odbcDriverConnect("DSN=myDSN") :
  [RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver
Manager] The specified DSN contains an architecture mismatch between the
Driver and Application
2: In odbcDriverConnect("DSN=myDSN") : ODBC connection failed

If anyone knows any more about this issue, I would be grateful for the
information!

Thanks,
Allie

R 2.12.0
Windows 7 Pro x64
Access 2007


from Ripley's ODBC Connectivity
"32-bit Windows drivers for Access 2007 and Excel 2007 are bundled with
Office 2007 but can be installed separately via the installer
AccessDatabaseEngine.exe available from  The Access/Excel 2010 versions
at ... have a 64-bit version: however the 64-bit drivers cannot be installed
alongside 32-bit versions of Office (as far as we know, and defnitely not
for Office 2007)."

odbcConnect docs:
"You must have the 32-bit drivers when using 32-bit R and the 64-bit drivers
when using 64-bit R: otherwise there will be a cryptic message about a
driver not being found. And the 64-bit drivers cannot be installed alongside
32-bit Microsoft Office, and vice versa."

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] dmultinomial

2010-11-18 Thread Alexander Shenkin
Hello All,

I'm trying to run a maximum likelihood analysis using dmultinomial (i'm
avoiding dmultinom as I'd like to run it with vectors for the ML stuff).
 However, I'm having a hard time getting even the simplest example
running.  Any help would be greatly appreciated.

> library(mc2d)
> dmultinomial(x=c(0,0,1),prob=c(1,1,1),size=1,log=TRUE)
Error in if (ncol(x) != K) stop("x[] and prob[] must be equal length
vectors or equal col matrix.") :
  argument is of length zero

# Once I get the simple stuff above running, I'd like to be able to do
the following:

> testmat=rmultinomial(4, 1, prob)
> testmat
 [,1] [,2] [,3]
[1,]100
[2,]001
[3,]001
[4,]001
> dmultinomial(x=testmat,prob=prob,size=1,log=TRUE)
Error in if (!is.null(size) && nrow(size) != n) size <- matrix(t(size),  :
  missing value where TRUE/FALSE needed

thanks,
allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] dmultinomial

2010-11-18 Thread Alexander Shenkin
Hello All,

I'm trying to run a maximum likelihood analysis using dmultinomial (i'm
avoiding dmultinom as I'd like to run it with vectors for the ML stuff).
 However, I'm having a hard time getting even the simplest example
running.  Any help would be greatly appreciated.

> library(mc2d)
> dmultinomial(x=c(0,0,1),prob=c(1,1,1),size=1,log=TRUE)
Error in if (ncol(x) != K) stop("x[] and prob[] must be equal length
vectors or equal col matrix.") :
  argument is of length zero

# Once I get the simple stuff above running, I'd like to be able to do
the following:

> testmat=rmultinomial(4, 1, prob)
> testmat
 [,1] [,2] [,3]
[1,]100
[2,]001
[3,]001
[4,]001
> dmultinomial(x=testmat,prob=prob,size=1,log=TRUE)
Error in if (!is.null(size) && nrow(size) != n) size <- matrix(t(size),  :
  missing value where TRUE/FALSE needed

thanks,
allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] translate vector of numbers to indicies of 0/1 matrix

2010-11-17 Thread Alexander Shenkin
Hello All,

Searched around, haven't found a decent solution.

I'd like to translate a vector of numbers to a matrix (or to a list of
vectors) such that the vector values would serve as indicies of the 1's
in an otherwise-zero-filled matrix (or list of vectors). For example:

> a = c(1,3,3,4)

# perform operation

 [,1] [,2] [,3] [,4]
[1,]1000
[2,]0010
[3,]0010
[4,]0001


Any help greatly appreciated!

thanks,
allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] finding euclidean proximate points in two datasets

2010-05-20 Thread Alexander Shenkin
On 5/20/2010 9:18 AM, David Winsemius wrote:
> 
> On May 20, 2010, at 10:02 AM, Alexander Shenkin wrote:
> 
>> Hello all,
>>
>> I've been pouring through the various spatial packages, but haven't come
>> across the right thing yet.
> 
> There is a SIG for such questions.

thanks - just joined it.

>> Given a set of points in 2-d space X, i'm trying to find the subset of
>> points in Y proximate to each point in X.  Furthermore, the proximity
>> threshold of each point in X differs (X$threshold).  I've constructed
>> this myself already, but it's horrificly slow with a dataset of 40k+
>> points in one set, and a 700 in the other.
>>
>> A very inefficient example of what I'm looking for:
> 
> Not really a reproducible example. If euclidean_dist is a function ,
> then it is not one in any of the packages I have installed.

it's not reproducible - i'll make a better effort to include
reproducible code in the future.  and that function is just one i would
have written, but didn't want to clog the email with code.  Anyway, here
is a reproducible example:

X = data.frame(x=c(1,2,3), y=c(2,3,1), threshold=c(1,2,4))
Y = data.frame(x=c(5,2,3,4,2,5,2,3), y=c(5,2,2,4,1,2,3,1))
proximate=list()
i=1
for (pt in 1:length(X$x)) {
proximate[[i]] <- sqrt((X[pt,]$x - Y$x)^2 + (X[pt,]$y - Y$y)^2) >
X[pt,]$threshold
i=i+1
}
proximate

>>
>>for (pt in X$idx) {
>>proximity[i] = euclidian_dist(X[pt]$x, X[pt]$y, Y$x, Y$y) <
>> X$threshold
>> i = i+1
>>}
>>
> 
> Have you considered first creating a subset of candidate points that are
> within "threshold" of each reference point on both coordinates. That
> might sidestep a lot of calculations on points that are easily
> eliminated on a single comparison. Then you could calculate distances
> within that surviving subset of points. On average that should give you
> an over 50% "hit rate":
> 
>> (4/3)*pi*0.5^3
> [1] 0.5235988

That's a nice idea.  I'll still be waiting quite a while while my
machine cranks, but not as long.  Still - I suspect there would be much
bigger gains if there were tailored packages.  I'll re-post over on
sig-geo about that.  thanks.

>> Perhaps crossdist() in spatstat is what I should use, and then code a
>> comparison with X$threshold after the cross-distances are computed.
>> However, I was wondering if there was another tool I should be
>> considering.  Any and all thoughts are very welcome.  Thanks in advance.
>>
>> Thanks,
>> Allie
>> -- 
>> Alexander Shenkin
>> PhD Candidate
>> School of Natural Resources and Environment
>> University of Florida

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] finding euclidean proximate points in two datasets

2010-05-20 Thread Alexander Shenkin
Hello all,

I've been pouring through the various spatial packages, but haven't come
across the right thing yet.

Given a set of points in 2-d space X, i'm trying to find the subset of
points in Y proximate to each point in X.  Furthermore, the proximity
threshold of each point in X differs (X$threshold).  I've constructed
this myself already, but it's horrificly slow with a dataset of 40k+
points in one set, and a 700 in the other.

A very inefficient example of what I'm looking for:

for (pt in X$idx) {
proximity[i] = euclidian_dist(X[pt]$x, X[pt]$y, Y$x, Y$y) <
X$threshold
i = i+1
}

Perhaps crossdist() in spatstat is what I should use, and then code a
comparison with X$threshold after the cross-distances are computed.
However, I was wondering if there was another tool I should be
considering.  Any and all thoughts are very welcome.  Thanks in advance.

Thanks,
Allie
-- 
Alexander Shenkin
PhD Candidate
School of Natural Resources and Environment
University of Florida

http://snre.ufl.edu/people/students.asp

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] export dataframe's column classes to a list

2010-05-19 Thread Alexander Shenkin
It does indeed!  thank you very much, and thanks to mark leeds as well,
who emailed me off list with a similar solution.

On 5/19/2010 3:49 PM, Peter Alspach wrote:
> Tena koe Allie
>
> Does
>
> sapply(myframe, class)
>
> do what you want?
>
> Peter Alspach
>
>   
>> -Original Message-
>> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
>> project.org] On Behalf Of Alexander Shenkin
>> Sent: Thursday, 20 May 2010 8:19 a.m.
>> To: r-help@r-project.org
>> Subject: [R] export dataframe's column classes to a list
>>
>> Hi Folks,
>>
>> I want to export a dataframe's column classes to a list so that I can
>> reinstantiate the dataframe from a CSV file in the future.  (I know
>> about save(), which I'm using in addition to this).
>>
>> what I want to do is the following:
>> write.csv(myframe);
>> col_classes = get_col_classes(myframe);
>> write.csv(col_classes, "column_classes")
>>
>> ... time passes, R gets closed, etc ...
>>
>> col_classes = read.csv("column_classes")
>> new_myframe = read.csv("myfile.csv", colClasses = col_classes)
>>
>>
>> My question is how to construct the "get_col_classes()" function
>> 
> above.
>   
>>  Any thoughts are greatly appreciated!
>>
>> Thanks,
>> Allie
>>
>> --
>> Alexander Shenkin
>> PhD Candidate
>> School of Natural Resources and Environment
>> University of Florida
>>
>> http://snre.ufl.edu/people/students.asp
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] export dataframe's column classes to a list

2010-05-19 Thread Alexander Shenkin
Hi Folks,

I want to export a dataframe's column classes to a list so that I can
reinstantiate the dataframe from a CSV file in the future.  (I know
about save(), which I'm using in addition to this).

what I want to do is the following:
write.csv(myframe);
col_classes = get_col_classes(myframe);
write.csv(col_classes, "column_classes")

... time passes, R gets closed, etc ...

col_classes = read.csv("column_classes")
new_myframe = read.csv("myfile.csv", colClasses = col_classes)


My question is how to construct the "get_col_classes()" function above.
 Any thoughts are greatly appreciated!

Thanks,
Allie

-- 
Alexander Shenkin
PhD Candidate
School of Natural Resources and Environment
University of Florida

http://snre.ufl.edu/people/students.asp

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] timing a function

2010-05-17 Thread Alexander Shenkin
You could also put the call to system.time inside the function itself:

f = function(x) {
system.time({
... #function's code
ret_val = ...
}); flush.console();
return ret_val;
}

i s'pose you'd miss out on the time taken to jump to the function code,
return the value, etc, but for functions that are heavy at all, that
wouldn't trip you up.

allie

On 5/17/2010 2:06 PM, Barry Rowlingson wrote:
> On Mon, May 17, 2010 at 6:24 PM, Peter Ehlers  wrote:
>
>   
>> Try
>>  system.time(y <- f(x))
>>
>> and see ?"=".
>>
>>  -Peter Ehlers
>> 
>  Ah ha. That explains the curly brackets I saw in a posting with
> system.time on stack overflow just now:
>
>  system.time({y=f(x)})
>
>  works as expected since the {} pair make a new code block. Also you
> can then time more than one statement:
>
>  system.time({y=f(x);z=g(y)})
>
>  - gives the total time for f(x) and g(y).
>
> Barry
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] pnmath on windows

2010-05-13 Thread Alexander Shenkin
Hello All,

I'd like to enable threaded computing on my system (windows 7 pro x64)
if possible.  pnmath seems to be the most accepted solution out there
for the time being, but it requires compiling.  Are there binaries for
windows available that don't require manual compiliation?  If not, does
anyone know what the bottleneck there is?  That is, what keeps me from
compiling it myself and handing it to a friend with another windows system?

Thanks,
Allie

-- 
Alexander Shenkin
PhD Candidate
School of Natural Resources and Environment
University of Florida

http://snre.ufl.edu/people/students.asp

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R for windows 64 bit

2010-01-12 Thread Alexander Shenkin
Hi Alessia,

Note that, while your physical limit might be 6 GB, Windows memory
management allows more memory than that to be allocated (aka Virtual
Memory, or at least that's what they called it in XP).  Windows swaps
out memory from RAM to the hard disk and back when necessary (please
excuse the explanation if you already know all this).  For processing
large vectors, this swapping might bring your system to a standstill. 
Regardless, the "maximum" memory for a windows process is larger than
the physical RAM you have available.

allie

On 1/12/2010 6:27 AM, alessia matano wrote:
> Fine, it worked. I will try in this way.
>
> Just the last question and I won't bother you further today. My
> machine right now has just 6 giga of RAM (it will be increased to 16
> in a few days), and I see that with this experimental version
> memory.limit is 6135.
>
> How is the command to increase the memory usage until the maximum I
> can (5 giga?). If I am writing memory.limit(5000) it still gives me
> the error:
>
> don't be silly! Your machine has a 4Gb address limit
>
> which is quite odd.
>
> Many thanks
> Best
> A.
>
> 2010/1/12 alessia matano :
>   
>> ok, perfect!
>> I will try with it...many many thanks. Have you got there also the
>> quantreg package, which has actually the same problem of sparseM
>> (32bit version)?
>>
>> best
>> alessia
>>
>> 2010/1/12 Uwe Ligges :
>> 
>>>
>>> On 12.01.2010 12:09, alessia matano wrote:
>>>   
 I am sorry, I know it is an experimental version, and I have been
 misleading saying a new version.

 Therefore, I will wait for when they will be available officially,
 since it is just a few days.
 
>>> Or just use today my private repository I indicated in the other mail.
>>>
>>> Uwe Ligges
>>>
>>>
>>>   
 However, I tried also to go to the cran pages and download them and
 insert into the library. For quantreg it worked, for sparseM it did
 not probably because it's a win32 version, as you said.



 2010/1/12 Prof Brian Ripley:
 
> On Tue, 12 Jan 2010, alessia matano wrote:
>
>   
>> Dear all,
>>
>> I just download and set this new version of R. I am now trying to
>> download the packages I need which are sperseM and quantreg. I
>> downloaded and insert into the library file the quantreg pacjkage and
>> it seems to work. However, when I try to do the same with sparseM I
>> get the following error message:
>>
>> Loading required package: SparseM
>> Error in inDL(x, as.logical(local), as.logical(now), ...) :
>>  unable to load shared library
>> 'C:/PROGRA~1/R/R-211~1.0DE/library/SparseM/libs/SparseM.dll':
>>  LoadLibrary failure:  %1 non è un'applicazione di Win32 valida.
>>
>>
>> Any help for it?
>> 
> Please do refer to the posting referred to in that thread (and Henrique,
> please do not post just the URL without the explanations).
>
> https://stat.ethz.ch/pipermail/r-devel/2010-January/056301.html
>
> You cannot mix 32-bit Windows binary packages with this experimental port
> (it is not a 'new version'): you need to install from the package
> sources.
>  If that is too difficult for you, please do not try to use unsupported
> experimental builds (and Uwe Ligges may have some binary packages
> available
> for test in a few days).
>
>
>   
>> Thanks a lot
>> alessia
>>
>> 2010/1/11 Henrique Dallazuanna:
>> 
>>> Try this version (beta of development version):
>>>
>>> http://www.stats.ox.ac.uk/pub/RWin/Win64/R-2.11.0dev-win64.exe
>>>
>>> On Mon, Jan 11, 2010 at 2:29 PM, alessia matano
>>> wrote:
>>>   
 Dear all,

 do you know if there is any particular version of R to implement with
 windows 64 bit, in such a way to increase the amount of memory it can
 use?

 How should I increase the memory, and more importantly to set a higher
 max vector size? It still stops me saying "Could not allocate vector
 of size 145"

 thanks to all
 alessia

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

 
>>>
>>>
>>> --
>>> Henrique Dallazuanna
>>> Curitiba-Paraná-Brasil
>>> 25° 25' 40" S 49° 16' 22" O
>>>
>>>   
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-gu

Re: [R] Avoiding loops

2009-09-02 Thread Alexander Shenkin
Though, from my limited understanding, the 'apply' family of functions
are actually just loops.  Please correct me if I'm wrong.  So, while
more readable (which is important), they're not necessarily more
efficient than explicit 'for' loops.

allie

On 9/2/2009 3:13 AM, Phil Spector wrote:
> Here's one way (assuming your data frame is named dat):
>
>with(dat,
> data.frame(a,t(sapply(a,function(x){
>apply(dat[a - x >= -5 & a - x <=
> 0,c('b','c')],2,sum)}
>
>
> - Phil Spector
>  Statistical Computing Facility
>  Department of Statistics
>  UC Berkeley
>  spec...@stat.berkeley.edu
>
>
>
> On Tue, 1 Sep 2009, dolar wrote:
>
>>
>> Would like some tips on how to avoid loops as I know they are slow in R
>>
>> i've got a data frame :
>>
>> a  b  c
>> 1  5  2
>> 4  6  9
>> 5  2  3
>> 8  3  2
>>
>> What i'd like is to sum for each value of a, the sum of b and the sum
>> of c
>> where a equal to or less than (with a distance of 5)
>>
>> i.e. for row three
>> we have a=5
>> i'd like to sum up b and sum up c with the above rule
>> since 5, 4 and 1 are less than (within a distance of 5) or equal to
>> 5, then
>> we should get the following result:
>>
>> a  b   c
>> 5  13  14
>>
>> the overall result should be
>> a   b   c
>> 1   5   2
>> 4   11  11
>> 5   13  14
>> 8   11  14
>>
>> how can i do this without a loop?
>> -- 
>> View this message in context:
>> http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Regular expression to define contents between parentheses

2009-08-25 Thread Alexander Shenkin
Hi Judith,

This probably isn't the only way to do it, but:

gsub("\\(.*?\\)", "", myvector, perl=TRUE)

seems to do the trick.

The problem is that regular expressions are greedy, so you were matching
everything between the first and last parens, as you noticed.  Putting
the question mark there makes it a "minimal" matching operation. 
Apparently this is only implemented in perl regex's, or at least in that
syntax.  Hence the 'perl=TRUE'.

hth,
allie

On 8/25/2009 3:17 PM, Judith Flores wrote:
> Hello dear R-helpers,
>
>I haven't been able to figure out of find a solution in the R-help 
> archives about how to delete all the characters contained in groups of 
> parenthesis. I have a vector that looks more or less like this:
>
> myvector<-c("something (80 km/h, sd) & more (6 kg/L,sd)", "somethingelse (48 
> m/s, sd) & moretoo (50g/L , sd)")
>
> I want to extract all the strings that are not contained in parenthesis, the 
> goal would be to obtain the following new vector:
>
> subvector<-c("something & more", "somethingelse & moretoo")
>
> I tried the following, but this pattern seems to enclose all that is included 
> between the first opened parenthesis and the last closed parethesis, which 
> makes sense, but it's not what I need:
>
> subvector<-gsub("\\((.*)\\)","",myvector
>
>
> Your help will be very appreciated.
>
> Thank you,
>
> Judith
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert list to data frame while controlling column types

2009-08-24 Thread Alexander Shenkin
On 8/24/2009 2:06 AM, Petr PIKAL wrote:
> Hi
> 
> r-help-boun...@r-project.org napsal dne 23.08.2009 17:29:48:
> 
>> On 8/23/2009 9:58 AM, David Winsemius wrote:
>>> I still have problems with this statement. As I understand R, this
>> should be impossible. I have looked at both you postings and neither of
>> them clarify the issues. How can you have blanks or spaces in an R
>> numeric vector?
>>
>>
>> Just because I search numeric columns doesn't mean that my regex matches
>> them!  I posted some info on my data frame in an earlier email:
>>
>> str(final_dataf)
>> 'data.frame':   1127 obs. of  43 variables:
>>  $ block  : Factor w/ 1 level "2": 1 1 1 1 1 1 1 1 1 1 ...
>>  $ treatment  : Factor w/ 4 levels "I","M","N","T": 1 1 1 1 1 1 ...
>>  $ transect   : Factor w/ 1 level "4": 1 1 1 1 1 1 1 1 1 1 ...
>>  $ tag: chr  NA "121AL" "122AL" "123AL" ...
>> ...
>>  $ h1 : num  NA NA NA NA NA NA NA NA NA NA ...
>> ...
>>
>> You can see that I do indeed have some numeric columns.  And while I
> 
> Well, AFAICS you have a data frame with 3 columns which are factors and 1 
> which is character. I do not see any numeric column. If you want to change 
> block and transect to numeric you can use
> 
> df$block <- as.numeric(as.character(df$block))

If you take a closer look at my data frame listing, you'll see that it
is "1127 obs. of  43 variables".  I edited the column listing for
readability, and you'll see even in my editing listing I do indeed have
one numeric column - "h1".  And as I mentioned earlier, I use
colClasses, so no need to change anything to numeric here.

> 
>> search them for spaces, I only do so because my dataset isn't so large
>> as to require me to exclude them from the search.  If my dataset grows
>> too big at some point, I will exclude numeric columns, and other columns
>> which cannot hold blanks or spaces.
>>
>> To clarify further with an example:
>>
>>> df = data.frame(a=c(1,2,3,4,5),b=c("a","","c","d"," "))
>>> df = as.data.frame(lapply(df, function(x){ is.na(x) <-
>> + grep('^\\s*$',x); return(x) }), stringsAsFactors = FALSE)
>>> df
>>   ab
>> 1 1a
>> 2 2 
>> 3 3c
>> 4 4d
>> 5 5 
> 
> which can be done also by
> df[,2] <- levels(df[,2])[1:2]<-NA
> 
> but maybe with less generalization

Yes - my point was to show how I looped through an entire data frame
looking for \\s*, even when some of the columns were numeric.  I gave
this simple example with a 2-column data frame to illustrate that point.

>>> str(df)
>> 'data.frame':   5 obs. of  2 variables:
>>  $ a: num  1 2 3 4 5
>>  $ b: Factor w/ 5 levels ""," ","a","c",..: 3 NA 4 5 NA
>>
>> And one final clarification: I left out "as.data.frame" in my previous
>> solution.  So it now becomes:
>>
>>> final_dataf = as.data.frame(lapply(final_dataf, function(x){ is.na(x)
>> + <- grep('^\\s*$',x); return(x) }), stringsAsFactors = FALSE)
> 
> Again not too much of clarification, in your first data frame second 
> column is a factor with some levels you want to convert to NA, which can 
> be done by different approaches.

This clarification was to show the code that worked (for posterity), as
my previous post left out an argument.  It seems that perhaps you missed
the previous emails.

> Your final_dataf is same as df.

Yes, that is the point.  As I mentioned in the first email of this
thread, I was trying to get around as.data.frame's automatic conversion
routines, in order to retain the original column types.  And it turned
out that gsub() was more of the problem than as.data.frame() was.
Please refer to the earlier emails for more information on that.

> Columns which shall be numeric and are read as factor/character by 
> read.table likely contain some values which strictly can not be considered 
> numeric. You can see them quite often in Excel like programs and some 
> examples are
> 
> 1..2, o.5, 12.o5 and or spaces, "-" e.t.c.
> 
> and you usually need handle them by hand.
> 
> Regards
> Petr
> 
>> Hope that clarifies things, and thanks for your help.
>>
>> Thanks,
>> Allie
>>
>>
>> On 8/23/2009 9:58 AM, David Winsemius wrote:
>>> On Aug 23, 2009, at 2:47 AM, Alexander Shenkin wrote:
>>>
>>>> On 8/21/200

Re: [R] Convert list to data frame while controlling column types

2009-08-23 Thread Alexander Shenkin
On 8/23/2009 9:58 AM, David Winsemius wrote:
> I still have problems with this statement. As I understand R, this
should be impossible. I have looked at both you postings and neither of
them clarify the issues. How can you have blanks or spaces in an R
numeric vector?


Just because I search numeric columns doesn't mean that my regex matches
them!  I posted some info on my data frame in an earlier email:

str(final_dataf)
'data.frame':   1127 obs. of  43 variables:
 $ block  : Factor w/ 1 level "2": 1 1 1 1 1 1 1 1 1 1 ...
 $ treatment  : Factor w/ 4 levels "I","M","N","T": 1 1 1 1 1 1 ...
 $ transect   : Factor w/ 1 level "4": 1 1 1 1 1 1 1 1 1 1 ...
 $ tag: chr  NA "121AL" "122AL" "123AL" ...
...
 $ h1 : num  NA NA NA NA NA NA NA NA NA NA ...
...

You can see that I do indeed have some numeric columns.  And while I
search them for spaces, I only do so because my dataset isn't so large
as to require me to exclude them from the search.  If my dataset grows
too big at some point, I will exclude numeric columns, and other columns
which cannot hold blanks or spaces.

To clarify further with an example:

> df = data.frame(a=c(1,2,3,4,5),b=c("a","","c","d"," "))
> df = as.data.frame(lapply(df, function(x){ is.na(x) <-
+ grep('^\\s*$',x); return(x) }), stringsAsFactors = FALSE)
> df
  ab
1 1a
2 2 
3 3c
4 4d
5 5 
> str(df)
'data.frame':   5 obs. of  2 variables:
 $ a: num  1 2 3 4 5
 $ b: Factor w/ 5 levels ""," ","a","c",..: 3 NA 4 5 NA

And one final clarification: I left out "as.data.frame" in my previous
solution.  So it now becomes:

> final_dataf = as.data.frame(lapply(final_dataf, function(x){ is.na(x)
+ <- grep('^\\s*$',x); return(x) }), stringsAsFactors = FALSE)

Hope that clarifies things, and thanks for your help.

Thanks,
Allie


On 8/23/2009 9:58 AM, David Winsemius wrote:
> 
> On Aug 23, 2009, at 2:47 AM, Alexander Shenkin wrote:
> 
>> On 8/21/2009 3:04 PM, David Winsemius wrote:
>>>
>>> On Aug 21, 2009, at 3:41 PM, Alexander Shenkin wrote:
>>>
>>>> Thanks everyone for their replies, both on- and off-list.  I should
>>>> clarify, since I left out some important information.  My original
>>>> dataframe has some numeric columns, which get changed to character by
>>>> gsub when I replace spaces with NAs.
>>>
>>> If you used is.na() <-  that would not happen to a true _numeric_ vector
>>> (but, of course, a numeric vector in a data.frame could not have spaces,
>>> so you are probably not using precise terminology).
>>
>> I do have true numeric columns, but I loop through my entire dataframe
>> looking for blanks and spaces for convenience.
> 
> I still have problems with this statement. As I understand R, this
> should be impossible. I have looked at both you postings and neither of
> them clarify the issues. How can you have blanks or spaces in an R
> numeric vector?
> 
> 
>>
>>> You would be well
>>> advised to include the actual code rather than applying loose
>>> terminology subject you your and our misinterpretation.
>>
>> I did include code in my previous email.  Perhaps you were looking for
>> different parts.
>>
>>>
>>> ?is.na
>>>
>>>
>>> I am guessing that you were using read.table() on the original data, in
>>> which case you should look at the colClasses parameter.
>>>
>>
>> yep - I use read.csv, and I do use colClasses.  But as I mentioned
>> earlier, gsub converts those columns to characters.  Thanks for the tip
>> about is.na() <-.  I'm now using the following, thus side-stepping the
>> whole "controlling as.data.frame's column conversion" issue:
>>
>> final_dataf = lapply(final_dataf, function(x){ is.na(x) <-
>> + grep('^\\s*$',x); return(x) })
> 
> 
> Good that you have a solution.
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert list to data frame while controlling column types

2009-08-22 Thread Alexander Shenkin
On 8/21/2009 3:04 PM, David Winsemius wrote:
> 
> On Aug 21, 2009, at 3:41 PM, Alexander Shenkin wrote:
> 
>> Thanks everyone for their replies, both on- and off-list.  I should
>> clarify, since I left out some important information.  My original
>> dataframe has some numeric columns, which get changed to character by
>> gsub when I replace spaces with NAs.
> 
> If you used is.na() <-  that would not happen to a true _numeric_ vector
> (but, of course, a numeric vector in a data.frame could not have spaces,
> so you are probably not using precise terminology).

I do have true numeric columns, but I loop through my entire dataframe
looking for blanks and spaces for convenience.

> You would be well
> advised to include the actual code rather than applying loose
> terminology subject you your and our misinterpretation.

I did include code in my previous email.  Perhaps you were looking for
different parts.

> 
> ?is.na
> 
> 
> I am guessing that you were using read.table() on the original data, in
> which case you should look at the colClasses parameter.
> 

yep - I use read.csv, and I do use colClasses.  But as I mentioned
earlier, gsub converts those columns to characters.  Thanks for the tip
about is.na() <-.  I'm now using the following, thus side-stepping the
whole "controlling as.data.frame's column conversion" issue:

final_dataf = lapply(final_dataf, function(x){ is.na(x) <-
+ grep('^\\s*$',x); return(x) })

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Convert list to data frame while controlling column types

2009-08-21 Thread Alexander Shenkin
Thanks everyone for their replies, both on- and off-list.  I should
clarify, since I left out some important information.  My original
dataframe has some numeric columns, which get changed to character by
gsub when I replace spaces with NAs.  Thus, in going back to a
dataframe, those (now character) columns get converted to factors.  I
recently added stringsAsFactors to get characters to make things a bit
easier.  I wrote the column-type reset function below, but it feels
kludgey, so was wondering if there was some other way to specify how one
might want as.data.frame to handle the columns. 

str(final_dataf)
'data.frame':   1127 obs. of  43 variables:
 $ block  : Factor w/ 1 level "2": 1 1 1 1 1 1 1 1 1 1 ...
 $ treatment  : Factor w/ 4 levels "I","M","N","T": 1 1 1 1 1 1 1 1 1 1 ...
 $ transect   : Factor w/ 1 level "4": 1 1 1 1 1 1 1 1 1 1 ...
 $ tag: chr  NA "121AL" "122AL" "123AL" ...
...
 $ h1 : num  NA NA NA NA NA NA NA NA NA NA ...
...

reset_col_types <- function (df, col_types) {
# Function to reset column types in dataframes.  col_types can be
constructed
# by using lapply(class,df)

coerce_fun = list (
"character"   = `as.character`,
"factor"  = `as.factor`,
"numeric" = `as.numeric`,
"integer" = `as.integer`,
"POSIXct" = `as.POSIXct`,
"logical" = `as.logical` )

for (i in 1:length(df)) {
df[,i] = coerce_fun[[ col_types[i] ]]( df[,i] ) #apply coerce
function
}
return(df)
}

col_types = lapply(final_dataf, class)
col_types = lapply(col_types, function(x) x[length(x)])  # for posix,
take the more specified class
names(col_types)=NULL
col_types = unlist(col_types)

final_dataf = as.data.frame(lapply(final_dataf, function(x)
gsub('^\\s*$',NA,x)), stringsAsFactors = FALSE)
final_dataf = reset_col_types(final_dataf, col_types)

Thanks,
Allie


On 8/21/2009 10:54 AM, Steve Lianoglou wrote:
> Hi Allie,
>
> On Aug 21, 2009, at 11:47 AM, Alexander Shenkin wrote:
>
>> Hello all,
>>
>> I have a list which I'd like to convert to a data frame, while
>> maintaining control of the columns' data types (akin to the colClasses
>> argument in read.table).  My numeric columns, for example, are getting
>> converted to factors by as.data.frame.  Is there a way to do this, or
>> will I have to do as I am doing right now: allow as.data.frame to coerce
>> column-types as it sees fit, and then convert them back manually?
>
> This doesn't sound right ... are there characters buried in your
> numeric columns somewhere that might be causing this?
>
> I'm pretty sure this shouldn't happen, and a small test case here goes
> along with my intuition:
>
> R> a <- list(a=1:10, b=rnorm(10), c=LETTERS[1:10])
> R> df <- as.data.frame(a)
> R> sapply(df, is.factor)
> a b c
> FALSE FALSE  TRUE
>
> Can you check to see if your data's wonky somehow?
>
> -steve
>
> -- 
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>   |  Memorial Sloan-Kettering Cancer Center
>   |  Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Convert list to data frame while controlling column types

2009-08-21 Thread Alexander Shenkin
Hello all,

I have a list which I'd like to convert to a data frame, while
maintaining control of the columns' data types (akin to the colClasses
argument in read.table).  My numeric columns, for example, are getting
converted to factors by as.data.frame.  Is there a way to do this, or
will I have to do as I am doing right now: allow as.data.frame to coerce
column-types as it sees fit, and then convert them back manually?

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Symbolic references - passing variable names into functions

2009-08-12 Thread Alexander Shenkin
Hello All,

I am trying to write a function which would operate on columns of a
dataframe specified in parameters passed to that function.

f = function(dataf, col1 = "column1", col2 = "column2") {
dataf$col1 = dataf$col2 # just as an example
}

The above, of course, does not work as intended.  In some languages one
can force evaluation of a variable, and then use that evaluation as the
variable name.  Thus,

> a = "myvar"
> (operator)a = 1
> myvar
[1] 1

Is there some operator which allows this symbolic referencing in R?

Thanks,
Allie

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.