[R] stfrtime function not returning proper results through sqldf package in R

2016-09-15 Thread Manohar Reddy
Hi ,



  I have data something looks like below (or PFA), but when I’m extracting
month  using *strftime*  function through *sqldf* library ,it’s returning
below results but it’s not returning exact results ,it supposed to return
 05,05,05,06,06,06.Can anyone please guide me how to do that with *strftime*
function.



Thanks in advance.



Quiries :


library(scales)

# load data:
log <- data.frame(Date =
c("2013/05/25","2013/05/28","2013/05/31","2013/06/01","2013/06/02","2013/06/05","2013/06/07"),
  Quantity = c(9,1,15,4,5,17,18))


# convert date variable from factor to date format:
log$Date <- as.Date(log$Date,
  "%Y/%m/%d") # tabulate all the options here
str(log)







Manu.
__
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] Visualizing and clustering one half of a symmetric matrix

2016-09-15 Thread Peter Langfelder
Do not set the upper (or lower) triangle to NA. Simply supply the full
matrix to pheatmap. I am not an expert on pheatmap but looking at the
manual you should supply clustering_distance_rows = "none",
clustering_distance_cols = "none" or something like that to make
pheatmap interpret the matrix as a distance matrix. Read carefully
through the help on pheatmap to make sure the function plots what you
want it to plot.

HTH,

Peter

On Thu, Sep 15, 2016 at 7:38 PM, Khan, Saad M. (MU-Student)
 wrote:
