Re: [R] Replacing value with "1"

2016-09-22 Thread Henrik Bengtsson
which(df == 1, arr.ind=TRUE) is useful here:

> df <- matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA), nrow=3)
> df
 [,1] [,2] [,3] [,4] [,5]
[1,]00100
[2,]   NA0110
[3,]0100   NA

> ## Identify (row,col) indices for 1:s
> idxs <- which(df == 1, arr.ind=TRUE)
> idxs
 row col
[1,]   3   2
[2,]   1   3
[3,]   2   3
[4,]   2   4

> ## Drop any in the last column
> idxs <- idxs[idxs[,"col"] < ncol(df), , drop=FALSE]
> idxs
 row col
[1,]   3   2
[2,]   1   3
[3,]   2   3
[4,]   2   4

> idxs[,"col"] <- idxs[,"col"] + 1L
> idxs
 row col
[1,]   3   3
[2,]   1   4
[3,]   2   4
[4,]   2   5

> df[idxs] <- 1
> df
 [,1] [,2] [,3] [,4] [,5]
[1,]00110
[2,]   NA0111
[3,]0110   NA

/Henrik

On Thu, Sep 22, 2016 at 8:13 PM, Jim Lemon  wrote:
> Hi Saba,
> Try this:
>
> df<-matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA),nrow=3)
> dimdf<-dim(df)
> df1<-df==1
> df[cbind(rep(FALSE,dimdf[1]),df1[,-dimdf[2]])]<-1
>
> Jim
>
>
>
> On Fri, Sep 23, 2016 at 12:27 PM, Saba Sehrish via R-help
>  wrote:
>> Hi
>>
>> I have a matrix that contains 1565 rows and 132 columns. All the 
>> observations are either "0" or "1". Now I want to keep all the observations 
>> same but just one change, i.e. whenever there is "1", the very next value in 
>> the same row should become "1". Please see below as a sample:
>>
>>>df
>>
>>  00100
>> NA0110
>>  0100NA
>>
>> What I want is:
>>
>>
>> 00110
>>NA0111
>> 0110NA
>>
>>
>>
>> I shall be thankful for the reply.
>>
>>
>> Saba
>>
>> __
>> 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-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] Replacing value with "1"

2016-09-22 Thread Jim Lemon
Hi Saba,
Try this:

df<-matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA),nrow=3)
dimdf<-dim(df)
df1<-df==1
df[cbind(rep(FALSE,dimdf[1]),df1[,-dimdf[2]])]<-1

Jim



On Fri, Sep 23, 2016 at 12:27 PM, Saba Sehrish via R-help
 wrote:
> Hi
>
> I have a matrix that contains 1565 rows and 132 columns. All the observations 
> are either "0" or "1". Now I want to keep all the observations same but just 
> one change, i.e. whenever there is "1", the very next value in the same row 
> should become "1". Please see below as a sample:
>
>>df
>
>  00100
> NA0110
>  0100NA
>
> What I want is:
>
>
> 00110
>NA0111
> 0110NA
>
>
>
> I shall be thankful for the reply.
>
>
> Saba
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] Replacing value with "1"

2016-09-22 Thread Richard M. Heiberger
tmpf <- function(x) {
  n <- length(x)
  indices <- which(match(x,1) == 1)
  x[indices+1] <- 1
  x[1:n]  ## needed for the case when the last item in a row has value 1
}

tmp <- matrix(c(0,0,1,0,0,
NA,0,1,1,0,
0,1,0,0,NA,
1,0,1,0,1), ## last item in row has value 1
  byrow=TRUE, 4, 5)

tmp

t(apply(tmp, 1, tmpf))

On Thu, Sep 22, 2016 at 10:27 PM, Saba Sehrish via R-help
 wrote:
> Hi
>
> I have a matrix that contains 1565 rows and 132 columns. All the observations 
> are either "0" or "1". Now I want to keep all the observations same but just 
> one change, i.e. whenever there is "1", the very next value in the same row 
> should become "1". Please see below as a sample:
>
>>df
>
>  00100
> NA0110
>  0100NA
>
> What I want is:
>
>
> 00110
>NA0111
> 0110NA
>
>
>
> I shall be thankful for the reply.
>
>
> Saba
>
> __
> 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] Replacing value with "1"

2016-09-22 Thread Saba Sehrish via R-help
Hi

I have a matrix that contains 1565 rows and 132 columns. All the observations 
are either "0" or "1". Now I want to keep all the observations same but just 
one change, i.e. whenever there is "1", the very next value in the same row 
should become "1". Please see below as a sample:

>df

 00100
NA0110
 0100NA

What I want is:


00110
   NA0111
0110NA



I shall be thankful for the reply.


Saba

__
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] Memory not release when an environment is created

2016-09-22 Thread peter dalgaard

> On 22 Sep 2016, at 21:24 , luke-tier...@uiowa.edu wrote:
> 
> On Thu, 22 Sep 2016, luke-tier...@uiowa.edu wrote:
> 
>> My preference is to use a top level function in the package or global
>> env that takes as arguments just the variables I want in the parent
>> frame. That avoids the explicit environment manipulations. Here that
>> would be
>> 
>>> makeFunc0 <- function(xmin, xmax)
>> function(y) (y - xmin) / (xmax - xmin)
>> 
> 
> But I do keep forgetting the need to force the parameters if you don't
> want a big value to stick around until the returned function is used
> the first time,

...not to mention the pains that arise if the makeFunc0 gets called with 
argument expressions that involve items that may change before the first call 
of the returned function.

> so a better definition of makeFUnc0 is
> 
> makeFunc0 <- function(xmin, xmax) {
>force(xmin)
>force(xmax)
>function(y) (y - xmin) / (xmax - xmin)
> }
> 
> Best,
> 
> luke
> 
>>> makeFunc1 <- function(x)
>> makeFunc0(min(x), max(x))
>> 
>>> f <- makeFunc1(1:1e8)
>>> ls.str(all=TRUE, environment(f))
>> xmax :  int 1
>> xmin :  int 1
>>> parent.env(environment(f))
>> 
>>> f(c(1234567, 2345678))
>> [1] 0.01234566 0.02345677
>> 
>> Best,
>> 
>> luke
>> 
>> 
>> On Thu, 22 Sep 2016, William Dunlap via R-help wrote:
>> 
>>> I like to have my function-returning functions use new.env(parent=XXX)
>>> to make an environment for the returned function and put into it only
>>> the objects needed by the function.  The 'XXX' should be a an environment
>>> which will hang around anyway.  It could be globalenv(), but if your
>>> function
>>> is in a package, as.environment(paste0("package:", .packageName))
>>> would work well.  The later ensures the your returned function has access
>>> to all the other functions in that package.
>>> E.g.,
 makeFunc1 <- function(x) {
>>>   envir <- new.env(parent = environment(sys.function()))
>>>   envir$xmax <- max(x)
>>>   envir$xmin <- min(x)
>>>   with(envir, function(y) (y - xmin) / (xmax - xmin))
>>> }
 f <- makeFunc1(1:1e8)
 ls.str(all=TRUE, environment(f))
