Re: [Rd] question regarding lang2 command in C

2014-04-02 Thread Romain François
Hi, 

This is easy if the gender and age are already vectors of some sort of the same 
size. 

SEXP df = PROTECT(allocVector(VECSXP, 2)); 
SET_VECTOR_ELT(df,0,gender) ;
SET_VECTOR_ELT(df,1,age) ;
SEXP names = PROTECT(allocVector(STRSXP,2));
SET_STRING_ELT(names,0,mkChar("age"))
SET_STRING_ELT(names,0,mkChar("gender"))
setAttrib(df, R_NamesSymbol, names );
setAttrib(df, R_ClassSymbol, mkString("data.frame"));
SEXP rn = PROTECT(allocVector(INTSXP,2)); 
INTEGER(rn)[0] = NA_INTEGER ;
INTEGER(rn)[1] = -length(gender);
setAttrib(df, R_RowNamesSymbol, rn) ;
UNPROTECT(3) ;
return df ;

If you really want to call back to R and make a call as you did before, you can 
do something like this: 

  SEXP call = PROTECT(lang4(install("data.frame"), age, gender, 
ScalarLogical(FALSE))) ;
  SET_TAG(CDR(call), install("age")) ;
  SET_TAG(CDDR(call), install("gender")) ;
  SET_TAG(CDR(CDDR(call)), install("stringsAsFactors")) ;
  SEXP df = PROTECT(eval(call, R_GlobalEnv)) ;
  UNPROTECT(2) ;
  return df ;

Or you can use Rcpp: 

  DataFrame df = DataFrame::create(
_["age"] = age, _["gender"] = gender, _["stringsAsFactors"] = FALSE
  ) ;

Romain

Le 3 avr. 2014 à 07:40, Sandip Nandi  a écrit :

> Hi ,
> 
> I am asking too many questions , sorry for that .  I am creating a data
> frame in C itself , reading a table .
> 
> The data frame calling code looks like this
> ==
> 
> *PROTECT(dfm=lang2(install("data.frame"),df));*
> *SEXP res = PROTECT(eval(dfm,R_GlobalEnv));*
> 
> UNPROTECT(2);
> return res;
> ==
> 
> It works fine , now the problem is I want to do the below one  from C
> itself  ( adding the *stringsAsFactors = FALSE* parameter)
> 
> df <- data.frame(gender, age, *stringsAsFactors = FALSE*);
> 
> How can I do it from C , adding extra parameters. Anyone has pointer or any
> example . It will be great help. I find the arguments will always be in
> pair , i don't find any example.
> 
> 
> I try to see the source code ,not able to make it
> 
> Thanks

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] question regarding lang2 command in C

2014-04-02 Thread Sandip Nandi
Hi ,

I am asking too many questions , sorry for that .  I am creating a data
frame in C itself , reading a table .

The data frame calling code looks like this
==

*PROTECT(dfm=lang2(install("data.frame"),df));*
*SEXP res = PROTECT(eval(dfm,R_GlobalEnv));*

UNPROTECT(2);
return res;
==

It works fine , now the problem is I want to do the below one  from C
itself  ( adding the *stringsAsFactors = FALSE* parameter)

df <- data.frame(gender, age, *stringsAsFactors = FALSE*);

How can I do it from C , adding extra parameters. Anyone has pointer or any
example . It will be great help. I find the arguments will always be in
pair , i don't find any example.


I try to see the source code ,not able to make it

Thanks

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] inconsistent error messages on Mac OS X

2014-04-02 Thread Dan Tenenbaum