> Hi all,
>
> I have a distance matrix (symmetric) which looks somewhat like this (only a 
> small portion shown)
>
> ENSG0101413 ENSG0176884 ENSG0185532 
> ENSG0106829
> ENSG0101413   1.000   1.000   1.000   
> 1.000
> ENSG0176884   0.328   0.258   0.260   
> 0.390
> ENSG0185532   1.000   1.000   1.000   
> 1.000
> ENSG0106829   0.684   0.443   0.531   
> 0.701
>
> These distances are custom measures that I need to cluster. Since it's a 
> symmetric matrix I only need to consider one half triangle of the matrix. So 
> I do something like this :-
>
> newmat <- ensembl_copygosimmat
> newmat[upper.tri(ensembl_copygosimmat)] <- NA
>
> Then I wanted to visualize how the lower triangle looked using pheatmap which 
> does hierarchical clustering itself.
>
> library(pheatmap)
> pheatmap(newmat)
>
> But since there are NA values in the matrix (in the upper half) it always 
> throws an error. I was wondering what would be the ideal way to visualize as 
> well as cluster such a matrix.
>
> Regards
> Saad
>
> [[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.


[R] Visualizing and clustering one half of a symmetric matrix

2016-09-15 Thread Khan, Saad M. (MU-Student)
Hi all,

I have a distance matrix (symmetric) which looks somewhat like this (only a 
small portion shown)

ENSG0101413 ENSG0176884 ENSG0185532 ENSG0106829
ENSG0101413   1.000   1.000   1.000   1.000
ENSG0176884   0.328   0.258   0.260   0.390
ENSG0185532   1.000   1.000   1.000   1.000
ENSG0106829   0.684   0.443   0.531   0.701

These distances are custom measures that I need to cluster. Since it's a 
symmetric matrix I only need to consider one half triangle of the matrix. So I 
do something like this :-

newmat <- ensembl_copygosimmat
newmat[upper.tri(ensembl_copygosimmat)] <- NA

Then I wanted to visualize how the lower triangle looked using pheatmap which 
does hierarchical clustering itself.

library(pheatmap)
pheatmap(newmat)

But since there are NA values in the matrix (in the upper half) it always 
throws an error. I was wondering what would be the ideal way to visualize as 
well as cluster such a matrix.

Regards
Saad

[[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] NaN Log-lik value in EM algorithm (fitting Gamma mixture model)

2016-09-15 Thread William Dunlap via R-help
Does the data contain non-positive values?

> out <- mixtools::gammamixEM(as.numeric(0:100), lambda = c(1, 1, 1)/3,
verb = TRUE)
iteration = 1  log-lik diff = NaN  log-lik = NaN
Error in while (diff > epsilon && iter < maxit) { :
  missing value where TRUE/FALSE needed


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Sep 15, 2016 at 3:04 PM, Aanchal Sharma 
wrote:

> I am using a function gammamixEM where it does it by default. I do not have
> the option to change it.
> Conceptually, what can make the algorithm not able to calculate likelihood
> value at all (and hence log-lik=Nan)? Is there sth wrong with the data?
> Under what conditions does it happen?
>
> On Wed, Sep 14, 2016 at 8:04 PM, Duncan Murdoch 
> wrote:
>
> > On 14/09/2016 4:46 PM, Aanchal Sharma wrote:
> >
> >> Hi,
> >>
> >> I am trying to fit Gamma mixture model to my data (residual values
> >> obtained
> >> after fitting Generalized linear Model) using gammamixEM. It is part of
> >> the
> >> script which does it for multiple datasets in loop. The code is running
> >> fine for some datasets but it terminates for some giving following
> error:
> >>
> >> " iteration = 1  log-lik diff = NaN  log-lik = NaN
> >> Error in while (diff > epsilon && iter < maxit) { :
> >>   missing value where TRUE/FALSE needed"
> >>
> >> Seems like EM is not able to calculate log-lik value (NaN) at the first
> >> iteration itself. any idea why that can happen?
> >> It works fine for the other genes in the loop. Tried looking for
> >> difference
> >> in the inputs, but could not come up with anything striking.
> >>
> >>
> > THere are lots of ways to get NaN in numerical calculations.   A common
> > one if you are using log() to calculate log likelihoods is that rounding
> > error gives you a negative likelihood, and then log(lik) comes out to
> NaN.
> >
> > You just need to look really closely at each step of your calculations.
> > Avoid using log(); use the functions that build it in (e.g. instead of
> > log(dnorm(x)), use dnorm(x, log = TRUE)).
> >
> > Duncan Murdoch
> >
> >
>
>
> --
> Anchal Sharma, PhD
> Postdoctoral Fellow
> 195, Little Albany street,
> Cancer Institute of New Jersey
> Rutgers University
> NJ-08901
>
> [[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] NaN Log-lik value in EM algorithm (fitting Gamma mixture model)

2016-09-15 Thread Aanchal Sharma
I am using a function gammamixEM where it does it by default. I do not have
the option to change it.
Conceptually, what can make the algorithm not able to calculate likelihood
value at all (and hence log-lik=Nan)? Is there sth wrong with the data?
Under what conditions does it happen?

On Wed, Sep 14, 2016 at 8:04 PM, Duncan Murdoch 
wrote:

> On 14/09/2016 4:46 PM, Aanchal Sharma wrote:
>
>> Hi,
>>
>> I am trying to fit Gamma mixture model to my data (residual values
>> obtained
>> after fitting Generalized linear Model) using gammamixEM. It is part of
>> the
>> script which does it for multiple datasets in loop. The code is running
>> fine for some datasets but it terminates for some giving following error:
>>
>> " iteration = 1  log-lik diff = NaN  log-lik = NaN
>> Error in while (diff > epsilon && iter < maxit) { :
>>   missing value where TRUE/FALSE needed"
>>
>> Seems like EM is not able to calculate log-lik value (NaN) at the first
>> iteration itself. any idea why that can happen?
>> It works fine for the other genes in the loop. Tried looking for
>> difference
>> in the inputs, but could not come up with anything striking.
>>
>>
> THere are lots of ways to get NaN in numerical calculations.   A common
> one if you are using log() to calculate log likelihoods is that rounding
> error gives you a negative likelihood, and then log(lik) comes out to NaN.
>
> You just need to look really closely at each step of your calculations.
> Avoid using log(); use the functions that build it in (e.g. instead of
> log(dnorm(x)), use dnorm(x, log = TRUE)).
>
> Duncan Murdoch
>
>


-- 
Anchal Sharma, PhD
Postdoctoral Fellow
195, Little Albany street,
Cancer Institute of New Jersey
Rutgers University
NJ-08901

[[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] box type in Hmisc xYplot

2016-09-15 Thread David Winsemius

> On Sep 15, 2016, at 1:32 PM, David Winsemius  wrote:
> 
> 
>> On Sep 15, 2016, at 12:59 PM, Cade, Brian  wrote:
>> 
>> Does anyone know how to change the box type in Hmisc package function
>> xYplot.  I want only the left and bottom axes drawn, similar to what I
>> would accomplish with bty="l" argument in plot() function.  bty= argument
>> did not do anything for me in xYplot().
> 
> Frank switched over to lattice so base-graphics arguments are not necessarily 
> honored (unless they're shared with lattice::xyplot). So you need to think 
> how you would specify the options in lattice::xyplot and then try to pass 
> those arguments, since there is a "dots" mechanism. I tried finding a 
> solution in Sarkar's Lattice book but didn't come up with anything. The "box" 
> settings I found applied to bwplot "boxes".
> 
> (Not attempting example since no data offered. You might consider searching 
> the archives.)
> 

Searching the archives "scores" again:

https://stat.ethz.ch/pipermail/r-help/2007-September/140098.html

-- 

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] dplyr or plyr or both?

2016-09-15 Thread Jeff Newmiller
You are ignoring the warning issued when you load plyr after dplyr and then 
complaining. Simply reversing the sequence of library statements is sufficient 
to fix your example.

I agree that it is not ideal and that using just one at a time is easier, but 
you can use both, and for now a lot of packages use plyr and are not going to 
be rewritten for dplyr because dplyr doesn't do everything plyr does.

The more subtle problem with using both is that you may need to be explicit 
about which package's function to use with dplyr:: or plyr:: notation, so WHERE 
POSSIBLE I also recommend using dplyr.
-- 
Sent from my phone. Please excuse my brevity.

On September 15, 2016 1:28:51 PM PDT, Frans Marcelissen 
 wrote:
>I never realised that nonsense results don’n bother experienced
>users….. Probably I am not experienced wiith my 6years of R
>professional work.
>I think your advise is incomplete: a %>% dplyr::group_by(groep) %>%
>dplyr::summarise(m=mean(v),n=dplyr::n())
>Gives the same problems and makes the line ugly. Or how would you do
>this?
>I stick with my advise: use dplyr. if you do not need plyr: stay away
>from it. If you need it: do not attach it, but use the plyr:: notation.
>I agree that plyr is something else as dplyr, and unfortunately plyr
>will be used for some time.
>
>Verzonden vanuit Mail voor Windows 10
>
>Van: Jeff Newmiller

__
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] box type in Hmisc xYplot

2016-09-15 Thread Cade, Brian
No even in xyplot() the scales argument is not eliminating plotting of the
top and right graph axes.  It is not the scale of the axes I want
eliminated but the actual lines.  But thank you for trying.

Brian

Brian S. Cade, PhD

U. S. Geological Survey
Fort Collins Science Center
2150 Centre Ave., Bldg. C
Fort Collins, CO  80526-8818

email:  ca...@usgs.gov 
tel:  970 226-9326


On Thu, Sep 15, 2016 at 2:44 PM, Bert Gunter  wrote:

> I do not know about xYplot, but the "scales" argument controls axes in
> xyplot. For example:
>
> scales = list(alternating = 1)
>
> would draw axes on left/bottom ...  see ?xyplot for details and subtleties.
>
> Again, might not work for xYplot.
>
> 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 Thu, Sep 15, 2016 at 1:32 PM, David Winsemius 
> wrote:
> >
> >> On Sep 15, 2016, at 12:59 PM, Cade, Brian  wrote:
> >>
> >> Does anyone know how to change the box type in Hmisc package function
> >> xYplot.  I want only the left and bottom axes drawn, similar to what I
> >> would accomplish with bty="l" argument in plot() function.  bty=
> argument
> >> did not do anything for me in xYplot().
> >
> > Frank switched over to lattice so base-graphics arguments are not
> necessarily honored (unless they're shared with lattice::xyplot). So you
> need to think how you would specify the options in lattice::xyplot and then
> try to pass those arguments, since there is a "dots" mechanism. I tried
> finding a solution in Sarkar's Lattice book but didn't come up with
> anything. The "box" settings I found applied to bwplot "boxes".
> >
> > (Not attempting example since no data offered. You might consider
> searching the archives.)
> >
> > --
> >
> > 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.
>
>

[[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] box type in Hmisc xYplot

2016-09-15 Thread Bert Gunter
I do not know about xYplot, but the "scales" argument controls axes in
xyplot. For example:

scales = list(alternating = 1)

would draw axes on left/bottom ...  see ?xyplot for details and subtleties.

Again, might not work for xYplot.

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 Thu, Sep 15, 2016 at 1:32 PM, David Winsemius  wrote:
>
>> On Sep 15, 2016, at 12:59 PM, Cade, Brian  wrote:
>>
>> Does anyone know how to change the box type in Hmisc package function
>> xYplot.  I want only the left and bottom axes drawn, similar to what I
>> would accomplish with bty="l" argument in plot() function.  bty= argument
>> did not do anything for me in xYplot().
>
> Frank switched over to lattice so base-graphics arguments are not necessarily 
> honored (unless they're shared with lattice::xyplot). So you need to think 
> how you would specify the options in lattice::xyplot and then try to pass 
> those arguments, since there is a "dots" mechanism. I tried finding a 
> solution in Sarkar's Lattice book but didn't come up with anything. The "box" 
> settings I found applied to bwplot "boxes".
>
> (Not attempting example since no data offered. You might consider searching 
> the archives.)
>
> --
>
> 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.

__
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] box type in Hmisc xYplot

2016-09-15 Thread David Winsemius

> On Sep 15, 2016, at 12:59 PM, Cade, Brian  wrote:
> 
> Does anyone know how to change the box type in Hmisc package function
> xYplot.  I want only the left and bottom axes drawn, similar to what I
> would accomplish with bty="l" argument in plot() function.  bty= argument
> did not do anything for me in xYplot().

Frank switched over to lattice so base-graphics arguments are not necessarily 
honored (unless they're shared with lattice::xyplot). So you need to think how 
you would specify the options in lattice::xyplot and then try to pass those 
arguments, since there is a "dots" mechanism. I tried finding a solution in 
Sarkar's Lattice book but didn't come up with anything. The "box" settings I 
found applied to bwplot "boxes".

(Not attempting example since no data offered. You might consider searching the 
archives.)

-- 

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] dplyr or plyr or both?

2016-09-15 Thread Frans Marcelissen
I never realised that nonsense results don’n bother experienced users….. 
Probably I am not experienced wiith my 6years of R professional work.
I think your advise is incomplete: a %>% dplyr::group_by(groep) %>% 
dplyr::summarise(m=mean(v),n=dplyr::n())
Gives the same problems and makes the line ugly. Or how would you do this?
I stick with my advise: use dplyr. if you do not need plyr: stay away from it. 
If you need it: do not attach it, but use the plyr:: notation.
I agree that plyr is something else as dplyr, and unfortunately plyr will be 
used for some time.

Verzonden vanuit Mail voor Windows 10

Van: Jeff Newmiller
[[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] separate commands by semicolon

2016-09-15 Thread Adrian Dușa
On Thu, Sep 15, 2016 at 10:28 PM, William Dunlap  wrote:

> The most reliable way to split such lines is with parse(text=x).
> Regular expressions don't do well with context-free grammars.
>

Oh, that's right of course.
> as.character(parse(text = x))
[1] "foo <- \"3;4\"""bar <- \"don't ; use semicolons\""

That was simple enough, thanks very much,
Adrian

-- 
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania

[[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] box type in Hmisc xYplot

2016-09-15 Thread Cade, Brian
Does anyone know how to change the box type in Hmisc package function
xYplot.  I want only the left and bottom axes drawn, similar to what I
would accomplish with bty="l" argument in plot() function.  bty= argument
did not do anything for me in xYplot().

Brian

Brian S. Cade, PhD

U. S. Geological Survey
Fort Collins Science Center
2150 Centre Ave., Bldg. C
Fort Collins, CO  80526-8818

email:  ca...@usgs.gov 
tel:  970 226-9326

[[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] dplyr or plyr or both?

2016-09-15 Thread Jeff Newmiller
The incorrect results are unfortunate and can trip up the inexperienced user, 
but this problem is straightforward to resolve if you explicitly specify which 
versions of the conflicting functions to use. The more interesting question I 
saw was whether the intent is to deprecate plyr, but so far that  does not 
appear to be the case.

While I agree that mixing them can be trouble-prone, I think in many cases both 
will continue to be used. 
-- 
Sent from my phone. Please excuse my brevity.

On September 15, 2016 10:08:05 AM PDT, Frans Marcelissen 
 wrote:
>Hello Christopher and others
>:
>What cannot be stressed enough is: do not combine both packages, it
>gives
>errors and incorrect results! I will show that below
>
>a<-data.frame(groep=1:4,v=1:40)
>library(dplyr)
>a %>% group_by(groep) %>% summarise(m=mean(v),n=n())
># groep m n
>#   
># 1 11910
># 2 22010
># 3 32110
># 4 42210
># correct
>
>library(plyr)
>a %>% group_by(groep) %>% summarise(m=mean(v),n=n())
>
>Error in n() : This function should not be called directly
># ???
>a %>% group_by(groep) %>% summarise(m=mean(v))
># m
># 1 20.5
>#incorrect!
>--
>
>So both n() and group_by from dplyr don't work after library(plyr)!
>
>My advice is: do not use plyr. Unfortunately plyr has some functions
>that
>are very important, and that are not in dplyr. For instance:
>rbind.fill()
>(for combining the rows of two dataframes with unequal columns). If you
>need this: do'nt library plyr, use plyr::rbind.fil
>
>Until now I have the impression that it is also possible to library
>dplyr
>after plyr, but it is better to remove plyr!
>
>This is a serious problem that has been reported before, but not solved
>(in
>dplyr 0.5.0 and plyr 1.8.4)
>
>Frams
>
>2016-09-15 16:09 GMT+02:00 Christopher W Ryan :
>
>> I've set myself the task of learning about these packages, and about
>> tidy data concepts.
>>
>> What is the relationship between plyr and dplyr?  Does the latter
>> replace the former (meaning I can concentrate on learning the
>latter)?
>> Or is there ever a need to use functions from both (meaning I should
>> learn both)?
>>
>> Thanks.
>>
>> --Chris Ryan
>>
>> __
>> 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] separate commands by semicolon

2016-09-15 Thread William Dunlap via R-help
The most reliable way to split such lines is with parse(text=x).
Regular expressions don't do well with context-free grammars.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Sep 15, 2016 at 12:08 PM, Adrian Dușa  wrote:

> Dear R-helpers,
>
> When parsing a text, I would like to separate commands written on the same
> line, by a semicolon.
> Something like:
>
> x <- "foo <- '3;4'; bar <- \"don't ; use semicolons\""
>
> Ideally, that would translate to these two commands in a character vector
> of length 2:
> foo <- '3;4'
> bar <- "don't ; use semicolons"
>
> It's probably a regexp magic, but I just can't find it.
>
> Any hint is highly appreciated,
> Adrian
>
> --
> Adrian Dusa
> University of Bucharest
> Romanian Social Data Archive
> Soseaua Panduri nr.90
> 050663 Bucharest sector 5
> Romania
>
> [[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] apply weight to a data frame

2016-09-15 Thread laura roncaglia
I am a beginner user of R.

I am writing the master thesis using a data frame from a national survey.
The data frame contains several variables, one of which contains the survey
weights.

I need to apply the survey weights to the data frame, in order to use the
data frame with the plm package (I need to run a fixed effect analysis).

I know that I could use packages different from plm, but I am more
interested in weighting the data frame.

Thank you in advance.

[[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] separate commands by semicolon

2016-09-15 Thread Adrian Dușa
Dear R-helpers,

When parsing a text, I would like to separate commands written on the same
line, by a semicolon.
Something like:

x <- "foo <- '3;4'; bar <- \"don't ; use semicolons\""

Ideally, that would translate to these two commands in a character vector
of length 2:
foo <- '3;4'
bar <- "don't ; use semicolons"

It's probably a regexp magic, but I just can't find it.

Any hint is highly appreciated,
Adrian

-- 
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania

[[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-es] Opinion/discusion segundo lenguaje

2016-09-15 Thread javier.ruben.marcuzzi
Estimado Fernando Macedo

Su pregunta debe ser dirigida en otro sentido, ¿Qué necesito hacer?, ¿Qué  
herramientas hay?

Java tiene librerías para análisis, pero casi queda en Android, algo en 
servidores, todas cosas que tienen otras formas o alternativas.

C#, F#, por ahí ahora que van hace multiplataforma, multi …., varias de esas 
cosas, son buenas alternativas (incluso Android y servidores).

C++, esta semana me llegó un correo desde INTEL donde hay herramientas que 
ofrecen para análisis, estadística, etc.

Computación de alto rendimiento, cualquiera de estos lenguajes tiene algo, se 
puede.

Otro punto, por ejemplo java a mi no me gusta, pero netbeans es genial para mi, 
autocompleta y me resulta muy cómodo. Ya no lo uso, porque abandoné java y php, 
pero la herramienta que colabore en la escritura es fundamental para evitar 
dolores de cabeza.

Yo cometí errores, lo que nunca me sale mal es ¿Qué necesito?, ¿Qué hay?, luego 
el ¿puedo?

Todos los lenguajes que nombre tienen algo para relacionarse con R, por tal 
motivo podría utilizarlos en conjunto. 

Javier Rubén Marcuzzi

De: Fernando Macedo
[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es

Re: [R] dplyr or plyr or both?

2016-09-15 Thread Christopher W. Ryan
Thank you Frans. This is exactly the sort of nuance that I want to learn 
about.


--Chris

Frans Marcelissen wrote:

Hello Christopher and others
:
What cannot be stressed enough is: do not combine both packages, it
gives errors and incorrect results! I will show that below

a<-data.frame(groep=1:4,v=1:40)
library(dplyr)
a %>% group_by(groep) %>% summarise(m=mean(v),n=n())
# groep m n
#   
# 1 11910
# 2 22010
# 3 32110
# 4 42210
# correct

library(plyr)
a %>% group_by(groep) %>% summarise(m=mean(v),n=n())

Error in n() : This function should not be called directly
# ???
a %>% group_by(groep) %>% summarise(m=mean(v))
# m
# 1 20.5
#incorrect!
--

So both n() and group_by from dplyr don't work after library(plyr)!

My advice is: do not use plyr. Unfortunately plyr has some functions
that are very important, and that are not in dplyr. For
instance: rbind.fill() (for combining the rows of two dataframes with
unequal columns). If you need this: do'nt library plyr, use plyr::rbind.fil

Until now I have the impression that it is also possible to library
dplyr after plyr, but it is better to remove plyr!

This is a serious problem that has been reported before, but not solved
(in dplyr 0.5.0 and plyr 1.8.4)

Frams

2016-09-15 16:09 GMT+02:00 Christopher W Ryan >:

I've set myself the task of learning about these packages, and about
tidy data concepts.

What is the relationship between plyr and dplyr?  Does the latter
replace the former (meaning I can concentrate on learning the latter)?
Or is there ever a need to use functions from both (meaning I should
learn both)?

Thanks.

--Chris Ryan

__
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-es] Saltar filas no numericas al importar csv

2016-09-15 Thread Fernando Macedo

Si:
Las filas no numéricas tienen algún patrón
Estas en un ambiente linux (aunque actualmente también se puede hacer en 
windows)

Y pensando fuera de R o usando system

Puedes usar sed antes de levantar los datos:

sed -i '/patrón/d' archivo.csv

Saludos.

Fernando Macedo

El 14/09/16 a las 13:37, javier.ruben.marcu...@gmail.com escribió:

Estimado Jesús Para Fernández

Entonces entiendo que el problema no es justo el saltar filas no numéricas, 
posiblemente en el siguiente ejemplo se explique el problema.

Los datos son algo como columnas y cada cierta cantidad  un encabezado como

Nombre  Árbol   identificación  altura  color
Pino navidad
Pino1   3   verde
Pino2   2   verde claro
….  …   …   …   …
Pino jardín 
Pino699 6   verde

¿Usted tiene los datos en un archivo de texto aproximado al ejemplo?

Javier Rubén Marcuzzi

De: Jesús Para Fernández
[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es

Re: [R] dplyr or plyr or both?

2016-09-15 Thread Frans Marcelissen
Hello Christopher and others
:
What cannot be stressed enough is: do not combine both packages, it gives
errors and incorrect results! I will show that below

a<-data.frame(groep=1:4,v=1:40)
library(dplyr)
a %>% group_by(groep) %>% summarise(m=mean(v),n=n())
# groep m n
#   
# 1 11910
# 2 22010
# 3 32110
# 4 42210
# correct

library(plyr)
a %>% group_by(groep) %>% summarise(m=mean(v),n=n())

Error in n() : This function should not be called directly
# ???
a %>% group_by(groep) %>% summarise(m=mean(v))
# m
# 1 20.5
#incorrect!
--

So both n() and group_by from dplyr don't work after library(plyr)!

My advice is: do not use plyr. Unfortunately plyr has some functions that
are very important, and that are not in dplyr. For instance: rbind.fill()
(for combining the rows of two dataframes with unequal columns). If you
need this: do'nt library plyr, use plyr::rbind.fil

Until now I have the impression that it is also possible to library dplyr
after plyr, but it is better to remove plyr!

This is a serious problem that has been reported before, but not solved (in
dplyr 0.5.0 and plyr 1.8.4)

Frams

2016-09-15 16:09 GMT+02:00 Christopher W Ryan :

> I've set myself the task of learning about these packages, and about
> tidy data concepts.
>
> What is the relationship between plyr and dplyr?  Does the latter
> replace the former (meaning I can concentrate on learning the latter)?
> Or is there ever a need to use functions from both (meaning I should
> learn both)?
>
> Thanks.
>
> --Chris Ryan
>
> __
> 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] Better use of regex

2016-09-15 Thread Bert Gunter
Thanks for the reproducible example.

Using regular expressions:

sub(".*HS_(.).*", "\\1", dimInfo[grep("HS_",dimInfo)])

The grep() gets just the indices that contain "HS_" and the sub()
picks up the character you want from the subvector indexed by them and
replaces everything with it.

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 Thu, Sep 15, 2016 at 9:17 AM, Doran, Harold  wrote:
> I have produced a terribly inefficient piece of codes. In the end, it gives 
> exactly what I need, but clumsily steps through multiple steps which I'm sure 
> could be more efficiently reduced.
>
> Below is a reproducible example. What I have to begin with is character 
> vector, dimInfo. What I want to do is parse this vector 1) find the elements 
> containing 'HS' and 2) grab *only* the first character after the "HS_". The 
> final line of code in the example gives what I need.
>
> Any suggestions on a better approach?
>
> Harold
>
>
> dimInfo <- c("RecordID", "oppID", "position", "key", "operational", 
> "IsSelected",
> "score", "item_1_HS_conv_ovrl_scr", "item_1_HS_elab_ovrl_scr",
> "item_1_HS_org_ovrl_scr")
>
> ff <- dimInfo[grep('HS', dimInfo)]
> gg <- strsplit(ff, 'HS_')
> hh <- sapply(1:3, function(i) gg[[i]][2])
> substr(hh, 1, 1)
>
> __
> 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] Better use of regex

2016-09-15 Thread ruipbarradas

Hello,

What about the following?

ff <- dimInfo[grep('HS', dimInfo)]
sub("^.*HS_([[:alnum:]]).*$", "\\1", ff)


Hope this helps,

Rui Barradas


Citando Doran, Harold :

I have produced a terribly inefficient piece of codes. In the end,  
it gives exactly what I need, but clumsily steps through multiple  
steps which I'm sure could be more efficiently reduced.


Below is a reproducible example. What I have to begin with is  
character vector, dimInfo. What I want to do is parse this vector 1)  
find the elements containing 'HS' and 2) grab *only* the first  
character after the "HS_". The final line of code in the example  
gives what I need.


Any suggestions on a better approach?

Harold


dimInfo <- c("RecordID", "oppID", "position", "key", "operational",  
"IsSelected",

"score", "item_1_HS_conv_ovrl_scr", "item_1_HS_elab_ovrl_scr",
"item_1_HS_org_ovrl_scr")

ff <- dimInfo[grep('HS', dimInfo)]
gg <- strsplit(ff, 'HS_')
hh <- sapply(1:3, function(i) gg[[i]][2])
substr(hh, 1, 1)

__
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] Better use of regex

2016-09-15 Thread Bob Rudis
Base:

Filter(Negate(is.na), sapply(regmatches(dimInfo, regexec("HS_(.{1})",
dimInfo)), "[", 2))

Modernverse:

library(stringi)
library(purrr)

stri_match_first_regex(dimInfo, "HS_(.{1})")[,2] %>%
  discard(is.na)


They both use capture groups to find the matches and return just the
matches. The "{1}" isn't really necessary but I include to show that you
can match whatever lengths you want, in this case just 1 char.

On Thu, Sep 15, 2016 at 12:17 PM, Doran, Harold  wrote:

> I have produced a terribly inefficient piece of codes. In the end, it
> gives exactly what I need, but clumsily steps through multiple steps which
> I'm sure could be more efficiently reduced.
>
> Below is a reproducible example. What I have to begin with is character
> vector, dimInfo. What I want to do is parse this vector 1) find the
> elements containing 'HS' and 2) grab *only* the first character after the
> "HS_". The final line of code in the example gives what I need.
>
> Any suggestions on a better approach?
>
> Harold
>
>
> dimInfo <- c("RecordID", "oppID", "position", "key", "operational",
> "IsSelected",
> "score", "item_1_HS_conv_ovrl_scr", "item_1_HS_elab_ovrl_scr",
> "item_1_HS_org_ovrl_scr")
>
> ff <- dimInfo[grep('HS', dimInfo)]
> gg <- strsplit(ff, 'HS_')
> hh <- sapply(1:3, function(i) gg[[i]][2])
> substr(hh, 1, 1)
>
> __
> 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] Better use of regex

2016-09-15 Thread Doran, Harold
I have produced a terribly inefficient piece of codes. In the end, it gives 
exactly what I need, but clumsily steps through multiple steps which I'm sure 
could be more efficiently reduced.

Below is a reproducible example. What I have to begin with is character vector, 
dimInfo. What I want to do is parse this vector 1) find the elements containing 
'HS' and 2) grab *only* the first character after the "HS_". The final line of 
code in the example gives what I need.

Any suggestions on a better approach?

Harold


dimInfo <- c("RecordID", "oppID", "position", "key", "operational", 
"IsSelected", 
"score", "item_1_HS_conv_ovrl_scr", "item_1_HS_elab_ovrl_scr", 
"item_1_HS_org_ovrl_scr")

ff <- dimInfo[grep('HS', dimInfo)]
gg <- strsplit(ff, 'HS_')
hh <- sapply(1:3, function(i) gg[[i]][2])
substr(hh, 1, 1)

__
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-es] Opinion/discusion segundo lenguaje

2016-09-15 Thread Fernando Macedo
Buenas a todos, ando recabando opiniones al respecto de introducirme en 
un nuevo lenguaje (más allá del "hola mundo" que lo debo haber hecho 
cientos de veces).


Particularmente no creo ser un gran programador en R pero me revuelvo 
con el google y la lista de compañeros logro sacar mis cosas adelante y 
he aprendido un montón en los últimos años. Por otro lado dado que 
trabajo con archivos de genotipados y semejantes he aprendido cosas 
básicas de sed y awk que me resuelven problemas curriculares.


Lo cierto es que hace un tiempo se me ocurrió que tenía que aprender a 
usar (más profundamente) Python. Luego en intercambio de opiniones con 
otros compañeros ya me surgen las dudas. ¿Para que?


Entiendo que R puede ser medio lerdo en algunas cosas pero con el 
desarrollo que ha tenido y mantiene muchas de sus limitaciones se han 
ido salvando y hay paquetes para paralelizar, vectorizar, etc que lo 
hacen cada vez más ágil.


Sabiendo que en la lista hay muchos "poliglotas", ¿ustedes que opinan?, 
¿vale la pena entrar en un lenguaje nuevo o continuar profundizando en R?
Si vale la pena python, ¿en que cosas? ¿Archivos grandes? ¿Algún proceso 
en particular que es más efectivo? ¿conexión con base de datos?


Me han surgido esas dudas, y aunque no sea una consulta específica de R 
me pareció apropiada e interesante para plantearla en la lista.


Si les parece no apropiada pido mis disculpas de antemano.

Saludos!

--
Fernando Macedo

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R] dplyr or plyr or both?

2016-09-15 Thread Bert Gunter
I see no reason to bother Hadley in the age of google.

Search on "dplyr versus plyr" and read what you get! (on the first hit, even)

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 Thu, Sep 15, 2016 at 7:20 AM,   wrote:
> Hello,
>
> Maybe you should ask the maintainer of both packages.
>
>> maintainer("plyr")
>
> [1] "Hadley Wickham "
>>
>> maintainer("dplyr")
>
> [1] "Hadley Wickham "
>
> Hope this helps,
>
> Rui Barradas
>
>
>
> Citando Christopher W Ryan :
>
>
>> I've set myself the task of learning about these packages, and about
>> tidy data concepts.
>>
>> What is the relationship between plyr and dplyr?  Does the latter
>> replace the former (meaning I can concentrate on learning the latter)?
>> Or is there ever a need to use functions from both (meaning I should
>> learn both)?
>>
>> Thanks.
>>
>> --Chris Ryan
>>
>> __
>> 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] dplyr or plyr or both?

2016-09-15 Thread ruipbarradas

Hello,

Maybe you should ask the maintainer of both packages.


maintainer("plyr")

[1] "Hadley Wickham "

maintainer("dplyr")

[1] "Hadley Wickham "

Hope this helps,

Rui Barradas



Citando Christopher W Ryan :


I've set myself the task of learning about these packages, and about
tidy data concepts.

What is the relationship between plyr and dplyr?  Does the latter
replace the former (meaning I can concentrate on learning the latter)?
Or is there ever a need to use functions from both (meaning I should
learn both)?

Thanks.

--Chris Ryan

__
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] dplyr or plyr or both?

2016-09-15 Thread Christopher W Ryan
I've set myself the task of learning about these packages, and about
tidy data concepts.

What is the relationship between plyr and dplyr?  Does the latter
replace the former (meaning I can concentrate on learning the latter)?
Or is there ever a need to use functions from both (meaning I should
learn both)?

Thanks.

--Chris Ryan

__
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] why data.frame, mutate package and not lists

2016-09-15 Thread Rolf Turner

On 15/09/16 14:04, S Ellison wrote:




If you want
to add variable to data.frame you have to use attach, detach. Right?


I'd have said "not at all", not "not quite". attach and detach have
almost exactly nothing to do with adding to a data frame. You can add
to a data frame using dfrm$newvar <-  dfrm['newvar'] <-
 cbind(dfrm, newvar=) #adds a new variable
called 'newvar' rbind #to add rows merge #to add columns and/or rows
from another data frame ... and a few other things.

The only relevance of attach/detach is to do with the behaviour of
attached objects, not to do with adding to data frames. If you have
attach()ed something, changing the original object does not
automatically update the copy of its variables in the current
environment, or vice versa, because attach(), as documented, creates
a _copy_. So _if_ you have attach()ed a data frame - or a list - you
can't change the copy by changing the original object and you can't
change the original object by changing the copy.  Only if you need to
change both do you need to detach and reattach.

As a rule, I generally avoid attach() for that and other reasons
(most of which are listed in ?attach). attach()is only sensible if
you have already completed all the manipulation needed on the
attached object first. Even then, using with() is safer.


Extremely well and clearly put.  This is one of those "I wish *I* had 
said that!" posts.


cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
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] why data.frame, mutate package and not lists

2016-09-15 Thread S Ellison


>If you want
> to add variable to data.frame you have to use attach, detach. Right?

I'd have said "not at all", not "not quite". attach and detach have almost 
exactly nothing to do with adding to a data frame. 
You can add to a data frame using  
dfrm$newvar <- 
dfrm['newvar'] <-  
cbind(dfrm, newvar=) #adds a new variable called 'newvar'
rbind #to add rows
merge #to add columns and/or rows from another data frame
... and a few other things.

The only relevance of attach/detach is to do with the behaviour of attached 
objects, not to do with adding to data frames. If you have attach()ed 
something, changing the original object does not automatically update the copy 
of its variables in the current environment, or vice versa, because attach(), 
as documented, creates a _copy_. So _if_ you have attach()ed a data frame - or 
a list - you can't change the copy by changing the original object and you 
can't change the original object by changing the copy.  Only if you need to 
change both do you need to detach and reattach.

As a rule, I generally avoid attach() for that and other reasons (most of which 
are listed in ?attach). attach()is only sensible if you have already completed 
all the manipulation needed on the attached object first. Even then, using 
with() is safer.

S Ellison


***
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
__
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.