>>> xmax :  int 1
>>> xmin :  int 1
 parent.env(environment(f))
>>> 
 f(c(1234567, 2345678))
>>> [1] 0.01234566 0.02345677
>>> Bill Dunlap
>>> TIBCO Software
>>> wdunlap tibco.com
>>> On Thu, Sep 22, 2016 at 8:41 AM, Olivier Merle 
>>> wrote:
 Dear,
 When I use big data for a temporary use it seems that the memory is not
 released when a function/environement is created nearby.
 Here the reproducible exemple:
 test<-function(){
 x=matrix(0,5,1)
 y=function(nb) nb^2
 return(y)
 }
 xx=test() # 3 Go of Ram is used
 gc() # Memory is not released !! even if x has been destroyed [look into
 software mem used]
 format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size
 of
 the object
 rm(xx)
 gc() # Memory is released
 ## Classic
 test2<-function(){
 x=matrix(0,5,1)
 y=1
 return(y)
 }
 xx=test2() # Memory is used
 gc() # => Memory is released
 How can I release the data in test without destroying the xx object ? As x
 which is big object is destroyed, I though I could get my memory back but
 it seems that the function y is keeping the x object.
 Best
 
[[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.
>>> 
>>> [[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.
>> 
>> 
> 
> -- 
> Luke Tierney
> Ralph E. Wareham Professor of Mathematical Sciences
> University of Iowa  Phone: 319-335-3386
> Department of Statistics andFax:   319-335-3017
>   Actuarial Science
> 241 Schaeffer Hall  email:   luke-tier...@uiowa.edu
> Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu
> 
> __
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Pri

Re: [R] [R-pkg-devel] doc url to vignette

2016-09-22 Thread S Ellison
> In other words, try to mislead CRAN.  

Well, no. The thought was that if CRAN has agreed an exception, as Uwe had 
indicated, you might want a simpler way of maintaining it than discussing it on 
every update.

I can see that that would sidestep an enforced regular review, though.

Keep up the good work; it is much appreciated.

Steve E

***
This email and any attachments are confidential. Any use...{{dropped:8}}

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


Re: [R] Help with PCA data file prep and R code

2016-09-22 Thread David L Carlson
Looking at your data there are several issues.

1. Tank is an integer, but it sounds like you intend to use it as a categorical 
measure. If so that it should be a factor, but factors cannot be used in pca. 
Is Tank 10 10 times more of something than Tank 1?

2. Date is a factor. That means you are not measuring time, just the fact that 
2 rows are the same time or different time. Factors cannot be used in pca.

3. Treatment is a factor, but factors cannot be used in pca.

4. Your log transformed data has many 0's and no negative values. Did you add 1 
to each value before taking logarithms?

First line of your code after reading the data:

> meso.pca <- prcomp(mesocleaned, center=TRUE, scale.=TRUE)
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
>scale. = TRUE)

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


-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Sarah Stinson
Sent: Wednesday, September 21, 2016 10:25 AM
To: r-help@r-project.org
Subject: Re: [R] Help with PCA data file prep and R code

Hello DRUGs,
I'm new to R and would appreciate some expert advice on prepping files for,
and running, PCA...

My data set consists of aquatic invertebrate and zooplankton count data and
physicochemical measurements from an ecotoxicology study. Four chemical
treatments were applied to mesocosm tanks, 4 replicates per treatment (16
tanks total), then data were collected weekly over a 3 month period.

I cleaned the data in excel by removing columns with all zero values, and
all rows with NA values.
All zooplankton values were volume normalized, then log normalized. All
other data was log normalized in excel prior to analysis in R. All vectorss
are numeric. I've attached the .txt file to this email rather that using
dput(dataframe).

My questions are:

1. Did I do the cleaning step appropriately? I know that there are ways to
run PCA's using data that contain NA values (pcaMethods), but wasn't able
to get the code to work...
(I understand that this isn't strictly an R question, but any help would be
appreciated.)
2. Does my code look correct for the PCA and visualization (see below)?

Thanks in advance,
Sarah

#read data
mesocleaned <- read.csv("MesoCleanedforPCA.9.16.16.csv")

#run PCA
meso.pca <- prcomp(mesocleaned,
   center = TRUE,
   scale. = TRUE)

# print method
print(meso.pca)

#compute standard deviation of each principal component
std_dev <- meso.pca$sdev

#compute variance
pr_var <- std_dev^2

#check variance of first 10 components
pr_var[1:10]

#proportion of variance explained
prop_varex <- pr_var/sum(pr_var)
prop_varex[1:20]

#The first principal component explains 12.7% of the variance
#The second explains 8.1%

#visualize
biplot(meso.pca)

#for visualization, make Treatment vector a factor instead of numeric
meso.treatment <- as.factor(mesocleaned[, 3])

#ggbiplot to visualize by Treatment group
#reference: https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/

library(devtools)
install_github("ggbiplot", "vqv")
library(ggbiplot)

print(ggbiplot(meso.pca, obs.scale = 1, var.scale = 1, groups =
meso.treatment, ellipse = TRUE, circle = TRUE))
g <- ggbiplot(meso.pca, obs.scale = 1, var.scale = 1,
  groups = meso.treatment, ellipse = TRUE,
  circle = TRUE)
g <- g + scale_color_brewer(name = deparse(substitute(Treatments)), palette
= 'Dark2') #must change meso.treatment to a factor for this to work
g <- g + theme(legend.direction = 'horizontal',
   legend.position = 'top')
print(g)

#Circle plot
#plot each variables coefficients inside a unit circle to get insight on a
possible interpretation for PCs.
#reference: https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/

theta <- seq(0,2*pi,length.out = 100)
circle <- data.frame(x = cos(theta), y = sin(theta))
p <- ggplot(circle,aes(x,y)) + geom_path()

loadings <- data.frame(meso.pca$rotation,
   .names = row.names(meso.pca$rotation))
p + geom_text(data=loadings,
  mapping=aes(x = PC1, y = PC2, label = .names, colour =
.names)) +
  coord_fixed(ratio=1) +
  labs(x = "PC1", y = "PC2")

On Tue, Sep 20, 2016 at 10:28 PM, Sarah Stinson 
wrote:

> Hello DRUGs,
> I'm new to R and would appreciate some expert advice on prepping files
> for, and running, PCA...
>
> My data set consists of aquatic invertebrate and zooplankton count data
> and physicochemical measurements from an ecotoxicology study. Four chemical
> treatments were applied to mesocosm tanks, 4 replicates per treatment (16
> tanks total), then data were collected weekly over a 3 month period.
>
> I cleaned the data in excel by removing columns with all zero values, and
> all rows with NA values.
> All zooplankton values were volume normalized, then log normalized. All
> other data was log normalized in excel prior to analysis in R. A

Re: [R] mgcv: bam(), error in models with random intercepts and random slopes

2016-09-22 Thread Fotis Fotiadis
Dear Professor Wood,

Thank you for taking the time to fix the problem.

Best,
Fotis



On Thu, Sep 22, 2016 at 4:25 PM, Simon Wood  wrote:

> Hi Fotis,
>
> Thanks for the report, and sending me the data and code (off list). The
> problem is triggered by 'ctrial' being a (one column) matrix. An immediate
> fix is
>
> data_a$ctrial <- as.numeric(data_a$ctrial)
>
> - mgcv 1.8-16 will catch the problem automatically internally.
>
> best,
> Simon
>
> On 20/09/16 17:22, Fotis Fotiadis wrote:
>
>> Hi all
>>
>> I am using the bam function of the mgcv package to model behavioral data
>> of
>> a learning experiment. To model individual variation in learning rate, I
>> am
>> testing models with (a) by-participant random intercepts of trial, (b)
>> by-participant random slopes and random intercepts of trial, and (c)
>> by-participant random smooth terms.
>>
>> While all (a) and (c) models converge, I am getting an error for every
>> possible variation of a model with random intercepts and random slopes.
>> For
>> example:
>>
>> m1.rs<-bam(acc~ 1 + igc + s(ctrial) + s(sbj, bs="re") + s(ctrial, sbj,
>> bs="re") , data=data_a, family=binomial)
>> Error in G$smooth[[i]]$first.para:G$smooth[[i]]$last.para :
>>argument of length 0
>>
>> Any idea on what that error might be?
>>
>> Thank you in advance for your time.
>> Fotis
>>
>> P.S.: R version: 3.3.1, mgcv version: 1.8.15
>>
>>
>
> --
> Simon Wood, School of Mathematics, University of Bristol BS8 1TW UK
> +44 (0)117 33 18273 http://www.maths.bris.ac.uk/~sw15190
>
>


-- 
PhD Candidate
Department of Philosophy and History of Science
University of Athens, Greece.
http://users.uoa.gr/~aprotopapas/LLL/en/members.html#fotisfotiadis

Notice: Please do not use this account for social networks invitations, for
sending chain-mails to me, or as it were a facebook account. Thank you for
respecting my privacy.

[[alternative HTML version deleted]]

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


Re: [R] Memory not release when an environment is created

2016-09-22 Thread luke-tierney

On Thu, 22 Sep 2016, luke-tier...@uiowa.edu wrote:


My preference is to use a top level function in the package or global
env that takes as arguments just the variables I want in the parent
frame. That avoids the explicit environment manipulations. Here that
would be


makeFunc0 <- function(xmin, xmax)

 function(y) (y - xmin) / (xmax - xmin)



But I do keep forgetting the need to force the parameters if you don't
want a big value to stick around until the returned function is used
the first time, so a better definition of makeFUnc0 is

makeFunc0 <- function(xmin, xmax) {
force(xmin)
force(xmax)
function(y) (y - xmin) / (xmax - xmin)
}

Best,

luke


makeFunc1 <- function(x)

 makeFunc0(min(x), max(x))


f <- makeFunc1(1:1e8)
ls.str(all=TRUE, environment(f))

xmax :  int 1
xmin :  int 1

parent.env(environment(f))



f(c(1234567, 2345678))

[1] 0.01234566 0.02345677

Best,

luke


On Thu, 22 Sep 2016, William Dunlap via R-help wrote:


I like to have my function-returning functions use new.env(parent=XXX)
to make an environment for the returned function and put into it only
the objects needed by the function.  The 'XXX' should be a an environment
which will hang around anyway.  It could be globalenv(), but if your
function
is in a package, as.environment(paste0("package:", .packageName))
would work well.  The later ensures the your returned function has access
to all the other functions in that package.

E.g.,

makeFunc1 <- function(x) {

   envir <- new.env(parent = environment(sys.function()))
   envir$xmax <- max(x)
   envir$xmin <- min(x)
   with(envir, function(y) (y - xmin) / (xmax - xmin))
}

f <- makeFunc1(1:1e8)
ls.str(all=TRUE, environment(f))

xmax :  int 1
xmin :  int 1

parent.env(environment(f))



f(c(1234567, 2345678))

[1] 0.01234566 0.02345677



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Sep 22, 2016 at 8:41 AM, Olivier Merle 
wrote:


Dear,

When I use big data for a temporary use it seems that the memory is not
released when a function/environement is created nearby.
Here the reproducible exemple:

test<-function(){
x=matrix(0,5,1)
y=function(nb) nb^2
return(y)
}
xx=test() # 3 Go of Ram is used
gc() # Memory is not released !! even if x has been destroyed [look into
software mem used]
format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size
of
the object
rm(xx)
gc() # Memory is released

## Classic
test2<-function(){
x=matrix(0,5,1)
y=1
return(y)
}
xx=test2() # Memory is used
gc() # => Memory is released

How can I release the data in test without destroying the xx object ? As x
which is big object is destroyed, I though I could get my memory back but
it seems that the function y is keeping the x object.

Best

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



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






--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa  Phone: 319-335-3386
Department of Statistics andFax:   319-335-3017
   Actuarial Science
241 Schaeffer Hall  email:   luke-tier...@uiowa.edu
Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu

__
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] Memory not release when an environment is created

2016-09-22 Thread luke-tierney

My preference is to use a top level function in the package or global
env that takes as arguments just the variables I want in the parent
frame. That avoids the explicit environment manipulations. Here that
would be


makeFunc0 <- function(xmin, xmax)

  function(y) (y - xmin) / (xmax - xmin)


makeFunc1 <- function(x)

  makeFunc0(min(x), max(x))


f <- makeFunc1(1:1e8)
ls.str(all=TRUE, environment(f))

xmax :  int 1
xmin :  int 1

parent.env(environment(f))



f(c(1234567, 2345678))

[1] 0.01234566 0.02345677

Best,

luke


On Thu, 22 Sep 2016, William Dunlap via R-help wrote:


I like to have my function-returning functions use new.env(parent=XXX)
to make an environment for the returned function and put into it only
the objects needed by the function.  The 'XXX' should be a an environment
which will hang around anyway.  It could be globalenv(), but if your
function
is in a package, as.environment(paste0("package:", .packageName))
would work well.  The later ensures the your returned function has access
to all the other functions in that package.

E.g.,

makeFunc1 <- function(x) {

   envir <- new.env(parent = environment(sys.function()))
   envir$xmax <- max(x)
   envir$xmin <- min(x)
   with(envir, function(y) (y - xmin) / (xmax - xmin))
}

f <- makeFunc1(1:1e8)
ls.str(all=TRUE, environment(f))

xmax :  int 1
xmin :  int 1

parent.env(environment(f))



f(c(1234567, 2345678))

[1] 0.01234566 0.02345677



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Sep 22, 2016 at 8:41 AM, Olivier Merle 
wrote:


Dear,

When I use big data for a temporary use it seems that the memory is not
released when a function/environement is created nearby.
Here the reproducible exemple:

test<-function(){
x=matrix(0,5,1)
y=function(nb) nb^2
return(y)
}
xx=test() # 3 Go of Ram is used
gc() # Memory is not released !! even if x has been destroyed [look into
software mem used]
format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size
of
the object
rm(xx)
gc() # Memory is released