- Original Message -
> From: "Adam Welc" 
> To: r-devel@r-project.org
> Sent: Wednesday, April 2, 2014 3:11:28 PM
> Subject: [Rd] inconsistent error messages on Mac OS X
> 
> Hi All,
> 
> I am one of the contributors to the FastR project (
> https://bitbucket.org/allr ) and I have
> encountered an interesting issue when trying to implement vector
> accesses
> within FastR. I am trying to understand what kind of error message
> should
> be generated for the following expression:
> 
> x<-1:4; x[[1]]<-NULL; x
> 
> In order to determine the error message, I ran the shell of standard
> GNU R
> (installed via MacPorts - R version 2.15.3) on Mac OS X 10.8.5 as
> follows,
> with R metadata (that is .RData or .Rhistory files) removed from the
> current directory 

Instead you should probably start R as follows:

R --vanilla

?Startup explains why removing .RData etc from the current directory is not 
enough, in the absence of --vanilla.

>(I have edited portions of the R header printed
> when the
> shell starts for the sake for readability):
> 
> Adams-MacBook-Air:work adam$ R
> 
> R version 2.15.3 (2013-03-01) -- "Security Blanket"
> Copyright (C) 2013 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
> Platform: x86_64-apple-darwin12.3.0/x86_64 (64-bit)
> ...
> ...
> Type 'q()' to quit R.
> 
> > x<-1:4; x[[1]]<-NULL; x
> Error in x[[1]] <- NULL :
>   incompatible types (from NULL to integer) in [[ assignment
> > q()
> Save workspace image? [y/n/c]: n
> 
> 
> Adams-MacBook-Air:work adam$ R
> 
> R version 2.15.3 (2013-03-01) -- "Security Blanket"
> Copyright (C) 2013 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
> Platform: x86_64-apple-darwin12.3.0/x86_64 (64-bit)
> ...
> ...
> Type 'q()' to quit R.
> 
> > x<-1:4; x[[1]]<-NULL; x
> Error in x[[1]] <- NULL :
>   more elements supplied than there are to replace
> >
> 
> 
> As you can see, the error message for the same expression is
> different on
> two subsequent executions of the GNU R shell (with no workspace image
> saving - but it does not matter, as I observe the same behavior if
> the
> workspace is saved).
> 
> I tried the same thing on Linux, but there the behavior seems
> consistent
> (the second message is displayed in each execution).
> 
> This issue is not specific to this single expression - it happens in
> other
> (though not all) cases when the NULL value is assigned to an element
> of a
> vector.
> 
> I was wondering if someone has observed the same behavior and perhaps
> knows
> what may be causing it...
> 

I can't reproduce this on 
R Under development (unstable) (2013-10-12 r64048)

with or without --vanilla, it consistently gives the second error message.

Dan


> Thank you
> 
> Adam
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] inconsistent error messages on Mac OS X

2014-04-02 Thread Adam Welc
Hi All,

I am one of the contributors to the FastR project (
https://bitbucket.org/allr ) and I have
encountered an interesting issue when trying to implement vector accesses
within FastR. I am trying to understand what kind of error message should
be generated for the following expression:

x<-1:4; x[[1]]<-NULL; x

In order to determine the error message, I ran the shell of standard GNU R
(installed via MacPorts - R version 2.15.3) on Mac OS X 10.8.5 as follows,
with R metadata (that is .RData or .Rhistory files) removed from the
current directory (I have edited portions of the R header printed when the
shell starts for the sake for readability):

Adams-MacBook-Air:work adam$ R

R version 2.15.3 (2013-03-01) -- "Security Blanket"
Copyright (C) 2013 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin12.3.0/x86_64 (64-bit)
...
...
Type 'q()' to quit R.

> x<-1:4; x[[1]]<-NULL; x
Error in x[[1]] <- NULL :
  incompatible types (from NULL to integer) in [[ assignment
> q()
Save workspace image? [y/n/c]: n


Adams-MacBook-Air:work adam$ R

R version 2.15.3 (2013-03-01) -- "Security Blanket"
Copyright (C) 2013 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin12.3.0/x86_64 (64-bit)
...
...
Type 'q()' to quit R.

> x<-1:4; x[[1]]<-NULL; x
Error in x[[1]] <- NULL :
  more elements supplied than there are to replace
>


As you can see, the error message for the same expression is different on
two subsequent executions of the GNU R shell (with no workspace image
saving - but it does not matter, as I observe the same behavior if the
workspace is saved).

I tried the same thing on Linux, but there the behavior seems consistent
(the second message is displayed in each execution).

This issue is not specific to this single expression - it happens in other
(though not all) cases when the NULL value is assigned to an element of a
vector.

I was wondering if someone has observed the same behavior and perhaps knows
what may be causing it...

Thank you

Adam

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Typeof for character vector in dataframe returns integer

2014-04-02 Thread Joris Meys
I know a few good books where this is explained.

This free one for example: http://cran.r-project.org/doc/manuals/R-intro.pdf
chapters 4 and 6.

Or http://rfordummies.com/ if you insist on paying for it.

PS: Jeff, R is not C. different place in the alphabet and all...


On Wed, Apr 2, 2014 at 2:55 AM, Sandip Nandi  wrote:

> Hi Jeff ,
>
> I dont think so ,
>
> > df[[0]]
> Error in .subset2(x, i, exact = exact) :
>   attempt to select less than one element
> >
> >
> > df[[1]]
> [1] F M M F F M F F
> Levels: F M
> > df[[2]]
> [1] 23 25 27 29 31 33 35 37
>
> Thanks
>
>
> On Tue, Apr 1, 2014 at 5:51 PM, Jeff Johnson 
> wrote:
>
> > Perhaps because indexes start at 0?
> >
> > Sent from my iPhone
> >
> > > On Apr 1, 2014, at 5:46 PM, Sandip Nandi 
> wrote:
> > >
> > > Hi ,
> > >
> > > I want to know is this behavior expected and why is that ? Need some
> help
> > >
> > > gender <- c("F", "M", "M", "F", "F", "M", "F", "F")
> > >> age<- c(23, 25, 27, 29, 31, 33, 35, 37)
> > >> df<- data.frame(gender,age)
> > >> typeof(df[[1]])
> > > [1] "integer"   >  Why is this integer . *Should not it be
> > > character ?*
> > >> typeof(df[[2]])
> > > [1] "double"
> > >
> > >> typeof(gender)
> > > [1] "character"
> > >> typeof(age)
> > > [1] "double"
> > >
> > > In my code i am trying to do some thing based on typeof and the type
> for
> > > character column is strange.
> > >
> > > Thanks,
> > > Sandip
> > >
> > >[[alternative HTML version deleted]]
> > >
> > > __
> > > R-devel@r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Mathematical Modelling, Statistics and Bio-Informatics

tel : +32 9 264 59 87
joris.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Typeof for character vector in dataframe returns integer

2014-04-02 Thread Jeff Johnson
Perhaps because indexes start at 0?

Sent from my iPhone

> On Apr 1, 2014, at 5:46 PM, Sandip Nandi  wrote:
> 
> Hi ,
> 
> I want to know is this behavior expected and why is that ? Need some help
> 
> gender <- c("F", "M", "M", "F", "F", "M", "F", "F")
>> age<- c(23, 25, 27, 29, 31, 33, 35, 37)
>> df<- data.frame(gender,age)
>> typeof(df[[1]])
> [1] "integer"   >  Why is this integer . *Should not it be
> character ?*
>> typeof(df[[2]])
> [1] "double"
> 
>> typeof(gender)
> [1] "character"
>> typeof(age)
> [1] "double"
> 
> In my code i am trying to do some thing based on typeof and the type for
> character column is strange.
> 
> Thanks,
> Sandip
> 
>[[alternative HTML version deleted]]
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Typeof for character vector in dataframe returns integer

2014-04-02 Thread Martin Maechler
PLEASE! 
All this does *not* belong to the R-devel mailing list.

It is entirely apt for R-help (or "similar", including stackoverflow).

Please do *not* misuse R-devel for basic R questions.

Martin Maechler,
ETH Zurich

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel