Re: [R] Issue replacing dataset values from read data

2016-05-06 Thread Jim Lemon
Hi Emily,
I haven't tested this exhaustively, but it seems to work:

df<-data.frame(id=2001:3300,yrssmoke=sample(1:40,1300,TRUE),
 cigsdaytotal=sample(1:60,1300,TRUE),yrsquit=sample(1:20,1300,TRUE))
dfNA<-sapply(df$id,"%in%",c(2165,2534,2553,2611,2983,3233))
# create your NA values
df[dfNA,c("yrsquit","packyrs")]<-NA
# since you know the NA id values
df[dfNA,"yrsquit"]<-0
df[dfNA,"packyrs"]<-df[dfNA,"yrssmoke"]*df[dfNA,"cigsdaytotal"]/20

Jim


On Sat, May 7, 2016 at 8:19 AM, Chang, Emily  wrote:
> Dear all,
>
> I am reading a modest dataset (2297 x 644) with specific values I want to 
> change. The code is inelegant but looks like this:
>
> df <- read.csv("mydata.csv", header = TRUE, stringsAsFactors = FALSE)
>
> # yrsquit, packyrs missing for following IDs. Manually change.
> for(myid in c(2165, 2534, 2553, 2611, 2983, 3233)){
>  temp <- subset(df, id == myid)
>  df[df$id == myid , "yrsquit"] <- 0
>  temp.yrssmoke <- temp$age-(temp$agesmoke+temp$yrsquit)
>  df[df$id == myid , "yrssmoke"]  <- temp.yrssmoke
>  df[df$id == myid , "packyrs"] <- (temp$cigsdaytotal/20)*(temp.yrssmoke)
> }
>
> If I run just the first line and then the for loop, it works.
> If I run the first line and for loop together, yrsquit is properly replaced 
> to == 0, but packyrs is NA still.
>
> Obviously there's many ways around this specific problem, but I was wondering 
> what the issue is here, so as to look out for and avoid it in the future.
>
> Apologies for the lack of reproducible code; I haven't yet reproduced the 
> problem with generated data.
>
> Much thanks in advance.
>
> Best regards,
> Emily
>
> [[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] month and output

2016-05-06 Thread David Winsemius

> On May 6, 2016, at 5:15 PM, Ashta  wrote:
> 
> Thank you very much David.
> 
> So there is no general formal that works year all round.
> 
> The first one work only Jan to Nov
> today <- Sys.Date()
> nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
> format(today,"%Y") )
> [1] "Jun2016"
> 
> The second one works only  for the last month of the year.
> today <- as.Date("2008-12-01")
> nextmo<- paste0(m <- month.abb[(as.numeric(format(today,
> format="%m"))+1) %/% 12] ,
>  as.numeric( format(today,"%Y") ) + (m == "Jan") )

Sorry;

This works as intended:

> today <- seq( from=as.Date("2008-1-01"), length=13, by="1 mo" )
> 
> nextmo<- paste0( m <- month.abb[ as.numeric(format(today, format="%m")) %% 
> 12+1] ,
+as.numeric( format(today,"%Y") ) + (m=="Jan") ); nextmo
 [1] "Feb2008" "Mar2008" "Apr2008" "May2008" "Jun2008" "Jul2008" "Aug2008" 
"Sep2008"
 [9] "Oct2008" "Nov2008" "Dec2008" "Jan2009" "Feb2009"



> nextmo
> 
> 
> Many thanks
> 
> 
> 
> 
> 
> On Fri, May 6, 2016 at 6:40 PM, David Winsemius  
> wrote:
>> 
>>> On May 6, 2016, at 4:30 PM, David Winsemius  wrote:
>>> 
>>> 
 On May 6, 2016, at 4:11 PM, Ashta  wrote:
 
 Hi all,
 
 I am trying to ge get the next month of the year.
 
 today <- Sys.Date()
 xx<- format(today, format="%B%Y")
 
 I got  "May2016",  but I want  Jun2016. How do I do that?
>>> 
>>> today <- Sys.Date()
>>> nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
>>>format(today,"%Y") )
>>> [1] "Jun2016"
>> 
>> It occurred to me that at the end of the year you would want to increment 
>> the year as well. This calculates the next month and increments the year 
>> value if needed:
>> 
>> today <- as.Date("2008-12-01")
>> nextmo<- paste0(m <- month.abb[(as.numeric(format(today, format="%m"))+1) 
>> %/% 12] ,
>>  as.numeric( format(today,"%Y") ) + (m == "Jan") )
>> nextmo
>> #[1] "Jan2009"
>>> 
 
 My other question is that, I read a data  and do some analysis  and I
 want to send all the results of the analysis to a pdf file
 
 Example
 x5 <- runif(15, 5.0, 7.5)
 x5
 
 
 I tried this one
 
 pdf(file=" test.pdf")
 x5
 dev.off()
>>> 
>>> pdf() opens a graphics device, so you need a function that establishes a 
>>> coordinate system:
>>> 
>>> x5 <- runif(15, 5.0, 7.5)
>>> pdf(file=" test.pdf");
>>> plot(1,1,type="n")
>>> text(1, 1, paste(round(x5, 2), collapse="\n") )
>>> dev.off()
>>> 
>> 
>> If you need to suppress the axes and their labels:
>> 
>> pdf(file=" test.pdf"); plot(1,1, type="n", axes=FALSE, xlab="", ylab="")
>> text(1, 1, paste(round(x5, 2), collapse="\n") )
>> dev.off()
>> 
>>> I doubt that this is what you really want, and suspect you really need to 
>>> be studying the capabilities supported by the knitr package. If I'm wrong 
>>> about that and you want a system that supports drawing and text on a blank 
>>> page, then first study:
>>> 
 library(grid)
 help(pac=grid)
>>> 
>>> If you choose that route then the text "R Graphics" by Paul Murrell will be 
>>> indispensable.
>>> 
>>> --
>>> 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.
>> 
>> David Winsemius
>> Alameda, CA, USA
>> 

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] replacing values of rows with identical row names in two dataframes

2016-05-06 Thread Jeff Newmiller
Please use reply-all to keep the mailing list in the loop,  and use plain text 
rather than HTML to make sure the your message gets through uncorrupted. 

?merge
?lapply

# untested
# align rows by date
df1a <- merge( df1, df2, by="date", all.x=TRUE )
# like-named columns have .x or .y appended
df1an0  <- grep( "\\.x$", names( df1a ), values=TRUE )
df1an <- substr( df1an0, 1, nchar( df1an0 ) - 2 )
# make a list of updated columns
df1b <- lapply( df1an, function(nm) { 
   nmx  <- paste0( nm, ".x" )
   nmy  <- paste0( nm, ".y" )
   ifelse( is.na( df1a[[ nmx ]] ), df1a[[ nmy ]], df1a[[ nmx ]] )
 }
# set the names of the fixed columns
df1b <- setNames( df1b, df1an )
# figure out the names of the non-duped columns
df1an1 <- grep( "\\.[xy]$", names( df1a ), invert =TRUE )
# make a new data frame
df1c  <- data.frame( df1a[ , df1an1, drop=FALSE ], df1b )

-- 
Sent from my phone. Please excuse my brevity.

On May 6, 2016 4:32:15 PM PDT, Saba Sehrish  wrote:
>No. If there is some other way, i would like to go for it.
>RegardsSaba 
>
>On Saturday, 7 May 2016, 11:30, Jeff Newmiller
> wrote:
> 
>
> Why would you want to use a for loop? Is this homework? 
>-- 
>Sent from my phone. Please excuse my brevity.
>
>On May 6, 2016 4:15:09 PM PDT, Saba Sehrish via R-help
> wrote:
>
>
>Hi 
>
>I have two dataframes(df1, df2) with equal number of columns (1566) but
>lesser rows in df2 (2772 in df1 and 40 in df2). Row names are 
>identical in both dataframes (date). I want to replace NAs of df1 with
>the values of df2 for all those rows having identical row names (date)
>but 
>without affecting already existing values in those rows of df1. 
>
>Please see below: 
>
>df1: 
>date 11A  11A   21B   3CC   3CC 
>20040101  100   150   NA   NA   140 
>20040115   200   NA   200   NA   NA 
>20040131   NA   165   180   190   190 
>20040205   NA   NA   NA   NA   NA 
>20040228   NA   NA   NA   NA   NA 
>20040301  150   155   170   150   160 
>20040315   NA   NA   180   190   200 
>20040331   NA   NA   NA   175   180 
>
>df2: 
>date 11A  11A   21B   3CC   3CC 
>20040131   170   NA   NA   NA   NA 
>20040228   140   145   165   150   155 
>20040331   NA  
>145   160   NA   NA 
>
>I want the resulting dataframe to be: 
>
>df3: 
>date 11A  11A   21B   3CC   3CC 
>20040101  100   150   NA   NA   140 
>20040115  200   NA   200   NA   NA 
>20040131  170   165   180   190   190 
>20040205  NA   NA   NA   NA   NA 
>20040228  140   145   165   150   155 
>20040301  150   155   170   150   160 
>20040315  NA   NA   180   190   200 
>20040331  NA   145   160   175   180 
>
>If it is possible, I would prefer to use "for loop" and "which"
>function to achieve the result. 
>
>Please guide me in this regard. 
>
>Thanks 
>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.
>
>
>
>   

[[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] Issue replacing dataset values from read data

2016-05-06 Thread Chang, Emily
Dear all,

I am reading a modest dataset (2297 x 644) with specific values I want to 
change. The code is inelegant but looks like this:

df <- read.csv("mydata.csv", header = TRUE, stringsAsFactors = FALSE)

# yrsquit, packyrs missing for following IDs. Manually change.
for(myid in c(2165, 2534, 2553, 2611, 2983, 3233)){
 temp <- subset(df, id == myid)
 df[df$id == myid , "yrsquit"] <- 0
 temp.yrssmoke <- temp$age-(temp$agesmoke+temp$yrsquit)
 df[df$id == myid , "yrssmoke"]  <- temp.yrssmoke
 df[df$id == myid , "packyrs"] <- (temp$cigsdaytotal/20)*(temp.yrssmoke)
}

If I run just the first line and then the for loop, it works.
If I run the first line and for loop together, yrsquit is properly replaced to 
== 0, but packyrs is NA still.

Obviously there's many ways around this specific problem, but I was wondering 
what the issue is here, so as to look out for and avoid it in the future.

Apologies for the lack of reproducible code; I haven't yet reproduced the 
problem with generated data.

Much thanks in advance.

Best regards,
Emily

[[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] month and output

2016-05-06 Thread William Dunlap via R-help
You could install and load the 'lubridate' package, which has month()
and month<-() functions so you can do the following:

> z <- as.Date(c("2015-01-29", "2016-01-29", "2016-05-07", "2016-12-25"))
> z
[1] "2015-01-29" "2016-01-29" "2016-05-07" "2016-12-25"
> month(z) <- month(z) + 1
> z
[1] NA   "2016-02-29" "2016-06-07" "2017-01-25"



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, May 6, 2016 at 5:15 PM, Ashta  wrote:

> Thank you very much David.
>
> So there is no general formal that works year all round.
>
> The first one work only Jan to Nov
> today <- Sys.Date()
> nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
>  format(today,"%Y") )
> [1] "Jun2016"
>
> The second one works only  for the last month of the year.
> today <- as.Date("2008-12-01")
>  nextmo<- paste0(m <- month.abb[(as.numeric(format(today,
> format="%m"))+1) %/% 12] ,
>   as.numeric( format(today,"%Y") ) + (m == "Jan") )
>  nextmo
>
>
> Many thanks
>
>
>
>
>
> On Fri, May 6, 2016 at 6:40 PM, David Winsemius 
> wrote:
> >
> >> On May 6, 2016, at 4:30 PM, David Winsemius 
> wrote:
> >>
> >>
> >>> On May 6, 2016, at 4:11 PM, Ashta  wrote:
> >>>
> >>> Hi all,
> >>>
> >>> I am trying to ge get the next month of the year.
> >>>
> >>> today <- Sys.Date()
> >>> xx<- format(today, format="%B%Y")
> >>>
> >>> I got  "May2016",  but I want  Jun2016. How do I do that?
> >>
> >> today <- Sys.Date()
> >> nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
> >> format(today,"%Y") )
> >> [1] "Jun2016"
> >
> > It occurred to me that at the end of the year you would want to
> increment the year as well. This calculates the next month and increments
> the year value if needed:
> >
> >  today <- as.Date("2008-12-01")
> >  nextmo<- paste0(m <- month.abb[(as.numeric(format(today,
> format="%m"))+1) %/% 12] ,
> >   as.numeric( format(today,"%Y") ) + (m == "Jan") )
> >  nextmo
> > #[1] "Jan2009"
> >>
> >>>
> >>> My other question is that, I read a data  and do some analysis  and I
> >>> want to send all the results of the analysis to a pdf file
> >>>
> >>> Example
> >>> x5 <- runif(15, 5.0, 7.5)
> >>> x5
> >>>
> >>>
> >>> I tried this one
> >>>
> >>> pdf(file=" test.pdf")
> >>> x5
> >>> dev.off()
> >>
> >> pdf() opens a graphics device, so you need a function that establishes
> a coordinate system:
> >>
> >> x5 <- runif(15, 5.0, 7.5)
> >> pdf(file=" test.pdf");
> >> plot(1,1,type="n")
> >> text(1, 1, paste(round(x5, 2), collapse="\n") )
> >> dev.off()
> >>
> >
> > If you need to suppress the axes and their labels:
> >
> >  pdf(file=" test.pdf"); plot(1,1, type="n", axes=FALSE, xlab="", ylab="")
> >  text(1, 1, paste(round(x5, 2), collapse="\n") )
> >  dev.off()
> >
> >> I doubt that this is what you really want, and suspect you really need
> to be studying the capabilities supported by the knitr package. If I'm
> wrong about that and you want a system that supports drawing and text on a
> blank page, then first study:
> >>
> >>> library(grid)
> >>> help(pac=grid)
> >>
> >> If you choose that route then the text "R Graphics" by Paul Murrell
> will be indispensable.
> >>
> >> --
> >> 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.
> >
> > 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] month and output

2016-05-06 Thread Ashta
Thank you very much David.

So there is no general formal that works year all round.

The first one work only Jan to Nov
today <- Sys.Date()
nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
 format(today,"%Y") )
[1] "Jun2016"

The second one works only  for the last month of the year.
today <- as.Date("2008-12-01")
 nextmo<- paste0(m <- month.abb[(as.numeric(format(today,
format="%m"))+1) %/% 12] ,
  as.numeric( format(today,"%Y") ) + (m == "Jan") )
 nextmo


Many thanks





On Fri, May 6, 2016 at 6:40 PM, David Winsemius  wrote:
>
>> On May 6, 2016, at 4:30 PM, David Winsemius  wrote:
>>
>>
>>> On May 6, 2016, at 4:11 PM, Ashta  wrote:
>>>
>>> Hi all,
>>>
>>> I am trying to ge get the next month of the year.
>>>
>>> today <- Sys.Date()
>>> xx<- format(today, format="%B%Y")
>>>
>>> I got  "May2016",  but I want  Jun2016. How do I do that?
>>
>> today <- Sys.Date()
>> nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
>> format(today,"%Y") )
>> [1] "Jun2016"
>
> It occurred to me that at the end of the year you would want to increment the 
> year as well. This calculates the next month and increments the year value if 
> needed:
>
>  today <- as.Date("2008-12-01")
>  nextmo<- paste0(m <- month.abb[(as.numeric(format(today, format="%m"))+1) 
> %/% 12] ,
>   as.numeric( format(today,"%Y") ) + (m == "Jan") )
>  nextmo
> #[1] "Jan2009"
>>
>>>
>>> My other question is that, I read a data  and do some analysis  and I
>>> want to send all the results of the analysis to a pdf file
>>>
>>> Example
>>> x5 <- runif(15, 5.0, 7.5)
>>> x5
>>>
>>>
>>> I tried this one
>>>
>>> pdf(file=" test.pdf")
>>> x5
>>> dev.off()
>>
>> pdf() opens a graphics device, so you need a function that establishes a 
>> coordinate system:
>>
>> x5 <- runif(15, 5.0, 7.5)
>> pdf(file=" test.pdf");
>> plot(1,1,type="n")
>> text(1, 1, paste(round(x5, 2), collapse="\n") )
>> dev.off()
>>
>
> If you need to suppress the axes and their labels:
>
>  pdf(file=" test.pdf"); plot(1,1, type="n", axes=FALSE, xlab="", ylab="")
>  text(1, 1, paste(round(x5, 2), collapse="\n") )
>  dev.off()
>
>> I doubt that this is what you really want, and suspect you really need to be 
>> studying the capabilities supported by the knitr package. If I'm wrong about 
>> that and you want a system that supports drawing and text on a blank page, 
>> then first study:
>>
>>> library(grid)
>>> help(pac=grid)
>>
>> If you choose that route then the text "R Graphics" by Paul Murrell will be 
>> indispensable.
>>
>> --
>> 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.
>
> 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] month and output

2016-05-06 Thread Bert Gunter
To add to what David said, maybe you want ?sink or ?capture.output  .

If you're really looking to combine your own text and R output, than
knitr is probably what you want. The RStudio ide integrates this
stuff, so you may want to look at that, too.

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 Fri, May 6, 2016 at 4:11 PM, Ashta  wrote:
> Hi all,
>
> I am trying to ge get the next month of the year.
>
> today <- Sys.Date()
> xx<- format(today, format="%B%Y")
>
> I got  "May2016",  but I want  Jun2016. How do I do that?
>
> My other question is that, I read a data  and do some analysis  and I
> want to send all the results of the analysis to a pdf file
>
> Example
> x5 <- runif(15, 5.0, 7.5)
> x5
>
>
> I tried this one
>
>  pdf(file=" test.pdf")
>  x5
> dev.off()
>
> I found the file is empty. I would appreciate if you help me out.
>
> Thanks in advance
>
> __
> 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] month and output

2016-05-06 Thread David Winsemius

> On May 6, 2016, at 4:30 PM, David Winsemius  wrote:
> 
> 
>> On May 6, 2016, at 4:11 PM, Ashta  wrote:
>> 
>> Hi all,
>> 
>> I am trying to ge get the next month of the year.
>> 
>> today <- Sys.Date()
>> xx<- format(today, format="%B%Y")
>> 
>> I got  "May2016",  but I want  Jun2016. How do I do that?
> 
> today <- Sys.Date()
> nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
> format(today,"%Y") )
> [1] "Jun2016"

It occurred to me that at the end of the year you would want to increment the 
year as well. This calculates the next month and increments the year value if 
needed:

 today <- as.Date("2008-12-01")
 nextmo<- paste0(m <- month.abb[(as.numeric(format(today, format="%m"))+1) %/% 
12] , 
  as.numeric( format(today,"%Y") ) + (m == "Jan") )
 nextmo
#[1] "Jan2009"
> 
>> 
>> My other question is that, I read a data  and do some analysis  and I
>> want to send all the results of the analysis to a pdf file
>> 
>> Example
>> x5 <- runif(15, 5.0, 7.5)
>> x5
>> 
>> 
>> I tried this one
>> 
>> pdf(file=" test.pdf")
>> x5
>> dev.off()
> 
> pdf() opens a graphics device, so you need a function that establishes a 
> coordinate system:
> 
> x5 <- runif(15, 5.0, 7.5)
> pdf(file=" test.pdf"); 
> plot(1,1,type="n")
> text(1, 1, paste(round(x5, 2), collapse="\n") )
> dev.off()
> 

If you need to suppress the axes and their labels:

 pdf(file=" test.pdf"); plot(1,1, type="n", axes=FALSE, xlab="", ylab="")
 text(1, 1, paste(round(x5, 2), collapse="\n") )
 dev.off()

> I doubt that this is what you really want, and suspect you really need to be 
> studying the capabilities supported by the knitr package. If I'm wrong about 
> that and you want a system that supports drawing and text on a blank page, 
> then first study:
> 
>> library(grid)
>> help(pac=grid)
> 
> If you choose that route then the text "R Graphics" by Paul Murrell will be 
> indispensable.
> 
> -- 
> 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.

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] Fwd: tcltk: click and return table cell index

2016-05-06 Thread Dalthorp, Daniel
Thanks, John.

The trouble with that solution is that it gives the index for where the
cursor was before clicking rather than the cell that was clicked. The
solution is that the  binding gives the x, y pixel coordinates of
the click to the callback, and those just need to be translated to cell
index via tkindex ( My (almost) solution gives the pixel coordinates of the
mouse click. There is a function that converts the coords to cell index,
but I was having trouble figuring out the format of parameters, but the
following works:

tcl(table1, "index", as.tclObj(paste0("@",x, ",", y)))

I.e., to retrieve the index of the cell that is clicked on:

# create table
tt<-tktoplevel()
tclRequire("Tktable")
table1<-tkwidget(tt,"table",rows=3,cols=3)
tkgrid(table1)

tkbind(table1, "", function(x, y){
  cellIndex<-tcl(table1, "index", as.tclObj(paste0("@",x, ",", y)))
  print(tclvalue(cellIndex))
})

Thanks again!

-Dan


On Sat, Apr 30, 2016 at 6:43 AM, Fox, John  wrote:

> Dear Daniel,
>
> Try
>
> tkbind(table1, "", function(){
>  res <- try(tclvalue(tkindex(table1, "active")), silent=TRUE)
>if (inherits(res, "try-error")) print (NULL)
>else print(res)
> })
>
> I put in the calls to print() so that you could see how it works.
>
> I hope this helps,
>  John
>
> -
> John Fox, Professor
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> Web: socserv.mcmaster.ca/jfox
>
>
> > -Original Message-
> > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of
> Dalthorp,
> > Daniel
> > Sent: April 29, 2016 1:42 PM
> > To: r-help@R-project.org (r-help@r-project.org) 
> > Subject: [R] tcltk: click and return table cell index
> >
> > I'm struggling mightily with what should be a simple task...when a user
> clicks
> > on a cell in a tcltk table widget, I need to know which cell was clicked.
> >
> > One idea that gives a cryptic error:
> > tkbind(table1, "", function(x, y){
> >   tcl(table1, "index", x, y)
> > }
> >
> > # x, y give pixel coordinates; "index" should give cell coordinates, but
> format
> > must be correct
> >
> > I get an error message:
> >
> > wrong # args: should be ".25.1 index  ?row|col?".
> >
> > To which I respond, "Yes, I know I have the format wrong, but how can I
> make
> > sense of THAT?"
> >
> > Does anyone know a simple fix?
> >
> > Much appreciated!
> >
> > -Dan
> >
> > --
> > Dan Dalthorp, PhD
> > USGS Forest and Rangeland Ecosystem Science Center Forest Sciences Lab,
> Rm
> > 189
> > 3200 SW Jefferson Way
> > Corvallis, OR 97331
> > ph: 541-750-0953
> > ddalth...@usgs.gov
> >
> >   [[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.
>



-- 
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 189
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
ddalth...@usgs.gov




-- 
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 189
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
ddalth...@usgs.gov

[[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] month and output

2016-05-06 Thread David Winsemius

> On May 6, 2016, at 4:11 PM, Ashta  wrote:
> 
> Hi all,
> 
> I am trying to ge get the next month of the year.
> 
> today <- Sys.Date()
> xx<- format(today, format="%B%Y")
> 
> I got  "May2016",  but I want  Jun2016. How do I do that?

today <- Sys.Date()
nextmo<- paste0( month.abb[ as.numeric(format(today, format="%m"))+1] ,
 format(today,"%Y") )
[1] "Jun2016"

> 
> My other question is that, I read a data  and do some analysis  and I
> want to send all the results of the analysis to a pdf file
> 
> Example
> x5 <- runif(15, 5.0, 7.5)
> x5
> 
> 
> I tried this one
> 
> pdf(file=" test.pdf")
> x5
> dev.off()

pdf() opens a graphics device, so you need a function that establishes a 
coordinate system:

x5 <- runif(15, 5.0, 7.5)
pdf(file=" test.pdf"); 
plot(1,1,type="n")
text(1, 1, paste(round(x5, 2), collapse="\n") )
dev.off()

I doubt that this is what you really want, and suspect you really need to be 
studying the capabilities supported by the knitr package. If I'm wrong about 
that and you want a system that supports drawing and text on a blank page, then 
first study:

> library(grid)
> help(pac=grid)

If you choose that route then the text "R Graphics" by Paul Murrell will be 
indispensable.

-- 
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] replacing values of rows with identical row names in two dataframes

2016-05-06 Thread Jeff Newmiller
Why would you want to use a for loop?  Is this homework? 
-- 
Sent from my phone. Please excuse my brevity.

On May 6, 2016 4:15:09 PM PDT, Saba Sehrish via R-help  
wrote:
>
>
>Hi 
>
>I have two dataframes(df1, df2) with equal number of columns (1566) but
>lesser rows in df2 (2772 in df1 and 40 in df2). Row names are 
>identical in both dataframes (date). I want to replace NAs of df1 with
>the values of df2 for all those rows having identical row names (date)
>but 
>without affecting already existing values in those rows of df1. 
>
>Please see below: 
>
>df1: 
>date 11A  11A   21B   3CC   3CC 
>20040101  100   150   NA   NA   140 
>20040115   200   NA   200   NA   NA 
>20040131   NA   165   180   190   190 
>20040205   NA   NA   NA   NA   NA 
>20040228   NA   NA   NA   NA   NA 
>20040301  150   155   170   150   160 
>20040315   NA   NA   180   190   200 
>20040331   NA   NA   NA   175   180 
>
>df2: 
>date 11A  11A   21B   3CC   3CC 
>20040131   170   NA   NA   NA   NA 
>20040228   140   145   165   150   155 
>20040331   NA   145   160   NA   NA 
>
>I want the resulting dataframe to be: 
>
>df3: 
>date 11A  11A   21B   3CC   3CC 
>20040101  100   150   NA   NA   140 
>20040115  200   NA   200   NA   NA 
>20040131  170   165   180   190   190 
>20040205  NA   NA   NA   NA   NA 
>20040228  140   145   165   150   155 
>20040301  150   155   170   150   160 
>20040315  NA   NA   180   190   200 
>20040331  NA   145   160   175   180 
>
>If it is possible, I would prefer to use "for loop" and "which"
>function to achieve the result. 
>
>Please guide me in this regard. 
>
>Thanks 
>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.

[[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] replacing values of rows with identical row names in two dataframes

2016-05-06 Thread Saba Sehrish via R-help


Hi 

I have two dataframes(df1, df2) with equal number of columns (1566) but lesser 
rows in df2 (2772 in df1 and 40 in df2). Row names are 
identical in both dataframes (date). I want to replace NAs of df1 with the 
values of df2 for all those rows having identical row names (date) but 
without affecting already existing values in those rows of df1. 

Please see below: 

df1: 
date 11A  11A   21B   3CC   3CC 
20040101  100   150   NA   NA   140 
20040115   200   NA   200   NA   NA 
20040131   NA   165   180   190   190 
20040205   NA   NA   NA   NA   NA 
20040228   NA   NA   NA   NA   NA 
20040301  150   155   170   150   160 
20040315   NA   NA   180   190   200 
20040331   NA   NA   NA   175   180 

df2: 
date 11A  11A   21B   3CC   3CC 
20040131   170   NA   NA   NA   NA 
20040228   140   145   165   150   155 
20040331   NA   145   160   NA   NA 

I want the resulting dataframe to be: 

df3: 
date 11A  11A   21B   3CC   3CC 
20040101  100   150   NA   NA   140 
20040115  200   NA   200   NA   NA 
20040131  170   165   180   190   190 
20040205  NA   NA   NA   NA   NA 
20040228  140   145   165   150   155 
20040301  150   155   170   150   160 
20040315  NA   NA   180   190   200 
20040331  NA   145   160   175   180 

If it is possible, I would prefer to use "for loop" and "which" function to 
achieve the result. 

Please guide me in this regard. 

Thanks 
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] month and output

2016-05-06 Thread Ashta
Hi all,

I am trying to ge get the next month of the year.

today <- Sys.Date()
xx<- format(today, format="%B%Y")

I got  "May2016",  but I want  Jun2016. How do I do that?

My other question is that, I read a data  and do some analysis  and I
want to send all the results of the analysis to a pdf file

Example
x5 <- runif(15, 5.0, 7.5)
x5


I tried this one

 pdf(file=" test.pdf")
 x5
dev.off()

I found the file is empty. I would appreciate if you help me out.

Thanks in advance

__
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] with vs. attach

2016-05-06 Thread Hadley Wickham
You may want to read http://rpubs.com/hadley/157957, which captures my
latest thinking (and tooling) around this problem. Feedback is much
appreciated.

Hadley

On Fri, May 6, 2016 at 2:14 PM, David Winsemius  wrote:
>
>> On May 6, 2016, at 5:47 AM, Spencer Graves 
>>  wrote:
>>
>>
>>
>> On 5/6/2016 6:46 AM, peter dalgaard wrote:
>>> On 06 May 2016, at 02:43 , David Winsemius  wrote:
>>>
> On May 5, 2016, at 5:12 PM, Spencer Graves 
>  wrote:
>
> I want a function to evaluate one argument
> in the environment of a data.frame supplied
> as another argument.  "attach" works for
> this, but "with" does not.  Is there a way
> to make "with" work?  I'd rather not attach
> the data.frame.
>
>
> With the following two functions "eval.w.attach"
> works but "eval.w.with" fails:
>
>
> dat <- data.frame(a=1:2)
> eval.w.attach <- function(x, dat){
>  attach(dat)
>  X <- x
>  detach()
>  X
> }
>
> eval.w.with <- function(x, dat){
>  with(dat, x)
> }
>
> eval.w.attach(a/2, dat) # returns c(.5, 1)
 How about using eval( substitute( ...))?

 eval.w.sub <- function(expr, datt){
   eval( substitute(expr), env=datt)
 }
 eval.w.sub(a/2, dat)
 #[1] 0.5 1.0


>>> Actually, I think a better overall strategy is to say that if you want to 
>>> pass an expression to a function, then pass an expression object (or a call 
>>> object or maybe a formula object).
>>>
>>> Once you figure out _how_ your eval.w.attach works (sort of), you'll get 
>>> the creeps:
>>>
>>> Lazy evaluation causes the argument x to be evaluated after the attach(), 
>>> hence the evaluation environment of an actual argument is being temporarily 
>>> modified from inside a function.
>>>
>>> Apart from upsetting computer science purists, there could be hidden 
>>> problems: One major issue is that  values in "dat" could be masked by 
>>> values in the global environment, another issue is that an error in 
>>> evaluating the expression will leave dat attached. So at a minimum, you 
>>> need to recode using on.exit() magic.
>>>
>>> So my preferences go along these lines:
>>>
 dat <- data.frame(a=1:2)
 eval.expression <- function(e, dat) eval(e, dat)
 eval.expression(quote(a/2), dat)
>>> [1] 0.5 1.0
 eval.expression(expression(a/2), dat)
>>> [1] 0.5 1.0
>>>
 eval.formula <- function(f, dat) eval(f[[2]], dat)
 eval.formula(~a/2, dat)
>>> [1] 0.5 1.0
>>
>> Hi, Peter:
>>
>>
>>  I don't like eval.expression or eval.formula, because they don't 
>> automatically accept what I naively thought should work and require more 
>> knowledge of the user.  What about David's eval.w.sub:
>>
>>
>> a <- pi
>> dat <- data.frame(a=1:2)
>> eval.w.sub <- function(a, Dat){
>>  eval( substitute(a), env=Dat)
>> }
>> > eval.w.sub(a/2, dat)
>> [1] 0.5 1.0
>
> I liked eval.expression and tested it with a bquote(...) argument to see if 
> that would succeed. It did, but it didn't return what you wanted for `a/2`, 
> so I tried seeing if a "double eval wuold deliver both yours and my desired 
> results:
>
>  eval.w.sub <- function(a, Dat){
>   eval( eval(substitute(a),Dat), env=Dat)
>  }
> x=2
>  eval.w.sub( a/2, dat)
> [1] 0.5 1.0
>  eval.w.sub( bquote(2*a*.(x) ), dat)
> [1] 4 8
>
> We are here retracing the path the Hadley took in some of his ggplot2 design 
> decsions. Unfortunately for me those NSE rules often left me confused about 
> what should and shouldn't be 'quoted' in the as-character sense and what 
> should be quote()-ed or "unquoted" in the bquote() sense.
> --
>
>>
>>
>>
>>  This produces what's desired in a way that seems simpler to me.
>>
>>
>>  By the way, I really appreciate Peter's insightful comments:
>>
>>
>> eval.w.attachOops <- function(x, Dat){
>>  attach(Dat)
>>  X <- x
>>  detach()
>>  X
>> }
>> > eval.w.attachOops(a/2, dat)
>> The following object is masked _by_ .GlobalEnv:
>>
>>a
>>
>> [1] 1.570796
>> > eval.w.attachOops(b/2, dat)
>> The following object is masked _by_ .GlobalEnv:
>>
>>a
>>
>> Error in eval.w.attachOops(b/2, dat) : object 'b' not found
>> > search()
>> [1] ".GlobalEnv""Dat"   "package:graphics"
>> [4] "package:grDevices" "package:utils" "package:datasets"
>> [7] "package:methods"   "Autoloads" "package:base"
>> > objects(2)
>> [1] "a"
>>
>> *** NOTES:
>>
>>
>>  1.  This gives a likely wrong answer with a warning if "a" exists in 
>> .GlobalEnv, and leaves "Dat" (NOT "dat") attached upon exit.
>>
>>
>>
>>  2.  A stray "detach()" [not shown here] detached "package:stats".  oops.
>>
>>
>> *** Using "on.exit" fixes the problem with failure to detach but not the 
>> likely wrong answer:
>>
>>
>> detach()
>> search()
>> eval.w.attachStillWrong <- function(x, dat){
>>  attach(dat)
>>  on.exit(detach(dat))
>>  X <- x
>>  X
>> }
>> The following object is masked _by_ .GlobalEnv:
>>
>>a
>>
>> [1] 1.570796

Re: [R] Help needed with successfully downloading and opening Agricolae package

2016-05-06 Thread Phillips,Douglas A
Thanks Sarah, downloaded the sp package separately and that resolved the error. 

> On May 6, 2016, at 5:43 PM, David Winsemius  wrote:
> 
> 
>> On May 6, 2016, at 1:41 PM, Sarah Goslee  wrote:
>> 
>> On Fri, May 6, 2016 at 4:33 PM, Jeff Newmiller  
>> wrote:
>>> I am puzzled why the original install.packages call did not also download
>>> the sp package, since the default argument dependencies = NA should have
>>> triggered installation of imports including spDep, which should in turn have
>>> installed the dependencies including the sp package. Anyone have a theory?
> 
> I think you need to set dependencies=TRUE to make the checks recursive.
> 
> -- 
> David.
> 
>> 
>> Any hypothesis would require more information, sessionInfo() at a very 
>> minimum.
>> 
>> 
>>> --
>>> Sent from my phone. Please excuse my brevity.
>>> 
 On May 6, 2016 12:47:44 PM PDT, Sarah Goslee  
 wrote:
 
 This is a plain-text email list, so your red doesn't show up, but
 since the error message said that the installer couldn't find the sp
 package, I'd start by installing that.
 
 Sarah
 
 On Fri, May 6, 2016 at 12:35 PM, Phillips,Douglas A 
 wrote:
> 
> Hi, I just downloaded the Agricolae package and tried to access it using
> the commands listed below (and received the error messages in red).  Any
> suggestions on resolving these errors?
> 
> Thanks for your assistance.
> 
> Doug
> 
> 
> 
>> install.packages("agricolae")
> 
>  % Total% Received % Xferd  Average Speed   TimeTime Time
> Current
> Dload
> Upload   Total   SpentLeft  Speed
>  0 00 00 0  0  0 --:--:-- --:--:-- --:--:--
> 0  0 00 00 0  0  0 --:--:-- --:--:-- --:--:--
> 0100  896k  100  896k0 0   900k  0 --:--:-- --:--:-- --:--:--
> 900k
> 
> The downloaded binary packages are in
> 
> /var/folders/qn/8tc0v1m971d361gv0mwsmxj8gn/T//RtmpYMZ97k/downloaded_packages
>> 
>> 
>> 
>> library(agricolae)
> 
> Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck =
> vI[[i]]) :
>  there is no package called ‘sp’
> Error: package or namespace load failed for ‘agricolae’
> 
>[[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.
__
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 needed with successfully downloading and opening Agricolae package

2016-05-06 Thread David Winsemius

> On May 6, 2016, at 1:41 PM, Sarah Goslee  wrote:
> 
> On Fri, May 6, 2016 at 4:33 PM, Jeff Newmiller  
> wrote:
>> I am puzzled why the original install.packages call did not also download
>> the sp package, since the default argument dependencies = NA should have
>> triggered installation of imports including spDep, which should in turn have
>> installed the dependencies including the sp package. Anyone have a theory?

I think you need to set dependencies=TRUE to make the checks recursive.

-- 
David.

> 
> Any hypothesis would require more information, sessionInfo() at a very 
> minimum.
> 
> 
>> --
>> Sent from my phone. Please excuse my brevity.
>> 
>> On May 6, 2016 12:47:44 PM PDT, Sarah Goslee  wrote:
>>> 
>>> This is a plain-text email list, so your red doesn't show up, but
>>> since the error message said that the installer couldn't find the sp
>>> package, I'd start by installing that.
>>> 
>>> Sarah
>>> 
>>> On Fri, May 6, 2016 at 12:35 PM, Phillips,Douglas A 
>>> wrote:
 
 Hi, I just downloaded the Agricolae package and tried to access it using
 the commands listed below (and received the error messages in red).  Any
 suggestions on resolving these errors?
 
 Thanks for your assistance.
 
 Doug
 
 
 
> install.packages("agricolae")
 
   % Total% Received % Xferd  Average Speed   TimeTime Time
 Current
  Dload
 Upload   Total   SpentLeft  Speed
   0 00 00 0  0  0 --:--:-- --:--:-- --:--:--
 0  0 00 00 0  0  0 --:--:-- --:--:-- --:--:--
 0100  896k  100  896k0 0   900k  0 --:--:-- --:--:-- --:--:--
 900k
 
 The downloaded binary packages are in
 
 /var/folders/qn/8tc0v1m971d361gv0mwsmxj8gn/T//RtmpYMZ97k/downloaded_packages
> 
> 
> 
> library(agricolae)
 
 Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck =
 vI[[i]]) :
   there is no package called ‘sp’
 Error: package or namespace load failed for ‘agricolae’
 
 [[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] find high correlated variables in a big matrix

2016-05-06 Thread David Winsemius

> On May 6, 2016, at 2:12 PM, Lida Zeighami  wrote:
> 
> Hi there,
> 
> Is there any way to find out high correlated variables among a big matrix?
> for example I have a matrix called data= 2000*5000 and I need to find the
> high correlated variables between the variables in the columns! (Need 100
> high correlated variables from 5000 variables in column)
> 
> I could calculate the correlation matrix and pick the high correlated ones
> but my problem is, I just can pick pairs of variables with high correlation
> and may be we have low correlation across the pairs! Means, in my 100*100
> correlation matrix, there are some pairs with low correlation and I
> couldn't find the 100 variables which they all have high correlation
> together!!!
> Would you please ley me know if there is any way?

The rcorr function in Hmisc will return a list whose first element is a 
correlation matrix

> base <- rnorm(100)

> test <- matrix(base+0.2*rnorm(300), 100)

> rcorr(test)[[1]]
  [,1]  [,2]  [,3]
[1,] 1.000 0.9631220 0.9721688
[2,] 0.9631220 1.000 0.9666564
[3,] 0.9721688 0.9666564 1.000

You can use which to to find the locations meeting a criterion (or two):

> mycorr <- .Last.value

> which(mycorr > 0.97 & mycorr != 1, arr.ind=TRUE)
 row col
[1,]   3   1
[2,]   1   3



-- 

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] find high correlated variables in a big matrix

2016-05-06 Thread Clint Bowman
Are you rying to find clusters of variables according to some distance 
metric?


Clint BowmanINTERNET:   cl...@ecy.wa.gov
Air Quality Modeler INTERNET:   cl...@math.utah.edu
Department of Ecology   VOICE:  (360) 407-6815
PO Box 47600FAX:(360) 407-7534
Olympia, WA 98504-7600

USPS:   PO Box 47600, Olympia, WA 98504-7600
Parcels:300 Desmond Drive, Lacey, WA 98503-1274

On Fri, 6 May 2016, Lida Zeighami wrote:


Hi there,

Is there any way to find out high correlated variables among a big matrix?
for example I have a matrix called data= 2000*5000 and I need to find the
high correlated variables between the variables in the columns! (Need 100
high correlated variables from 5000 variables in column)

I could calculate the correlation matrix and pick the high correlated ones
but my problem is, I just can pick pairs of variables with high correlation
and may be we have low correlation across the pairs! Means, in my 100*100
correlation matrix, there are some pairs with low correlation and I
couldn't find the 100 variables which they all have high correlation
together!!!
Would you please ley me know if there is any way?

Thanks

[[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] find high correlated variables in a big matrix

2016-05-06 Thread Lida Zeighami
Hi there,

Is there any way to find out high correlated variables among a big matrix?
for example I have a matrix called data= 2000*5000 and I need to find the
high correlated variables between the variables in the columns! (Need 100
high correlated variables from 5000 variables in column)

I could calculate the correlation matrix and pick the high correlated ones
but my problem is, I just can pick pairs of variables with high correlation
and may be we have low correlation across the pairs! Means, in my 100*100
correlation matrix, there are some pairs with low correlation and I
couldn't find the 100 variables which they all have high correlation
together!!!
Would you please ley me know if there is any way?

Thanks

[[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] Help needed with successfully downloading and opening Agricolae package

2016-05-06 Thread Sarah Goslee
On Fri, May 6, 2016 at 4:33 PM, Jeff Newmiller  wrote:
> I am puzzled why the original install.packages call did not also download
> the sp package, since the default argument dependencies = NA should have
> triggered installation of imports including spDep, which should in turn have
> installed the dependencies including the sp package. Anyone have a theory?

Any hypothesis would require more information, sessionInfo() at a very minimum.


> --
> Sent from my phone. Please excuse my brevity.
>
> On May 6, 2016 12:47:44 PM PDT, Sarah Goslee  wrote:
>>
>> This is a plain-text email list, so your red doesn't show up, but
>> since the error message said that the installer couldn't find the sp
>> package, I'd start by installing that.
>>
>> Sarah
>>
>> On Fri, May 6, 2016 at 12:35 PM, Phillips,Douglas A 
>> wrote:
>>>
>>>  Hi, I just downloaded the Agricolae package and tried to access it using
>>> the commands listed below (and received the error messages in red).  Any
>>> suggestions on resolving these errors?
>>>
>>>  Thanks for your assistance.
>>>
>>>  Doug
>>>
>>>
>>>
  install.packages("agricolae")
>>>
>>>% Total% Received % Xferd  Average Speed   TimeTime Time
>>> Current
>>>   Dload
>>>  Upload   Total   SpentLeft  Speed
>>>0 00 00 0  0  0 --:--:-- --:--:-- --:--:--
>>> 0  0 00 00 0  0  0 --:--:-- --:--:-- --:--:--
>>> 0100  896k  100  896k0 0   900k  0 --:--:-- --:--:-- --:--:--
>>> 900k
>>>
>>>  The downloaded binary packages are in
>>>
>>> /var/folders/qn/8tc0v1m971d361gv0mwsmxj8gn/T//RtmpYMZ97k/downloaded_packages



  library(agricolae)
>>>
>>>  Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck =
>>> vI[[i]]) :
>>>there is no package called ‘sp’
>>>  Error: package or namespace load failed for ‘agricolae’
>>>
>>>  [[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] Help needed with successfully downloading and opening Agricolae package

2016-05-06 Thread Jeff Newmiller
I am puzzled why the original install.packages call did not also download the 
sp package, since the default argument dependencies = NA should have triggered 
installation of imports including spDep, which should in turn have installed 
the dependencies including the sp package. Anyone have a theory? 
-- 
Sent from my phone. Please excuse my brevity.

On May 6, 2016 12:47:44 PM PDT, Sarah Goslee  wrote:
>This is a plain-text email list, so your red doesn't show up, but
>since the error message said that the installer couldn't find the sp
>package, I'd start by installing that.
>
>Sarah
>
>On Fri, May 6, 2016 at 12:35 PM, Phillips,Douglas A 
>wrote:
>> Hi, I just downloaded the Agricolae package and tried to access it
>using the commands listed below (and received the error messages in
>red).  Any suggestions on resolving these errors?
>>
>> Thanks for your assistance.
>>
>> Doug
>>
>>
>>
>>> install.packages("agricolae")
>>   % Total% Received % Xferd  Average Speed   TimeTime
>Time  Current
>>  Dload  Upload   Total   Spent   
>Left  Speed
>>   0 00 00 0  0  0 --:--:-- --:--:--
>--:--:-- 0  0 00 00 0  0  0 --:--:--
>--:--:-- --:--:-- 0100  896k  100  896k0 0   900k  0
>--:--:-- --:--:-- --:--:--  900k
>>
>> The downloaded binary packages are in
>>
>/var/folders/qn/8tc0v1m971d361gv0mwsmxj8gn/T//RtmpYMZ97k/downloaded_packages
>>>
>>>
>>> library(agricolae)
>> Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck =
>vI[[i]]) :
>>   there is no package called ‘sp’
>> Error: package or namespace load failed for ‘agricolae’
>>
>> [[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] Help needed with successfully downloading and opening Agricolae package

2016-05-06 Thread Sarah Goslee
This is a plain-text email list, so your red doesn't show up, but
since the error message said that the installer couldn't find the sp
package, I'd start by installing that.

Sarah

On Fri, May 6, 2016 at 12:35 PM, Phillips,Douglas A  wrote:
> Hi, I just downloaded the Agricolae package and tried to access it using the 
> commands listed below (and received the error messages in red).  Any 
> suggestions on resolving these errors?
>
> Thanks for your assistance.
>
> Doug
>
>
>
>> install.packages("agricolae")
>   % Total% Received % Xferd  Average Speed   TimeTime Time  
> Current
>  Dload  Upload   Total   SpentLeft  Speed
>   0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 
> 0  0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 
> 0100  896k  100  896k0 0   900k  0 --:--:-- --:--:-- --:--:--  
> 900k
>
> The downloaded binary packages are in
> /var/folders/qn/8tc0v1m971d361gv0mwsmxj8gn/T//RtmpYMZ97k/downloaded_packages
>>
>>
>> library(agricolae)
> Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
>   there is no package called ‘sp’
> Error: package or namespace load failed for ‘agricolae’
>
> [[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] with vs. attach

2016-05-06 Thread David Winsemius

> On May 6, 2016, at 5:47 AM, Spencer Graves 
>  wrote:
> 
> 
> 
> On 5/6/2016 6:46 AM, peter dalgaard wrote:
>> On 06 May 2016, at 02:43 , David Winsemius  wrote:
>> 
 On May 5, 2016, at 5:12 PM, Spencer Graves 
  wrote:
 
 I want a function to evaluate one argument
 in the environment of a data.frame supplied
 as another argument.  "attach" works for
 this, but "with" does not.  Is there a way
 to make "with" work?  I'd rather not attach
 the data.frame.
 
 
 With the following two functions "eval.w.attach"
 works but "eval.w.with" fails:
 
 
 dat <- data.frame(a=1:2)
 eval.w.attach <- function(x, dat){
  attach(dat)
  X <- x
  detach()
  X
 }
 
 eval.w.with <- function(x, dat){
  with(dat, x)
 }
 
 eval.w.attach(a/2, dat) # returns c(.5, 1)
>>> How about using eval( substitute( ...))?
>>> 
>>> eval.w.sub <- function(expr, datt){
>>>   eval( substitute(expr), env=datt)
>>> }
>>> eval.w.sub(a/2, dat)
>>> #[1] 0.5 1.0
>>> 
>>> 
>> Actually, I think a better overall strategy is to say that if you want to 
>> pass an expression to a function, then pass an expression object (or a call 
>> object or maybe a formula object).
>> 
>> Once you figure out _how_ your eval.w.attach works (sort of), you'll get the 
>> creeps:
>> 
>> Lazy evaluation causes the argument x to be evaluated after the attach(), 
>> hence the evaluation environment of an actual argument is being temporarily 
>> modified from inside a function.
>> 
>> Apart from upsetting computer science purists, there could be hidden 
>> problems: One major issue is that  values in "dat" could be masked by values 
>> in the global environment, another issue is that an error in evaluating the 
>> expression will leave dat attached. So at a minimum, you need to recode 
>> using on.exit() magic.
>> 
>> So my preferences go along these lines:
>> 
>>> dat <- data.frame(a=1:2)
>>> eval.expression <- function(e, dat) eval(e, dat)
>>> eval.expression(quote(a/2), dat)
>> [1] 0.5 1.0
>>> eval.expression(expression(a/2), dat)
>> [1] 0.5 1.0
>> 
>>> eval.formula <- function(f, dat) eval(f[[2]], dat)
>>> eval.formula(~a/2, dat)
>> [1] 0.5 1.0
> 
> Hi, Peter:
> 
> 
>  I don't like eval.expression or eval.formula, because they don't 
> automatically accept what I naively thought should work and require more 
> knowledge of the user.  What about David's eval.w.sub:
> 
> 
> a <- pi
> dat <- data.frame(a=1:2)
> eval.w.sub <- function(a, Dat){
>  eval( substitute(a), env=Dat)
> }
> > eval.w.sub(a/2, dat)
> [1] 0.5 1.0

I liked eval.expression and tested it with a bquote(...) argument to see if 
that would succeed. It did, but it didn't return what you wanted for `a/2`, so 
I tried seeing if a "double eval wuold deliver both yours and my desired 
results:

 eval.w.sub <- function(a, Dat){
  eval( eval(substitute(a),Dat), env=Dat)
 }
x=2
 eval.w.sub( a/2, dat)
[1] 0.5 1.0
 eval.w.sub( bquote(2*a*.(x) ), dat)
[1] 4 8

We are here retracing the path the Hadley took in some of his ggplot2 design 
decsions. Unfortunately for me those NSE rules often left me confused about 
what should and shouldn't be 'quoted' in the as-character sense and what should 
be quote()-ed or "unquoted" in the bquote() sense.
-- 

> 
> 
> 
>  This produces what's desired in a way that seems simpler to me.
> 
> 
>  By the way, I really appreciate Peter's insightful comments:
> 
> 
> eval.w.attachOops <- function(x, Dat){
>  attach(Dat)
>  X <- x
>  detach()
>  X
> }
> > eval.w.attachOops(a/2, dat)
> The following object is masked _by_ .GlobalEnv:
> 
>a
> 
> [1] 1.570796
> > eval.w.attachOops(b/2, dat)
> The following object is masked _by_ .GlobalEnv:
> 
>a
> 
> Error in eval.w.attachOops(b/2, dat) : object 'b' not found
> > search()
> [1] ".GlobalEnv""Dat"   "package:graphics"
> [4] "package:grDevices" "package:utils" "package:datasets"
> [7] "package:methods"   "Autoloads" "package:base"
> > objects(2)
> [1] "a"
> 
> *** NOTES:
> 
> 
>  1.  This gives a likely wrong answer with a warning if "a" exists in 
> .GlobalEnv, and leaves "Dat" (NOT "dat") attached upon exit.
> 
> 
> 
>  2.  A stray "detach()" [not shown here] detached "package:stats".  oops.
> 
> 
> *** Using "on.exit" fixes the problem with failure to detach but not the 
> likely wrong answer:
> 
> 
> detach()
> search()
> eval.w.attachStillWrong <- function(x, dat){
>  attach(dat)
>  on.exit(detach(dat))
>  X <- x
>  X
> }
> The following object is masked _by_ .GlobalEnv:
> 
>a
> 
> [1] 1.570796
> > eval.w.attachStillWrong(b/2, dat)
> The following object is masked _by_ .GlobalEnv:
> 
>a
> 
> Error in eval.w.attachStillWrong(b/2, dat) : object 'b' not found
> > search()
> [1] ".GlobalEnv""package:grDevices" "package:utils"
> [4] "package:datasets"  "package:methods"   "Autoloads"
> [7] "package:base"
> 
> 
>  Thanks again to Peter 

[R] Help needed with successfully downloading and opening Agricolae package

2016-05-06 Thread Phillips,Douglas A
Hi, I just downloaded the Agricolae package and tried to access it using the 
commands listed below (and received the error messages in red).  Any 
suggestions on resolving these errors?

Thanks for your assistance.

Doug



> install.packages("agricolae")
  % Total% Received % Xferd  Average Speed   TimeTime Time  Current
 Dload  Upload   Total   SpentLeft  Speed
  0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 0  
0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 0100 
 896k  100  896k0 0   900k  0 --:--:-- --:--:-- --:--:--  900k

The downloaded binary packages are in
/var/folders/qn/8tc0v1m971d361gv0mwsmxj8gn/T//RtmpYMZ97k/downloaded_packages
>
>
> library(agricolae)
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
  there is no package called ‘sp’
Error: package or namespace load failed for ‘agricolae’

[[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] Freq table

2016-05-06 Thread Bert Gunter
This is not a code writing service. Posters are expected to first make
an honest effort and show us their code as part of their post. Please
read the posting guide to learn what is expected.

However, a hint to get you started: see ?cut

If you have not already gone through an R tutorial or two, please do
so before posting further. There are many good ones on the web.

Cheers,
Bert Gunter
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 Fri, May 6, 2016 at 9:40 AM, Partha Sinha  wrote:
> M1 M2 M4
> 60 86 48
> 72 90 86
> 66 86 62
> 69 60 48
> 66 86 58
> I  want to frequency table by binning the data in 0-60, 61-80,80-100
> and want output as
>M1   M2   M3
> 0-60
> 61-80
> 80-100
>
> How to do
>
> [[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] Freq table

2016-05-06 Thread Partha Sinha
M1 M2 M4
60 86 48
72 90 86
66 86 62
69 60 48
66 86 58
I  want to frequency table by binning the data in 0-60, 61-80,80-100
and want output as
   M1   M2   M3
0-60
61-80
80-100

How to do

[[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] RandomForest-question about split point and GetTree function

2016-05-06 Thread Adam Sanders
I am looking at results of a random forest. In the documentation, it says
the following for categorical variables:

"For categorical predictors, the splitting point is represented by an
integer, whose binary expansion gives the identities of the categories that
goes to left or right. For example, if a predictor has four categories, and
the split point is 13. The binary expansion of 13 is (1, 0, 1, 1) (because
13 = 1 ∗ 2 0 + 0 ∗ 2 1 + 1 ∗ 2 2 + 1 ∗ 2 3 ), so cases with categories 1,
3, or 4 in this predictor get sent to the left, and the rest to the right. "

I am unsure how to interpret this when the splitting point is 0. I was
thinking it means all categories would be to the right. Is this correct?

[[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] openssl package install error

2016-05-06 Thread Charles Determan
I am trying to install 'openssl' on ubuntu 14.04.  I already of libssl-dev
and libcurl4-openssl-dev installed.  But when I try to install I get a
bunch of errors complaining about 'unknown type 'u_char''.

Thoughts?

Excerpt of output:

Found pkg-config cflags and libs!
Using PKG_CFLAGS=
Using PKG_LIBS=-lssl -lcrypto
** libs
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
aes.c -o aes.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
base64.c -o base64.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
bignum.c -o bignum.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
cert.c -o cert.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
diffie.c -o diffie.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
envelope.c -o envelope.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
error.c -o error.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
hash.c -o hash.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
info.c -o info.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
keygen.c -o keygen.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
onload.c -o onload.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
openssh.c -o openssh.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
rand.c -o rand.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
read.c -o read.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
rsa.c -o rsa.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
signing.c -o signing.o
ccache gcc-4.8 -I/usr/share/R/include -DNDEBUG  -fpic  -std=c99 -c
ssl.c -o ssl.o
In file included from /usr/include/resolv.h:65:0,
 from ssl.c:15:
/usr/include/arpa/nameser.h:115:2: error: unknown type name ‘u_char’
  const u_char *_msg, *_eom;
  ^
/usr/include/arpa/nameser.h:117:2: error: unknown type name ‘u_char’
  const u_char *_sections[ns_s_max];
  ^
/usr/include/arpa/nameser.h:120:2: error: unknown type name ‘u_char’
  const u_char *_msg_ptr;

[[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] bootstrapping code with ordered categorical data(five categories)

2016-05-06 Thread thanoon younis
Hi

I need a bootstrapping code with ordered categorical data(five categories)
to re-samplling a real data with 16 variables and 200 sample size.


Any help please




-- 
Thanoon Y. Thanoon
PhD
Department of Mathematical Sciences
Faculty of Science
University Technology Malaysia, UTM
E.Mail: thanoon.youni...@gmail.com
E.Mail: dawn_praye...@yahoo.com
Facebook:Thanoon Younis AL-Shakerchy
Twitter: Thanoon Alshakerchy
H.P:00601127550205

[[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] with vs. attach

2016-05-06 Thread Spencer Graves



On 5/6/2016 6:46 AM, peter dalgaard wrote:

On 06 May 2016, at 02:43 , David Winsemius  wrote:


On May 5, 2016, at 5:12 PM, Spencer Graves 
 wrote:

I want a function to evaluate one argument
in the environment of a data.frame supplied
as another argument.  "attach" works for
this, but "with" does not.  Is there a way
to make "with" work?  I'd rather not attach
the data.frame.


With the following two functions "eval.w.attach"
works but "eval.w.with" fails:


dat <- data.frame(a=1:2)
eval.w.attach <- function(x, dat){
  attach(dat)
  X <- x
  detach()
  X
}

eval.w.with <- function(x, dat){
  with(dat, x)
}

eval.w.attach(a/2, dat) # returns c(.5, 1)

How about using eval( substitute( ...))?

eval.w.sub <- function(expr, datt){
   eval( substitute(expr), env=datt)
 }
eval.w.sub(a/2, dat)
#[1] 0.5 1.0



Actually, I think a better overall strategy is to say that if you want to pass 
an expression to a function, then pass an expression object (or a call object 
or maybe a formula object).

Once you figure out _how_ your eval.w.attach works (sort of), you'll get the 
creeps:

Lazy evaluation causes the argument x to be evaluated after the attach(), hence 
the evaluation environment of an actual argument is being temporarily modified 
from inside a function.

Apart from upsetting computer science purists, there could be hidden problems: One major 
issue is that  values in "dat" could be masked by values in the global 
environment, another issue is that an error in evaluating the expression will leave dat 
attached. So at a minimum, you need to recode using on.exit() magic.

So my preferences go along these lines:


dat <- data.frame(a=1:2)
eval.expression <- function(e, dat) eval(e, dat)
eval.expression(quote(a/2), dat)

[1] 0.5 1.0

eval.expression(expression(a/2), dat)

[1] 0.5 1.0


eval.formula <- function(f, dat) eval(f[[2]], dat)
eval.formula(~a/2, dat)

[1] 0.5 1.0


Hi, Peter:


  I don't like eval.expression or eval.formula, because they don't 
automatically accept what I naively thought should work and require more 
knowledge of the user.  What about David's eval.w.sub:



a <- pi
dat <- data.frame(a=1:2)
eval.w.sub <- function(a, Dat){
  eval( substitute(a), env=Dat)
}
> eval.w.sub(a/2, dat)
[1] 0.5 1.0


  This produces what's desired in a way that seems simpler to me.


  By the way, I really appreciate Peter's insightful comments:


eval.w.attachOops <- function(x, Dat){
  attach(Dat)
  X <- x
  detach()
  X
}
> eval.w.attachOops(a/2, dat)
The following object is masked _by_ .GlobalEnv:

a

[1] 1.570796
> eval.w.attachOops(b/2, dat)
The following object is masked _by_ .GlobalEnv:

a

Error in eval.w.attachOops(b/2, dat) : object 'b' not found
> search()
[1] ".GlobalEnv""Dat"   "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods"   "Autoloads" "package:base"
> objects(2)
[1] "a"

*** NOTES:


  1.  This gives a likely wrong answer with a warning if "a" exists 
in .GlobalEnv, and leaves "Dat" (NOT "dat") attached upon exit.




  2.  A stray "detach()" [not shown here] detached 
"package:stats".  oops.



*** Using "on.exit" fixes the problem with failure to detach but not the 
likely wrong answer:



detach()
search()
eval.w.attachStillWrong <- function(x, dat){
  attach(dat)
  on.exit(detach(dat))
  X <- x
  X
}
The following object is masked _by_ .GlobalEnv:

a

[1] 1.570796
> eval.w.attachStillWrong(b/2, dat)
The following object is masked _by_ .GlobalEnv:

a

Error in eval.w.attachStillWrong(b/2, dat) : object 'b' not found
> search()
[1] ".GlobalEnv""package:grDevices" "package:utils"
[4] "package:datasets"  "package:methods"   "Autoloads"
[7] "package:base"


  Thanks again to Peter and David.  Spencer


Peter D.




--
David.



eval.w.with(a/2, dat) # Error ... 'a' not found


Thanks, Spencer Graves

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


__
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] svyciprop object

2016-05-06 Thread Leonardo Ferreira Fontenelle
Em Sex 6 mai. 2016, às 06:20, kende jan via R-help escreveu:
> Hi, I'd like to access to the different elements in a svyciprop object
> (to the confidence intervals in particular...). But none of the functions
> I know works.Thank you for your help !

I don't know what data set you are using, so for reproducibility I'm
using the data set from the example in the function documentation.

=
library(survey)
data(api)
dclus1 <- svydesign(ids = ~ dnum, fpc = ~ fpc, data = apiclus1)
grr <- svyciprop(~ I(ell == 0), dclus1, method = "likelihood")
attr(grr, "ci")
# 2.5%97.5% 
# 0.0006639212 0.1077784084
=

HTH,

Leonardo Ferreira Fontenelle
PhD candidate in epidemiology, Federal University of Pelotas
Professor of medicine, Vila Velha University
Legislative analyst in health, Municipal Chamber of Vitória

__
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] svyciprop object

2016-05-06 Thread Jeff Newmiller
Your post in HTML format came through garbled.  Please post plain text email on 
this list. 

Your question is unclear as well.  Perhaps you are not familiar with using the 
str() function preview what is in an object? 
-- 
Sent from my phone. Please excuse my brevity.

On May 6, 2016 2:20:00 AM PDT, kende jan via R-help  
wrote:
>Hi, I'd like to access to the different elements in a svyciprop object
>(to the confidence intervals in particular...). But none of the
>functions I know works.Thank you for your help !
>> grr <- svyciprop(~temp==bzz, dclus1)> grr                           
>   2.5%   97.5%temp == bzz 0.040719697 0.027622756 0.05965>
>attributes(grr)$names[1] "temp == bzz"
>$var                        as.numeric(temp == bzz)as.numeric(temp ==
>bzz)       6.42377038236e-05
>$ci           2.5%           97.5% 0.0276227559667 0.0596454643748 
>$class[1] "svyciprop"
>> grr$ciErreur dans grr$ci : $ operator is invalid for atomic vectors>
>grr["ci"]   NA > ci(grr)Erreur : impossible de trouver la fonction
>"ci"
>
>
>   [[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] with vs. attach

2016-05-06 Thread peter dalgaard

On 06 May 2016, at 02:43 , David Winsemius  wrote:

> 
>> On May 5, 2016, at 5:12 PM, Spencer Graves 
>>  wrote:
>> 
>> I want a function to evaluate one argument
>> in the environment of a data.frame supplied
>> as another argument.  "attach" works for
>> this, but "with" does not.  Is there a way
>> to make "with" work?  I'd rather not attach
>> the data.frame.
>> 
>> 
>> With the following two functions "eval.w.attach"
>> works but "eval.w.with" fails:
>> 
>> 
>> dat <- data.frame(a=1:2)
>> eval.w.attach <- function(x, dat){
>>  attach(dat)
>>  X <- x
>>  detach()
>>  X
>> }
>> 
>> eval.w.with <- function(x, dat){
>>  with(dat, x)
>> }
>> 
>> eval.w.attach(a/2, dat) # returns c(.5, 1)
> 
> How about using eval( substitute( ...))?
> 
> eval.w.sub <- function(expr, datt){
>   eval( substitute(expr), env=datt)
> }
> eval.w.sub(a/2, dat)
> #[1] 0.5 1.0
> 
> 

Actually, I think a better overall strategy is to say that if you want to pass 
an expression to a function, then pass an expression object (or a call object 
or maybe a formula object). 

Once you figure out _how_ your eval.w.attach works (sort of), you'll get the 
creeps: 

Lazy evaluation causes the argument x to be evaluated after the attach(), hence 
the evaluation environment of an actual argument is being temporarily modified 
from inside a function. 

Apart from upsetting computer science purists, there could be hidden problems: 
One major issue is that  values in "dat" could be masked by values in the 
global environment, another issue is that an error in evaluating the expression 
will leave dat attached. So at a minimum, you need to recode using on.exit() 
magic.

So my preferences go along these lines:

> dat <- data.frame(a=1:2)
> eval.expression <- function(e, dat) eval(e, dat)
> eval.expression(quote(a/2), dat)
[1] 0.5 1.0
> eval.expression(expression(a/2), dat)
[1] 0.5 1.0

> eval.formula <- function(f, dat) eval(f[[2]], dat)
> eval.formula(~a/2, dat)
[1] 0.5 1.0

Peter D.



> -- 
> David.
> 
> 
>> 
>> eval.w.with(a/2, dat) # Error ... 'a' not found
>> 
>> 
>> Thanks, Spencer Graves
>> 
>>  [[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.

-- 
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  Priv: pda...@gmail.com

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


[R] Truncreg package help

2016-05-06 Thread Philipp Schaper
Dear R userers,

I am running truncated regressions with the 'truncreg' package. My sample 
is large (6,000 observations), the data is left-truncated at 1 and the 
left tail of the data is heavily centered at 1. When I am running the 
regression I receive the following error message: 

  Error in optim(par = start[!fixed], fn = logLikFunc, control = control, 
: 
  initial value in 'vmmin' is not finite

>From a previous discussion (
http://r.789695.n4.nabble.com/betareg-help-td3350129.html) on a similar 
issue in the betareg function I assume that the error message stems from 
that the estimate of the starting value of the precision parameter is 
negative. However, I do not know how I can take care of this. Thus I would 
be very thankful for any help.

Best regards,
Philipp 
[[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] svyciprop object

2016-05-06 Thread kende jan via R-help
Hi, I'd like to access to the different elements in a svyciprop object (to the 
confidence intervals in particular...). But none of the functions I know 
works.Thank you for your help !
> grr <- svyciprop(~temp==bzz, dclus1)> grr                               2.5%  
>  97.5%temp == bzz 0.040719697 0.027622756 0.05965> attributes(grr)$names[1] 
> "temp == bzz"
$var                        as.numeric(temp == bzz)as.numeric(temp == bzz)      
 6.42377038236e-05
$ci           2.5%           97.5% 0.0276227559667 0.0596454643748 
$class[1] "svyciprop"
> grr$ciErreur dans grr$ci : $ operator is invalid for atomic vectors> 
> grr["ci"]   NA > ci(grr)Erreur : impossible de trouver la fonction "ci"


[[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] with vs. attach

2016-05-06 Thread Spencer Graves



On 5/5/2016 11:17 PM, Bert Gunter wrote:

... and it's exactly with.default's code !



Thanks for pointing that out.  Unfortunately, it didn't work inside 
another function.  However, if I had looked at it, I might have been 
able to thought to try it.  Spencer


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, May 5, 2016 at 7:38 PM, Spencer Graves
 wrote:

Hi, David:  That works.  Thanks very much.  Spencer Graves



On 5/5/2016 7:43 PM, David Winsemius wrote:

On May 5, 2016, at 5:12 PM, Spencer Graves
 wrote:

I want a function to evaluate one argument
in the environment of a data.frame supplied
as another argument.  "attach" works for
this, but "with" does not.  Is there a way
to make "with" work?  I'd rather not attach
the data.frame.


With the following two functions "eval.w.attach"
works but "eval.w.with" fails:


dat <- data.frame(a=1:2)
eval.w.attach <- function(x, dat){
attach(dat)
X <- x
detach()
X
}

eval.w.with <- function(x, dat){
with(dat, x)
}

eval.w.attach(a/2, dat) # returns c(.5, 1)

How about using eval( substitute( ...))?

   eval.w.sub <- function(expr, datt){
 eval( substitute(expr), env=datt)
   }
   eval.w.sub(a/2, dat)
#[1] 0.5 1.0



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