## Classic
test2<-function(){
x=matrix(0,5,1)
y=1
return(y)
}
xx=test2() # Memory is used
gc() # => Memory is released

How can I release the data in test without destroying the xx object ? As x
which is big object is destroyed, I though I could get my memory back but
it seems that the function y is keeping the x object.

Best

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



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



--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa  Phone: 319-335-3386
Department of Statistics andFax:   319-335-3017
   Actuarial Science
241 Schaeffer Hall  email:   luke-tier...@uiowa.edu
Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu

__
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] Memory not release when an environment is created

2016-09-22 Thread William Dunlap via R-help
I like to have my function-returning functions use new.env(parent=XXX)
to make an environment for the returned function and put into it only
the objects needed by the function.  The 'XXX' should be a an environment
which will hang around anyway.  It could be globalenv(), but if your
function
is in a package, as.environment(paste0("package:", .packageName))
would work well.  The later ensures the your returned function has access
to all the other functions in that package.

E.g.,
> makeFunc1 <- function(x) {
envir <- new.env(parent = environment(sys.function()))
envir$xmax <- max(x)
envir$xmin <- min(x)
with(envir, function(y) (y - xmin) / (xmax - xmin))
}
> f <- makeFunc1(1:1e8)
> ls.str(all=TRUE, environment(f))
xmax :  int 1
xmin :  int 1
> parent.env(environment(f))

> f(c(1234567, 2345678))
[1] 0.01234566 0.02345677



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Sep 22, 2016 at 8:41 AM, Olivier Merle 
wrote:

> Dear,
>
> When I use big data for a temporary use it seems that the memory is not
> released when a function/environement is created nearby.
> Here the reproducible exemple:
>
> test<-function(){
> x=matrix(0,5,1)
> y=function(nb) nb^2
> return(y)
> }
> xx=test() # 3 Go of Ram is used
> gc() # Memory is not released !! even if x has been destroyed [look into
> software mem used]
> format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size
> of
> the object
> rm(xx)
> gc() # Memory is released
>
> ## Classic
> test2<-function(){
> x=matrix(0,5,1)
> y=1
> return(y)
> }
> xx=test2() # Memory is used
> gc() # => Memory is released
>
> How can I release the data in test without destroying the xx object ? As x
> which is big object is destroyed, I though I could get my memory back but
> it seems that the function y is keeping the x object.
>
> Best
>
> [[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.
>

[[alternative HTML version deleted]]

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


Re: [R] Variable String formation

2016-09-22 Thread David Winsemius

> On Sep 22, 2016, at 8:01 AM, Roy Mendelssohn - NOAA Federal 
>  wrote:
> 
> Hi All:
> 
> I am trying to write code to create a string to be executed as a command.  
> The string will be of the form:
> 
> "param <- param[,rev(seq_len(dataYLen)),,drop = FALSE]"
> 
> Now just creating that string is simple enough.  Where the problem arises is 
> the array param could be 2, 3, or 4 dimensions, and the dimension where  
> "rev(seq_len(dataYLen))" occurs can vary.  At present I have the following 
> solution:
> 
>  paramLen <-  3
>  latLoc <- 2
>  myComma1 <- paste(rep(',', times = (latLoc-1)), 
> 'rev(seq_len(dataYLen))', sep="", collapse="")
>  myComma2 <- paste(rep(',', times = (paramLen-latLoc+1)),sep="", 
> collapse="")
>  paramCommand <- paste0('param <- param[', myComma1, myComma2, 'drop = 
> FALSE]')
> 
> (paramLen can be 2,3,4 and latLoc can be 1,2,3,4)  but this strikes me as 
> pretty kludgy.  I am hoping there is a more elegant way of doing this.
> 

 Take a look at this function to see if it allows you to make this cleaner:

?R.utils::extract.array 

(Author = Henrik Bengtsson; so you have can have confidence in its quality.)

Found with the ever-useful `findFn` function:

findFn("extract slice array")
found 28 matches;  retrieving 2 pages
2 
Downloaded 28 links in 24 packages.

To whose author/maintainer I give almost daily silent thank yous:

 maintainer('sos')
[1] "Spencer Graves "


> Thanks,
> 
> -Roy
> 
> 


David Winsemius
Alameda, CA, USA

__
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] Memory not release when an environment is created

2016-09-22 Thread David Winsemius

> On Sep 22, 2016, at 9:45 AM, Ismail SEZEN  wrote:
> 
> 
>> On 22 Sep 2016, at 18:41, Olivier Merle  wrote:
>> 
>> Dear,
>> 
>> When I use big data for a temporary use it seems that the memory is not
>> released when a function/environement is created nearby.
>> Here the reproducible exemple:
>> 
>> test<-function(){
>> x=matrix(0,5,1)
>> y=function(nb) nb^2
>> return(y)
>> }
>> xx=test() # 3 Go of Ram is used
>> gc() # Memory is not released !! even if x has been destroyed [look into
>> software mem used]
> 
> Because y is a function and returns with its own environment.
> 
> ls(environment(xx)) # x and y objects are still there
> 
>> How can I release the data in test without destroying the xx object ? As x
>> which is big object is destroyed, I though I could get my memory back but
>> it seems that the function y is keeping the x object.
> 
> if you do not need the x object in y function then remove it in it’s own 
> environment as follows;
> 
>> test<-function(){
>> x=matrix(0,5,1)
>   rm(x)
>> y=function(nb) nb^2
>> return(y)
>> }
> 
> or if you need to remove it out of the function;
> 
> rm("x", envir = environment(xx))
> ls(environment(xx)) # x has gone

That's much clearer than my `eval(quote(...` approach. I had forgotten that 
`rm` had an 'environment' parameter. I had considered trying:

environment(xx)$x <- NULL   # but after looking at `environment`'s help page I 
was pretty sure it would have failed 

# But there again I was wrong:

test<-function(){
x=matrix(0,500,100)
y=function(nb) nb^2
return(y)
}
xx=test()

environment(xx)$x <- NULL


> object.size( get("x", envir=environment(xx) ) )
0 bytes

I still think rm() is the way to go but this offers another illustration of 
available methods of environment mangling, er, manipulation.

Best;
David.


> 
> If y function uses x somehow, then you will need to live with a big object.
> __
> 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.

David Winsemius
Alameda, CA, USA

__
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] Memory not release when an environment is created

2016-09-22 Thread David Winsemius

> On Sep 22, 2016, at 8:41 AM, Olivier Merle  wrote:
> 
> Dear,
> 
> When I use big data for a temporary use it seems that the memory is not
> released when a function/environement is created nearby.
> Here the reproducible exemple:
> 
> test<-function(){
> x=matrix(0,5,1)
> y=function(nb) nb^2
> return(y)
> }
> xx=test() # 3 Go of Ram is used
> gc() # Memory is not released !! even if x has been destroyed [look into
> software mem used]

Looking at this I would imagine that the unreleased memory is allocated within 
the `xx` objects environment (since functions in R are actually closures that 
carry along their environments of creation.


> format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size of
> the object
> rm(xx)
> gc() # Memory is released
> 
> ## Classic
> test2<-function(){
> x=matrix(0,5,1)
> y=1
> return(y)
> }
> xx=test2() # Memory is used
> gc() # => Memory is released
> 
> How can I release the data in test without destroying the xx object ? As x
> which is big object is destroyed, I though I could get my memory back but
> it seems that the function y is keeping the x object.

That's how I understand the semantics of R. You would need to replace the 
environment that is attached to `xx`. I madesomwaht smaller object:

test<-function(
x=matrix(0,500,
y=function(nb) 
return(y)
}
xx=test() 

> environment(xx)


> object.size(xx)
1384 bytes
> ls(x, envir=environment(xx) )
[1] "x" "y"
> ?get
> object.size( get("x", envir=environment(xx) ) )
400200 bytes

Even with that perspective in mind it still took me two tries to get rid of the 
xx object, since my abilities to "program on the language" are still fairly 
modest.:

> eval( x <- NULL, envir=environment(xx) )
NULL
> object.size( get("x", envir=environment(xx) ) )
400200 bytes
> eval( quote(x <- NULL), envir=environment(xx) )
> object.size( get("x", envir=environment(xx) ) )
0 bytes

So now I have learned that object.size does not include measurements of the 
size of function environments, a fact about which I was not aware.

Best;
David.

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

David Winsemius
Alameda, CA, USA

__
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] Memory not release when an environment is created

2016-09-22 Thread Ismail SEZEN

> On 22 Sep 2016, at 18:41, Olivier Merle  wrote:
> 
> Dear,
> 
> When I use big data for a temporary use it seems that the memory is not
> released when a function/environement is created nearby.
> Here the reproducible exemple:
> 
> test<-function(){
> x=matrix(0,5,1)
> y=function(nb) nb^2
> return(y)
> }
> xx=test() # 3 Go of Ram is used
> gc() # Memory is not released !! even if x has been destroyed [look into
> software mem used]

Because y is a function and returns with its own environment.

ls(environment(xx)) # x and y objects are still there

> How can I release the data in test without destroying the xx object ? As x
> which is big object is destroyed, I though I could get my memory back but
> it seems that the function y is keeping the x object.

if you do not need the x object in y function then remove it in it’s own 
environment as follows;

> test<-function(){
> x=matrix(0,5,1)
   rm(x)
> y=function(nb) nb^2
> return(y)
> }

or if you need to remove it out of the function;

rm("x", envir = environment(xx))
ls(environment(xx)) # x has gone

 If y function uses x somehow, then you will need to live with a big object.
__
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] Memory not release when an environment is created

2016-09-22 Thread Olivier Merle
Dear,

When I use big data for a temporary use it seems that the memory is not
released when a function/environement is created nearby.
Here the reproducible exemple:

test<-function(){
x=matrix(0,5,1)
y=function(nb) nb^2
return(y)
}
xx=test() # 3 Go of Ram is used
gc() # Memory is not released !! even if x has been destroyed [look into
software mem used]
format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size of
the object
rm(xx)
gc() # Memory is released

## Classic
test2<-function(){
x=matrix(0,5,1)
y=1
return(y)
}
xx=test2() # Memory is used
gc() # => Memory is released

How can I release the data in test without destroying the xx object ? As x
which is big object is destroyed, I though I could get my memory back but
it seems that the function y is keeping the x object.

Best

[[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] Variable String formation

2016-09-22 Thread Roy Mendelssohn - NOAA Federal
Hi All:

I am trying to write code to create a string to be executed as a command.  The 
string will be of the form:

"param <- param[,rev(seq_len(dataYLen)),,drop = FALSE]"

Now just creating that string is simple enough.  Where the problem arises is 
the array param could be 2, 3, or 4 dimensions, and the dimension where  
"rev(seq_len(dataYLen))" occurs can vary.  At present I have the following 
solution:

  paramLen <-  3
  latLoc <- 2
  myComma1 <- paste(rep(',', times = (latLoc-1)), 'rev(seq_len(dataYLen))', 
sep="", collapse="")
  myComma2 <- paste(rep(',', times = (paramLen-latLoc+1)),sep="", 
collapse="")
  paramCommand <- paste0('param <- param[', myComma1, myComma2, 'drop = 
FALSE]')

(paramLen can be 2,3,4 and latLoc can be 1,2,3,4)  but this strikes me as 
pretty kludgy.  I am hoping there is a more elegant way of doing this.

Thanks,

-Roy

**
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

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


Re: [R] "invalid argument to unary operator" while selecting rows by name

2016-09-22 Thread ruipbarradas

Hello,

data["601",] doesn't generate an error because you can also refer to a  
row by its name, as an alternative to refering to it by row number.  
It's the same with vectors, just consider the following case.


(x <- c("601"=1, b=2))
x[1]
x["601"]  # the same

But when you want to remove it you must negate an index number so  
x[-"601"] is wrong for reasons already explained.


Rui Barradas


Citando Pauline Laïlle :


Hi, thanks for the answer.
In this case, the row named "601" is not the 601st row of the table, but
the 117th. data[601,] actually refers to a non existing row.
I was wondering why data[-"601,] generates an error message whereas
data["601",] does not?

2016-09-20 19:08 GMT+02:00 Bert Gunter :


Hint: "601"  is not 601.

Have you gone through any R tutorials?

Cheers,
Bert
Bert Gunter

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


On Tue, Sep 20, 2016 at 5:42 AM, Pauline Laïlle
 wrote:
> Dear all,
>
> I built a dataframe with read.csv2(). Initially, row names are integers
> (order of answers to a survey). They are listed in the csv's first
column.
> The import works well and my dataframe looks like I wanted it to look.
>
> Row names go as follows :
>  [1] "6"   "29"  "31"  "32"  "52"  "55"  "63"  "71"  "72"  "80"  "88"
"89"
>  "91"  "93"  "105" "110" "111" "117" "119" "120"
>  [21] "122" "127" "128" "133" "137" "140" "163" "165" "167" "169" "177"
> "178" "179" "184" "186" "192" "193" "200" "201" "228"
> etc.
>
> I would like to drop rows "601" & "604" to clean the dataframe.
>
> While data["601",] shows me the first row i'd like to drop, data[-"601",]
> returns the following :
> Error in -"601" : invalid argument to unary operator
>
> idem with data[c("601","604"),] and data[-c("601","604"),]
>
> It is the first time that I run into this specific error. After reading a
> bit about it I still don't understand what it means and how to fix it.
>
> Thanks for reading!
> Best,
> Pauline.
>
> [[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.



[[alternative HTML version deleted]]

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


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

Re: [R] Add annotation text outside of an xyplot (lattice package)

2016-09-22 Thread Carlos Ortega
Hi,

Yes, you can use "latticeExtra" package and use a text layer on top of your
current chart.

Thanks,
Carlos Ortega

2016-09-22 16:04 GMT+02:00 Jun Shen :

> Dear list,
>
> Just wonder if there is a way to add annotation text outside an xyplot,
> (e.g. the bottom of the plot). the panel.text seems only add text within
> the plot. Thanks.
>
> Jun
>
> [[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.
>



-- 
Saludos,
Carlos Ortega
www.qualityexcellence.es

[[alternative HTML version deleted]]

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


Re: [R] `head` doesn't show all columns for an empty data.frame

2016-09-22 Thread Duncan Murdoch

On 22/09/2016 4:45 AM, Colin Phillips wrote:

I'm sure I'm doing something wrong, but I'm seeing strange behaviour using the 
`head` and `tail` functions on an empty data.frame.

To reproduce:
# create an empty data frame.  I actually read an empty table from Excel using 
`readWorkbook` from package `openxlsx`
test <- structure(list(Code = NULL, Name = NULL, Address = NULL, Sun.Hrs = NULL,
Mon.Hrs = NULL), .Names = c("Code", "Name", "Address", "Sun.Hrs",
"Mon.Hrs"), class = "data.frame", row.names = integer(0))


That's not a valid dataframe, it's just labelled as one.  If you tried 
to create it with data.frame(), you'd get something different.


> test <- data.frame(Code = NULL, Name = NULL, Address = NULL, Sun.Hrs 
= NULL,

+ Mon.Hrs = NULL)
> test
data frame with 0 columns and 0 rows

You can create a zero-row dataframe as long as you put 0-length vectors 
in as columns.  NULL is not a vector.


> test <- data.frame(Code = numeric(0), Name = numeric(0), Address = 
numeric(0), Sun.Hrs = numeric(0),

+ Mon.Hrs = numeric(0))
> test
[1] CodeNameAddress Sun.Hrs Mon.Hrs
<0 rows> (or 0-length row.names)

If you do that, head() works:

> head(test)
[1] CodeNameAddress Sun.Hrs Mon.Hrs
<0 rows> (or 0-length row.names)

So this is a bug in openxlsx.  It's also a well-known limitation of the 
S3 object system:  you can easily create things that are labelled with a 
certain class, but aren't valid objects of that class.


Duncan Murdoch

__
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] Add annotation text outside of an xyplot (lattice package)

2016-09-22 Thread Jun Shen
Dear list,

Just wonder if there is a way to add annotation text outside an xyplot,
(e.g. the bottom of the plot). the panel.text seems only add text within
the plot. Thanks.

Jun

[[alternative HTML version deleted]]

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


Re: [R] "invalid argument to unary operator" while selecting rows by name

2016-09-22 Thread Pauline Laïlle
Tanks for all your answers and for taking the time to help me better
understand my mistake.
I will take your advice and do some reading!
Best,
P.

Le mercredi 21 septembre 2016, William Dunlap  a écrit :

> The OP cannot be entirely blamed for thinking that x[,-"ColName"]
> would omit x's "ColName" from the result.  Base R and many packages
> have commonly used functions that do context-sensitive (aka 'nonstandard')
> evaluation.
>
> E.g. subset() evaluates each argument in a different way:
>   > subset(data.frame(ColA=1:3,ColB=-(11:13)), -ColB>11, -ColA)
> ColB
>   2  -12
>   3  -13
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Wed, Sep 21, 2016 at 7:45 AM, Bert Gunter  > wrote:
>
>> No, Rui, your example misses the point. Your initial sentence hits it.
>>
>> The OP needs to carefully read
>> ?"["
>> and/or spend some time with a suitable R tutorial to learn proper
>> syntax for subscripting. Asking foolish questions in lieu of doing her
>> homework seems wrongheaded to me. Others may disagree, of course.
>>
>> Cheers,
>> Bert
>>
>>
>>
>>
>> Cheers,
>> Bert
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Wed, Sep 21, 2016 at 1:26 AM,  > > wrote:
>> > Hello,
>> >
>> > The error message means exactly what it says. The operator '-' is
>> > unary and cannot be followed by a non-numeric atomic object (a vector).
>> > Try for instance
>> >
>> > x <- list(a=1:10, b=rnorm(5))
>> > -x
>> >
>> > Rui Barradas
>> >
>> >
>> > Citando Pauline Laïlle > >:
>> >
>> >> Works like a charm, thanks! Still don't know what that error message
>> >> means though. Any idea?
>> >>
>> >>   2016-09-20 20:13 GMT+02:00 > >:
>> >>> Sorry, I've made a stupid mistake.
>> >>> It's obviously the other way around.
>> >>>
>> >>> ix <- which(rownames(data) %in% c("601", "604"))
>> >>> clean <- data[-ix, ]
>> >>>
>> >>> Rui Barradas
>> >>>
>> >>> Citando ruipbarra...@sapo.pt
>> :
>>  Hello,
>> 
>>  Try something like the following.
>> 
>>  ix <- which(c("601", "604") %in% rownames(data))
>>  clean <- data[-ix, ]
>> 
>>  Hope this helps,
>> 
>>  Rui Barradas
>> 
>>  Citando Pauline Laïlle > >:
>> 
>> > Dear all,
>> >
>> > I built a dataframe with read.csv2(). Initially, row names are
>> integers
>> > (order of answers to a survey). They are listed in the csv's first
>> column.
>> > The import works well and my dataframe looks like I wanted it to
>> look.
>> >
>> > Row names go as follows :
>> > [1] "6"   "29"  "31"  "32"  "52"  "55"  "63"  "71"  "72"  "80"
>> "88"  "89"
>> > "91"  "93"  "105" "110" "111" "117" "119" "120"
>> > [21] "122" "127" "128" "133" "137" "140" "163" "165" "167" "169"
>> "177"
>> > "178" "179" "184" "186" "192" "193" "200" "201" "228"
>> > etc.
>> >
>> > I would like to drop rows "601" & "604" to clean the dataframe.
>> >
>> > While data["601",] shows me the first row i'd like to drop,
>> data[-"601",]
>> > returns the following :
>> > Error in -"601" : invalid argument to unary operator
>> >
>> > idem with data[c("601","604"),] and data[-c("601","604"),]
>> >
>> > It is the first time that I run into this specific error. After
>> reading a
>> > bit about it I still don't understand what it means and how to fix
>> it.
>> >
>> > Thanks for reading!
>> > Best,
>> > Pauline.
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@r-project.org
>>  mailing list --
>> To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> 
>>  __
>>  R-help@r-project.org
>>  mailing list --
>> To UNSUBSCRIBE and more, see
>>  https://stat.ethz.ch/mailman/listinfo/r-help
>>  PLEASE do read the posting guide
>>  http://www.R-project.org/posting-guide.html
>>  and provide commented, minimal, self-contained, reproducible code.
>> >>>
>> >>>
>> >
>> >
>> >
>> > [[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/posti
>> ng-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://ww

Re: [R] "invalid argument to unary operator" while selecting rows by name

2016-09-22 Thread Pauline Laïlle
Works like a charm, thanks!
Still don't know what that error message means though. Any idea?

2016-09-20 20:13 GMT+02:00 :

> Sorry, I've made a stupid mistake.
> It's obviously the other way around.
>
> ix <- which(rownames(data) %in% c("601", "604"))
> clean <- data[-ix, ]
>
>
> Rui Barradas
>
>
> Citando ruipbarra...@sapo.pt:
>
>
> Hello,
>>
>> Try something like the following.
>>
>> ix <- which(c("601", "604") %in% rownames(data))
>> clean <- data[-ix, ]
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
>>
>> Citando Pauline Laïlle :
>>
>> Dear all,
>>>
>>> I built a dataframe with read.csv2(). Initially, row names are integers
>>> (order of answers to a survey). They are listed in the csv's first
>>> column.
>>> The import works well and my dataframe looks like I wanted it to look.
>>>
>>> Row names go as follows :
>>> [1] "6"   "29"  "31"  "32"  "52"  "55"  "63"  "71"  "72"  "80"  "88"
>>> "89"
>>> "91"  "93"  "105" "110" "111" "117" "119" "120"
>>> [21] "122" "127" "128" "133" "137" "140" "163" "165" "167" "169" "177"
>>> "178" "179" "184" "186" "192" "193" "200" "201" "228"
>>> etc.
>>>
>>> I would like to drop rows "601" & "604" to clean the dataframe.
>>>
>>> While data["601",] shows me the first row i'd like to drop, data[-"601",]
>>> returns the following :
>>> Error in -"601" : invalid argument to unary operator
>>>
>>> idem with data[c("601","604"),] and data[-c("601","604"),]
>>>
>>> It is the first time that I run into this specific error. After reading a
>>> bit about it I still don't understand what it means and how to fix it.
>>>
>>> Thanks for reading!
>>> Best,
>>> Pauline.
>>>
>>> [[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/posti
>>> ng-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/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
>

[[alternative HTML version deleted]]

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

[R] `head` doesn't show all columns for an empty data.frame

2016-09-22 Thread Colin Phillips
I'm sure I'm doing something wrong, but I'm seeing strange behaviour using the 
`head` and `tail` functions on an empty data.frame.

To reproduce:
# create an empty data frame.  I actually read an empty table from Excel using 
`readWorkbook` from package `openxlsx`
test <- structure(list(Code = NULL, Name = NULL, Address = NULL, Sun.Hrs = 
NULL, 
Mon.Hrs = NULL), .Names = c("Code", "Name", "Address", "Sun.Hrs", 
"Mon.Hrs"), class = "data.frame", row.names = integer(0))

# show the data frame
test

# output in console:
# [1] CodeNameAddress Sun.Hrs Mon.Hrs
# <0 rows> (or 0-length row.names)

# note that the data frame has 0 rows and 5 columns
# show the structure
str(test)

# output in console:
#'data.frame':  0 obs. of  5 variables:
# $ Code   : NULL
# $ Name   : NULL
# $ Address: NULL
# $ Sun.Hrs: NULL
# $ Mon.Hrs: NULL

#again, the structure shows 5 columns.  However...
head(test); tail(test)

# output in console:
#[1] NameSun.Hrs
#<0 rows> (or 0-length row.names)
#[1] NameSun.Hrs
#<0 rows> (or 0-length row.names)

# now we have only two columns
 
Weird, right?

So, here's my session info:
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

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

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

other attached packages:
 [1] tidyr_0.6.0   lpSolve_5.6.13flexclust_1.3-4   modeltools_0.2-21 
lattice_0.20-34   gtools_3.5.0  reshape2_1.4.1ash_1.0-15
RODBC_1.3-13 
[10] ggmap_2.6.1   ggplot2_2.1.0 dplyr_0.5.0   assertthat_0.1
openxlsx_3.0.0   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7   plyr_1.8.4tools_3.3.1   digest_0.6.10 
tibble_1.2gtable_0.2.0  png_0.1-7 DBI_0.5-1 
mapproj_1.2-4
[10] parallel_3.3.1proto_0.3-10  stringr_1.1.0 RgoogleMaps_1.4.1 
maps_3.1.1R6_2.1.3  jpeg_0.1-8sp_1.2-3  
magrittr_1.5 
[19] scales_0.4.0  geosphere_1.5-5   colorspace_1.2-6  labeling_0.3  
stringi_1.1.1 lazyeval_0.2.0munsell_0.4.3 rjson_0.2.15 

This is not an urgent issue, I just think it's curious, so it would be nice to 
understand why it happens.

Thanks,

Colin

__
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] "invalid argument to unary operator" while selecting rows by name

2016-09-22 Thread Pauline Laïlle
Hi, thanks for the answer.
In this case, the row named "601" is not the 601st row of the table, but
the 117th. data[601,] actually refers to a non existing row.
I was wondering why data[-"601,] generates an error message whereas
data["601",] does not?

2016-09-20 19:08 GMT+02:00 Bert Gunter :

> Hint: "601"  is not 601.
>
> Have you gone through any R tutorials?
>
> Cheers,
> Bert
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Tue, Sep 20, 2016 at 5:42 AM, Pauline Laïlle
>  wrote:
> > Dear all,
> >
> > I built a dataframe with read.csv2(). Initially, row names are integers
> > (order of answers to a survey). They are listed in the csv's first
> column.
> > The import works well and my dataframe looks like I wanted it to look.
> >
> > Row names go as follows :
> >  [1] "6"   "29"  "31"  "32"  "52"  "55"  "63"  "71"  "72"  "80"  "88"
> "89"
> >  "91"  "93"  "105" "110" "111" "117" "119" "120"
> >  [21] "122" "127" "128" "133" "137" "140" "163" "165" "167" "169" "177"
> > "178" "179" "184" "186" "192" "193" "200" "201" "228"
> > etc.
> >
> > I would like to drop rows "601" & "604" to clean the dataframe.
> >
> > While data["601",] shows me the first row i'd like to drop, data[-"601",]
> > returns the following :
> > Error in -"601" : invalid argument to unary operator
> >
> > idem with data[c("601","604"),] and data[-c("601","604"),]
> >
> > It is the first time that I run into this specific error. After reading a
> > bit about it I still don't understand what it means and how to fix it.
> >
> > Thanks for reading!
> > Best,
> > Pauline.
> >
> > [[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.
>

[[alternative HTML version deleted]]

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

Re: [R] if/else help

2016-09-22 Thread Crombie, Burnette N
Thanks very much for your detailed reply to my post.  Very helpful/useful 
tool(s) you’ve provide me.  Best wishes, B.

From: William Dunlap [mailto:wdun...@tibco.com]
Sent: Wednesday, September 21, 2016 10:48 AM
To: Crombie, Burnette N 
Cc: r-help@r-project.org
Subject: Re: [R] if/else help

If you write your code as functions you can avoid the nasty 
'if(exists("x"))x<-...' business this by writing default values for
arguments to your function.   They will be computed only when
they are used.  E.g.,
analyzeData <- function(a=0, b=0, c=0, d="x", r4 = data.frame(a, b, c, d)) {
summary(r4)
}
> analyzeData(c=101:102)
   a   b   c d
 Min.   :0   Min.   :0   Min.   :101.0   x:2
 1st Qu.:0   1st Qu.:0   1st Qu.:101.2
 Median :0   Median :0   Median :101.5
 Mean   :0   Mean   :0   Mean   :101.5
 3rd Qu.:0   3rd Qu.:0   3rd Qu.:101.8
 Max.   :0   Max.   :0   Max.   :102.0
> analyzeData(r4=data.frame(a=10:11,b=20:21,c=30:31,d=c("x","y")))
   a   b   c d
 Min.   :10.00   Min.   :20.00   Min.   :30.00   x:1
 1st Qu.:10.25   1st Qu.:20.25   1st Qu.:30.25   y:1
 Median :10.50   Median :20.50   Median :30.50
 Mean   :10.50   Mean   :20.50   Mean   :30.50
 3rd Qu.:10.75   3rd Qu.:20.75   3rd Qu.:30.75
 Max.   :11.00   Max.   :21.00   Max.   :31.00


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Sep 20, 2016 at 12:31 PM, Crombie, Burnette N 
mailto:bcrom...@utk.edu>> wrote:
If a data.frame (r4) does not exist in my R environment, I would like to create 
it before I move on to the next step in my script. How do I make that happen?  
Here is what I want to do from a code perspective:

if (exists(r4))
{
is.data.frame(get(r4))
}
else
{
a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
}

Thanks for your help,
B

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


[[alternative HTML version deleted]]

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

Re: [R] if/else help

2016-09-22 Thread Crombie, Burnette N
Thank you for your time, Don.  Exactly what I was looking for - a one-liner.  
Feedback from others on this post has been good to expand my knowledge, though. 
 I'm too old for homework but have just started using R if/else, loops, and 
functions and trying to get the hang of them.  Best wishes - B

-Original Message-
From: MacQueen, Don [mailto:macque...@llnl.gov] 
Sent: Wednesday, September 21, 2016 11:26 AM
To: Crombie, Burnette N ; r-help@r-project.org
Subject: Re: [R] if/else help

Hopefully this is not a homework question.

The other responses are fine, but I would suggest the simplest way to do 
exactly what you ask is


if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x')


The exists() function requires a character string for its first argument, i.e., 
the name of the object, not the object itself (check the help page for exists).

Using "get" to get it doesn't make sense if it already exists.

-Don

--
Don MacQueen

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





On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N"
 wrote:

>If a data.frame (r4) does not exist in my R environment, I would like 
>to create it before I move on to the next step in my script. How do I 
>make that happen?  Here is what I want to do from a code perspective:
>
>if (exists(r4))
>{
>is.data.frame(get(r4))
>}
>else
>{
>a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) }
>
>Thanks for your help,
>B
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] mgcv: bam(), error in models with random intercepts and random slopes

2016-09-22 Thread Simon Wood

Hi Fotis,

Thanks for the report, and sending me the data and code (off list). The 
problem is triggered by 'ctrial' being a (one column) matrix. An 
immediate fix is


data_a$ctrial <- as.numeric(data_a$ctrial)

- mgcv 1.8-16 will catch the problem automatically internally.

best,
Simon

On 20/09/16 17:22, Fotis Fotiadis wrote:

Hi all

I am using the bam function of the mgcv package to model behavioral data of
a learning experiment. To model individual variation in learning rate, I am
testing models with (a) by-participant random intercepts of trial, (b)
by-participant random slopes and random intercepts of trial, and (c)
by-participant random smooth terms.

While all (a) and (c) models converge, I am getting an error for every
possible variation of a model with random intercepts and random slopes. For
example:

m1.rs<-bam(acc~ 1 + igc + s(ctrial) + s(sbj, bs="re") + s(ctrial, sbj,
bs="re") , data=data_a, family=binomial)
Error in G$smooth[[i]]$first.para:G$smooth[[i]]$last.para :
   argument of length 0

Any idea on what that error might be?

Thank you in advance for your time.
Fotis

P.S.: R version: 3.3.1, mgcv version: 1.8.15




--
Simon Wood, School of Mathematics, University of Bristol BS8 1TW UK
+44 (0)117 33 18273 http://www.maths.bris.ac.uk/~sw15190

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