Re: [R] One Dimensional Monte Carlo Simulation

2017-07-14 Thread HUL-Anthony Egerton
Further to my email below, I have just realised that I forgot to include the 
specification of L and R.

Hence, the code needs to include the following additional lines at the start;-

L<-7.5e6
R<-2.5e6

Apologies for any confusion caused!

Best regards,

Tony


> On 12 Jul 2017, at 10:03 AM, HUL-Anthony Egerton 
>  wrote:
> 
> I am trying to code a basic Monte Carlo Simulation in R where a Poisson 
> distribution generates a frequency output that is then input into a Lognormal 
> distribution, which produces a different, independent severity for each 
> incidence. The individual incidences are then summed to produce an aggregate 
> amount per period/iteration. 
> 
> Here is the code;-
> 
> scale<-15.08707 
> shape<-0.8592507 
> lambda.risk<-1.75 
> L<-7.5e5 
> R<-2.5e6 
> iterations<-replicate(1,{ 
>  claims1<-0 
>  claims2<-0 
>  claims3<-0 
>  claims4<-0 
>  claims5<-0 
> claims6<-0 
>  claims7<-0 
>  claims<-c(claims1,claims2,claims3,claims4,claims5,claims6,claims7)
>  freq<-(qpois(runif(1),lambda.risk)) 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if(freq>=1){claims1<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if(freq>=2){claims2<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=3) {claims3<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=4) {claims4<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=5) {claims5<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=6) {claims6<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=7) {claims7<-sev} 
>  claims<-c(claims1,claims2,claims3,claims4,claims5,claims6,claims7) 
>  min(22.5e6,sum(claims)) 
> })
> 
> I am new to R, but am sure that there must be a simpler way to code this 
> process. 
> 
> Furthermore, as the Poisson lambda increases, there is a need to include 
> provision for potentially more incidences, which will require a manual 
> expansion of the code to claims8, claims9 etc.
> 
> Can you assist, please?
> 
> Best regards,
> 
> Anthony A H Egerton MA MBA ACII
> ��
> HUNTINGTON UNDERWRITING LIMITED
> Labuan FT, Malaysia
> 
> 
> 
> CONFIDENTIALITY NOTE: 
>  
> The information contained in this email message may be legally privileged and 
> contain confidential information and is intended only for the use of the 
> individual or entity to whom it is addressed. If the reader of this message 
> is not the intended recipient, you are hereby notified that any 
> dissemination, distribution or copy of this message is strictly prohibited. 
> If you have received this email in error, please immediately delete this 
> message.
> 
> Begin forwarded message:
> 
>> From: r-help-requ...@r-project.org
>> Date: 8 July 2017 at 6:00:02 PM SGT
>> To: r-help@r-project.org
>> Subject: R-help Digest, Vol 173, Issue 8
>> Reply-To: r-help@r-project.org
>> 
>> Send R-help mailing list submissions to
>>r-help@r-project.org
>> 
>> To subscribe or unsubscribe via the World Wide Web, visit
>>https://stat.ethz.ch/mailman/listinfo/r-help
>> or, via email, send a message with subject or body 'help' to
>>r-help-requ...@r-project.org
>> 
>> You can reach the person managing the list at
>>r-help-ow...@r-project.org
>> 
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of R-help digest..."
>> 
>> 
>> Today's Topics:
>> 
>>   1. R loop function in Tableau (Dai, Shengyu)
>>   2. Factor vs character in a data.frame vs vector (John Kane)
>>   3. Re: Factor vs character in a data.frame vs vector (Marc Schwartz)
>>   4. Re: Scoring and Ranking Methods (David Winsemius)
>>   5. Re: R loop function in Tableau (David Winsemius)
>>   6. Re: [Rd] italic font on cairo devices in R 3.4 (frede...@ofb.net)
>>   7. Re: [Rd] italic font on cairo devices in R 3.4 (frede...@ofb.net)
>>   8. Beginner s quwry about cfa in lavaan (Martina Hule?ov?)
>>   9. Re: [Rd] italic font on cairo devices in R 3.4 (Ilia Kats)
>>  10. Re: Beginner s quwry about cfa in lavaan (Bert Gunter)
>>  11. Re: Factor vs character in a data.frame vs vector (Marc Schwartz)
>>  12. Re: Factor vs character in a data.frame vs vector (John Kane)
>>  13. Re: [Rd] italic font on cairo devices in R 3.4 (Yixuan Qiu)
>> 
>> 
>> --
>> 
>> Message: 1
>> Date: Thu, 06 Jul 2017 17:19:47 -0400
>> From: "Dai, Shengyu" 
>> To: 
>> Subject: [R] R loop function in Tableau
>> Message-ID: <15947cd3-cd7b-4bb6-a994-d49d514df...@rpi.edu>
>> Content-Type: text/plain;charset="UTF-8"
>> 
>> Hi R helpers,
>> 
>> 
>> 
>> I hope this email finds you well.
>> 
>> 
>> 
>> I am having trouble with R loop function in Tableau. Please see the 
>> attachment of a simple dataset. The problem is to test whether the value of 
>> columns is match.
>> 
>> 
>> 
>> Because Tableau do not have iteration function, I coded ?if statement? in 
>> Tableau to reali

Re: [R] Making 2 dimensional vector from the 3 dimensional one

2017-07-14 Thread Bert Gunter
Like this ?

y <- array(1:24, dim = 2:4)
yy <- array(y, dim = c(6,4))


> y
, , 1

 [,1] [,2] [,3]
[1,]135
[2,]246

, , 2

 [,1] [,2] [,3]
[1,]79   11
[2,]8   10   12

, , 3

 [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

, , 4

 [,1] [,2] [,3]
[1,]   19   21   23
[2,]   20   22   24

> yy
 [,1] [,2] [,3] [,4]
[1,]17   13   19
[2,]28   14   20
[3,]39   15   21
[4,]4   10   16   22
[5,]5   11   17   23
[6,]6   12   18   24


Explanation: An array is simply a vector with a "dim" attribute,
inexed in column major order.  See an Intro to R (ships with R) or any
good R tutorial (e.g. on the web) for details.


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, Jul 14, 2017 at 2:43 AM, Csima, Gabriella
 wrote:
> Hi All,
> I want to make a 1 dimension vector from the first two dimensions of a 3 
> dimension array, so make a 2 dimension vector from a 3-dimension one, with 
> "fusing" (making as.vector) the first two dimensions.
> It seems to be very easy, but I cannot find the solution, I mean it would 
> very strange, that I would do taking the single 1 dimensional vectors from 
> the 3 dimensional one, make one vector from the first two vectors and make 
> two dimension from the original 3rd one and this new one, but  I think there 
> would be an easier way that I do not find.
> Could you help me, please?
> Thanks very much,
> Gabi
>
>
> [[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] Making 2 dimensional vector from the 3 dimensional one

2017-07-14 Thread Jim Lemon
Hi Gabi,
Let's say you have something like this:

gc_array<-array(1:27,c(3,3,3))
> gc_array
, , 1

[,1] [,2] [,3]
[1,]147
[2,]258
[3,]369

, , 2

[,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

, , 3

[,1] [,2] [,3]
[1,]   19   22   25
[2,]   20   23   26
[3,]   21   24   27

You seem to want to collapse this into a two dimensional array using
the values in the three dimensional array. For instance, if you want
to collapse in the third dimension by summing the values in each 2D
"slice":

> gc_array[,,1]+gc_array[,,2]+gc_array[,,3]
[,1] [,2] [,3]
[1,]   30   39   48
[2,]   33   42   51
[3,]   36   45   54

The problem is working out whether the result means what you want. If
the first two dimensions were geographic coordinates and the third
contained a series of measurements over time, then you would have the
aggregate of those measures over time. Is this what you are trying to
do?

Jim

On Fri, Jul 14, 2017 at 7:43 PM, Csima, Gabriella
 wrote:
> Hi All,
> I want to make a 1 dimension vector from the first two dimensions of a 3 
> dimension array, so make a 2 dimension vector from a 3-dimension one, with 
> "fusing" (making as.vector) the first two dimensions.
> It seems to be very easy, but I cannot find the solution, I mean it would 
> very strange, that I would do taking the single 1 dimensional vectors from 
> the 3 dimensional one, make one vector from the first two vectors and make 
> two dimension from the original 3rd one and this new one, but  I think there 
> would be an easier way that I do not find.
> Could you help me, please?
> Thanks very much,
> Gabi
>
>
> [[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] Making 2 dimensional vector from the 3 dimensional one

2017-07-14 Thread Rolf Turner


On 14/07/17 21:43, Csima, Gabriella wrote:


Hi All,
I want to make a 1 dimension vector from the first two dimensions of a 3 dimension array, 
so make a 2 dimension vector from a 3-dimension one, with "fusing" (making 
as.vector) the first two dimensions.
It seems to be very easy, but I cannot find the solution, I mean it would very 
strange, that I would do taking the single 1 dimensional vectors from the 3 
dimensional one, make one vector from the first two vectors and make two 
dimension from the original 3rd one and this new one, but  I think there would 
be an easier way that I do not find.
Could you help me, please?
Thanks very much,


(1) Please avoid posting in HTML (although no great harm was done in 
this instance).  This is a plain text mailing list, and HTML often makes 
a mess.


(2) Spend some time learning R (e.g. read "An Introduction to R" from 
the R web site).  In particular learn about array indexing.


(3) It is not *at all* clear to me what you actually want to do.  A 
simple example of what you've got and what you want to get would help.


(4) What on earth do you mean by "a 1 dimension vector"?  A
1 dimensional vector is a scalar.  I can't believe that this is actually 
what you are talking about.  Please clarify.


cheers,

Rolf Turner

--
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] Conduct Network Analysis

2017-07-14 Thread Rainer Krug
I don’t know anything about network analysis, but I really guess that more 
information is needed to give an answer.

Cheers,

Rainer

> On 14 Jul 2017, at 05:08, SEB140004 Student  wrote:
> 
> Greeting.
> 
> Dear Mr/Mrs/Miss,
> 
> I want to create a network by using R but I only have a table that contain
> OTU ID and the abundance value of two samples ONLY.
> 
> Isn't possible? If can, which package can be used?
> Greatly appreciated to any suggestions and helps.
> 
> Thank you.
> 
> Best regards,
> Kang Chin Yi
> 
>   [[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] Making 2 dimensional vector from the 3 dimensional one

2017-07-14 Thread Csima, Gabriella
Hi All,
I want to make a 1 dimension vector from the first two dimensions of a 3 
dimension array, so make a 2 dimension vector from a 3-dimension one, with 
"fusing" (making as.vector) the first two dimensions.
It seems to be very easy, but I cannot find the solution, I mean it would very 
strange, that I would do taking the single 1 dimensional vectors from the 3 
dimensional one, make one vector from the first two vectors and make two 
dimension from the original 3rd one and this new one, but  I think there would 
be an easier way that I do not find.
Could you help me, please?
Thanks very much,
Gabi


[[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] Efficient Binning

2017-07-14 Thread Jeff Newmiller
?cut

cut( 0.51, boundaries )

You can also use as.integer to convert the resulting factor to an integer. 
-- 
Sent from my phone. Please excuse my brevity.

On July 14, 2017 7:10:59 AM PDT, Dan Abner  wrote:
>Hi all,
>
>I have a situation where I have 16 bins. I generate a random number and
>then want to know which bin number the random number falls in. Right
>now, I
>am using a serious of 16 if() else {} statements which get very
>complicated
>with the embedded curly braces. Is there a more efficient (i.e.,
>easier)
>way to go about this?
>
>
>boundaries<-(0:16)/16
>rand<-runif(1)
>
>Which bin number (1:16) does rand fall in?
>
>Thanks,
>
>Dan
>
>   [[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] Efficient Binning

2017-07-14 Thread Marc Schwartz

> On Jul 14, 2017, at 9:10 AM, Dan Abner  wrote:
> 
> Hi all,
> 
> I have a situation where I have 16 bins. I generate a random number and
> then want to know which bin number the random number falls in. Right now, I
> am using a serious of 16 if() else {} statements which get very complicated
> with the embedded curly braces. Is there a more efficient (i.e., easier)
> way to go about this?
> 
> 
> boundaries<-(0:16)/16
> rand<-runif(1)
> 
> Which bin number (1:16) does rand fall in?
> 
> Thanks,
> 
> Dan


See ?findInterval

> findInterval(rand, boundaries)
[1] 6

There are various function arguments as well, relative to how to deal with the 
interval boundaries, as described on the help page.

Regards,

Marc Schwartz

__
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] Efficient Binning

2017-07-14 Thread Dan Abner
Hi all,

I have a situation where I have 16 bins. I generate a random number and
then want to know which bin number the random number falls in. Right now, I
am using a serious of 16 if() else {} statements which get very complicated
with the embedded curly braces. Is there a more efficient (i.e., easier)
way to go about this?


boundaries<-(0:16)/16
rand<-runif(1)

Which bin number (1:16) does rand fall in?

Thanks,

Dan

[[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] setwd in windows

2017-07-14 Thread Duncan Murdoch

On 14/07/2017 7:42 AM, Troels Ring wrote:

Dear friends - windows R 3.3.3 - sorry to ask a simple question - but I
cannot make setwd work properly in scripts

In the example below I have made a directory in C (firstdir) and a
directory in that (secdir) and the intention is to change directory to
the second from the first - it works when I put the entire path but not
the tilde - what am I missing? I was around rwf 2.14 as per the help to
setwd - to define the HOME - and redefine it. As I read 2.14 R_USER is
used for HOME when set. To no avail.


The tilde is a Unix shell convention, it's not supported by Windows.  R 
emulates it, but you should expect some differences (as there are 
between Unix flavours).


To see what "~/secdir" is interpreted as, call path.expand().

On my system with Windows 10 in a VM:

> path.expand("~/secdir")
[1] "C:\\Users\\murdoch\\Documents/secdir"
> Sys.setenv(R_USER = "C:/firstdir")
> path.expand("~/secdir")
[1] "C:\\Users\\murdoch\\Documents/secdir"

R sets the home directory during startup, it doesn't consult the R_USER 
variable every time it needs it.


Duncan Murdoch




Best wishes

Troels

getwd()
#[1] "C:/firstdir"
  Sys.getenv("R_USER")
#"C:\\Users\\Bruger\\Documents"
dir()
#[1] "chdir.R" "secdir"
setwd("~/secdir")
error in setwd("~/secdir") : cannot change working directory
setwd("C:/firstdir/secdir")
getwd()
#"C:/firstdir/secdir"
setwd("C:/firstdir")
Sys.setenv(R_USER = "C:/firstdir")
Sys.getenv("R_USER")
setwd("~/secdir")


  2.14 What are HOME and working directories?

Several places in the documentation use these terms.

The working directory is the directory from which|Rgui|or|Rterm|was
launched, unless a shortcut was used when it is given by the ‘Start in’
field of the shortcut’s properties. You can find this from R code by the
call|getwd()|.

The home directory is set as follows: If environment variable|R_USER|is
set, its value is used. Otherwise if environment variable|HOME|is set,
its value is used. After those two user-controllable settings, R tries
to find system-defined home directories. It first tries to use the
Windows "personal" directory (typicallyC:\Users\username\Documents). If
that fails, if both environment variables|HOMEDRIVE|and|HOMEPATH|are set
(and they normally are), the value is${HOMEDRIVE}${HOMEPATH}. If all of
these fail, the current working directory is used.




[[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] setwd in windows

2017-07-14 Thread Troels Ring
Fine - no nothing is wrong with setwd("./secdir") but I got the other 
code with the tilde as a supplementum and wondered why I couldn't make 
it work. I believe R_USER was successfully renamed - and per the faq 
thought it should work as specified.


Small problem though

Troels


Den 14-07-2017 kl. 13:54 skrev peter dalgaard:

I am not sure what is going on here. The example seems incomplete. At any rate:

(a) I would be wary of changing my home directory mid-session. That is usually 
just not done.

(b) For a relative directory change, is anything wrong with setwd("./secdir") ?

-pd


On 14 Jul 2017, at 13:42 , Troels Ring  wrote:

Dear friends - windows R 3.3.3 - sorry to ask a simple question - but I
cannot make setwd work properly in scripts

In the example below I have made a directory in C (firstdir) and a
directory in that (secdir) and the intention is to change directory to
the second from the first - it works when I put the entire path but not
the tilde - what am I missing? I was around rwf 2.14 as per the help to
setwd - to define the HOME - and redefine it. As I read 2.14 R_USER is
used for HOME when set. To no avail.

Best wishes

Troels

getwd()
#[1] "C:/firstdir"
  Sys.getenv("R_USER")
#"C:\\Users\\Bruger\\Documents"
dir()
#[1] "chdir.R" "secdir"
setwd("~/secdir")
error in setwd("~/secdir") : cannot change working directory
setwd("C:/firstdir/secdir")
getwd()
#"C:/firstdir/secdir"
setwd("C:/firstdir")
Sys.setenv(R_USER = "C:/firstdir")
Sys.getenv("R_USER")
setwd("~/secdir")


  2.14 What are HOME and working directories?

Several places in the documentation use these terms.

The working directory is the directory from which|Rgui|or|Rterm|was
launched, unless a shortcut was used when it is given by the ‘Start in’
field of the shortcut’s properties. You can find this from R code by the
call|getwd()|.

The home directory is set as follows: If environment variable|R_USER|is
set, its value is used. Otherwise if environment variable|HOME|is set,
its value is used. After those two user-controllable settings, R tries
to find system-defined home directories. It first tries to use the
Windows "personal" directory (typicallyC:\Users\username\Documents). If
that fails, if both environment variables|HOMEDRIVE|and|HOMEPATH|are set
(and they normally are), the value is${HOMEDRIVE}${HOMEPATH}. If all of
these fail, the current working directory is used.




[[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] Help with R script

2017-07-14 Thread Vijayan Padmanabhan
Thanks Ulrik and MacQueen
I am taking inputs from both your options to
arrive at the solution that will work for my
specific requirements..
Will post my final solution once I succeed..which
could help others with similar challenge in their
work..
Appreciate both your time shared on suggesting
these solutions..

Thanks & Regards
VP




From:   Ulrik Stervbo

To: "MacQueen, Don" ,
Vijayan Padmanabhan
,
"r-help@r-project.org"

Date:   14-07-2017 10:39
Subject:Re: [R] Help with R script



@Don your solution does not solve Vijayan's
scenario 2. I used spread and gather for that.

An alternative solution to insert mising Fval -
picking up with Don's newtst - is

newtst <- c("FName: fname1", "Fval: Fval1.name1",
"FName: fname2", "Fval: Fval2.name2", "FName:
fname3", "FName: fname4", "Fval: fval4.fname4")

newtst_new <- vector(mode = "character", length =
sum(grepl("FName", newtst)) * 2)
newtst_len <- length(newtst)
i <- 1
j <- 1
while(i <= newtst_len){
  if(grepl("FName", newtst[i]) & grepl("Fval",
newtst[i + 1])){
    newtst_new[c(j, j + 1)] <- newtst[c(i, i + 1)]
    i <- i + 2
  }else{
    newtst_new[c(j, j + 1)] <- c(newtst[c(i)],
"Fval: ")
    i <- i + 1
  }
  j <- j + 2

}
newtst_new

which is also not very pretty.

HTH
Ulrik

On Thu, 13 Jul 2017 at 16:48 MacQueen, Don <
macque...@llnl.gov> wrote:
  Using Ulrik’s example data (and assuming I
  understand what is wanted), here is what I would
  do:

  ex.dat <- c("FName: fname1", "Fval:
  Fval1.name1", "Fval: ", "FName: fname2", "Fval:
  Fval2.name2", "FName: fname3")
  tst <- data.frame(x = ex.dat,
  stringsAsFactors=FALSE)

  sp <- strsplit(tst$x, ':', fixed=TRUE)
  chk <- unlist(lapply(sp, function(txt) txt[2] !=
  ' '))
  newtst <- tst[chk,,drop=FALSE]

  This both assumes and requires that ALL of the
  rows are structured as in the example data in
  the original question.
  For example:
    if any row is missing the “:”, it will fail.
    If the “:” is not followed by a space
  character it may fail (I have not checked)

  -Don

  --
  Don MacQueen

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


  On 7/13/17, 6:47 AM, "R-help on behalf of Ulrik
  Stervbo"  wrote:

      Hi Vijayan,

      one way going about it *could* be this:

      library(dplyr)
      library(tidyr)
      library(purrr)

      ex_dat <- c("FName: fname1", "Fval:
  Fval1.name1", "Fval: ", "FName:
      fname2", "Fval: Fval2.name2", "FName:
  fname3")

      data.frame(x = ex_dat) %>%
        separate(x, c("F1", "F2"), sep = ": ") %>%
        filter(F2 != "") %>%
        group_by(F1) %>%
        mutate(indx = row_number()) %>%
        spread(F1, F2, fill = "") %>%
        gather(F1, F2, FName, Fval) %>%
        arrange(indx) %>%
        mutate(x = paste(F1, F2, sep = ": ")) %>%
        select(x) %>%
        flatten_chr()

      It is not particularly nice or clever, but
  it gets the job done using R.

      HTH
      Ulrik

      On Thu, 13 Jul 2017 at 13:13 Vijayan
  Padmanabhan 
      wrote:

      >
      > Dear R-help Group
      >
      >
      > Scenario 1:
      > I have a text file running to 1000 of
  lines...that
      > is like as follows:
      >
      > [922] "FieldName: Wk3PackSubMonth"
      >
      >  [923] "FieldValue: Apr"
      >
      >  [924] "FieldName: Wk3PackSubYear"
      >
      >  [925] "FieldValue: 2017"
      >
      >  [926] "FieldName: Wk3Code1"
      >
      >  [927] "FieldValue: "
      >
      >  [928] "FieldValue: K4"
      >
      >  [929] "FieldName: Wk3Code2"
      >
      >  [930] "FieldValue: "
      >
      >  [931] "FieldValue: Q49"
      >
      >
      > I want this to be programmatically
  corrected to
      > read as follows: (All consecutive lines
  starting
      > with FieldValue is cleaned to retain only
  one
      > line)
      >
      > [922] "FieldName: Wk3PackSubMonth"
      >
      >  [923] "FieldValue: Apr"
      >
      >  [924] "FieldName: Wk3PackSubYear"
      >
      >  [925] "FieldValue: 2017"
      >
      >  [926] "FieldName: Wk3Code1"
      >
      >  [927] "FieldValue: K4"
      >
      >  [928] "FieldName: Wk3Code2"
      >
      >  [929] "FieldValue: Q49"
      >
      > Scenario 2:
      > In the same file, in some instances, the
  lines
      > could be as follows: in this case,
  wherever a line
      > is beginning with FieldName and the
  subsequent
      > line is not displaying a FieldValue, I
  would want
      > to programmatically identify such lines
  and insert
      > FieldValue (as blank).
      >
      > [941] "FieldName: Wk3Code6"
      >
      >  [942] "FieldValue: "
      >
      >  [943] "FieldName: Wk3Code7"
      >
      >  [944] "FieldValue: "
      >
      >  [945] "FieldName:
  Wk3PackWSColorStiffRemarkCode1"
      >
      >  [946] "FieldName:
  Wk3PackWSColorWrappRemarkCode1"
      >
      >  [947] "FieldName:
      > Wk3PackWSDelamiStiffRemarkCode1"
      >
      

Re: [R] setwd in windows

2017-07-14 Thread peter dalgaard
I am not sure what is going on here. The example seems incomplete. At any rate:

(a) I would be wary of changing my home directory mid-session. That is usually 
just not done.

(b) For a relative directory change, is anything wrong with setwd("./secdir") ?

-pd

> On 14 Jul 2017, at 13:42 , Troels Ring  wrote:
> 
> Dear friends - windows R 3.3.3 - sorry to ask a simple question - but I 
> cannot make setwd work properly in scripts
> 
> In the example below I have made a directory in C (firstdir) and a 
> directory in that (secdir) and the intention is to change directory to 
> the second from the first - it works when I put the entire path but not 
> the tilde - what am I missing? I was around rwf 2.14 as per the help to 
> setwd - to define the HOME - and redefine it. As I read 2.14 R_USER is 
> used for HOME when set. To no avail.
> 
> Best wishes
> 
> Troels
> 
> getwd()
> #[1] "C:/firstdir"
>  Sys.getenv("R_USER")
> #"C:\\Users\\Bruger\\Documents"
> dir()
> #[1] "chdir.R" "secdir"
> setwd("~/secdir")
> error in setwd("~/secdir") : cannot change working directory
> setwd("C:/firstdir/secdir")
> getwd()
> #"C:/firstdir/secdir"
> setwd("C:/firstdir")
> Sys.setenv(R_USER = "C:/firstdir")
> Sys.getenv("R_USER")
> setwd("~/secdir")
> 
> 
>  2.14 What are HOME and working directories?
> 
> Several places in the documentation use these terms.
> 
> The working directory is the directory from which|Rgui|or|Rterm|was 
> launched, unless a shortcut was used when it is given by the ‘Start in’ 
> field of the shortcut’s properties. You can find this from R code by the 
> call|getwd()|.
> 
> The home directory is set as follows: If environment variable|R_USER|is 
> set, its value is used. Otherwise if environment variable|HOME|is set, 
> its value is used. After those two user-controllable settings, R tries 
> to find system-defined home directories. It first tries to use the 
> Windows "personal" directory (typicallyC:\Users\username\Documents). If 
> that fails, if both environment variables|HOMEDRIVE|and|HOMEPATH|are set 
> (and they normally are), the value is${HOMEDRIVE}${HOMEPATH}. If all of 
> these fail, the current working directory is used.
> 
> 
> 
> 
>   [[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.

-- 
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] setwd in windows

2017-07-14 Thread Troels Ring
Dear friends - windows R 3.3.3 - sorry to ask a simple question - but I 
cannot make setwd work properly in scripts

In the example below I have made a directory in C (firstdir) and a 
directory in that (secdir) and the intention is to change directory to 
the second from the first - it works when I put the entire path but not 
the tilde - what am I missing? I was around rwf 2.14 as per the help to 
setwd - to define the HOME - and redefine it. As I read 2.14 R_USER is 
used for HOME when set. To no avail.

Best wishes

Troels

getwd()
#[1] "C:/firstdir"
  Sys.getenv("R_USER")
#"C:\\Users\\Bruger\\Documents"
dir()
#[1] "chdir.R" "secdir"
setwd("~/secdir")
error in setwd("~/secdir") : cannot change working directory
setwd("C:/firstdir/secdir")
getwd()
#"C:/firstdir/secdir"
setwd("C:/firstdir")
Sys.setenv(R_USER = "C:/firstdir")
Sys.getenv("R_USER")
setwd("~/secdir")


  2.14 What are HOME and working directories?

Several places in the documentation use these terms.

The working directory is the directory from which|Rgui|or|Rterm|was 
launched, unless a shortcut was used when it is given by the ‘Start in’ 
field of the shortcut’s properties. You can find this from R code by the 
call|getwd()|.

The home directory is set as follows: If environment variable|R_USER|is 
set, its value is used. Otherwise if environment variable|HOME|is set, 
its value is used. After those two user-controllable settings, R tries 
to find system-defined home directories. It first tries to use the 
Windows "personal" directory (typicallyC:\Users\username\Documents). If 
that fails, if both environment variables|HOMEDRIVE|and|HOMEPATH|are set 
(and they normally are), the value is${HOMEDRIVE}${HOMEPATH}. If all of 
these fail, the current working directory is used.




[[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] Conduct Network Analysis

2017-07-14 Thread Boris Steipe
Have a look at the functions available in the igraph package.


B.







> On Jul 13, 2017, at 11:08 PM, SEB140004 Student  
> wrote:
> 
> Greeting.
> 
> Dear Mr/Mrs/Miss,
> 
> I want to create a network by using R but I only have a table that contain
> OTU ID and the abundance value of two samples ONLY.
> 
> Isn't possible? If can, which package can be used?
> Greatly appreciated to any suggestions and helps.
> 
> Thank you.
> 
> Best regards,
> Kang Chin Yi
> 
>   [[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] Conduct Network Analysis

2017-07-14 Thread Rainer Krug
I don’t know anything about network analysis, but I really guess that more 
information is needed to give an answer.

Cheers,

Rainer

> On 14 Jul 2017, at 05:08, SEB140004 Student  wrote:
> 
> Greeting.
> 
> Dear Mr/Mrs/Miss,
> 
> I want to create a network by using R but I only have a table that contain
> OTU ID and the abundance value of two samples ONLY.
> 
> Isn't possible? If can, which package can be used?
> Greatly appreciated to any suggestions and helps.
> 
> Thank you.
> 
> Best regards,
> Kang Chin Yi
> 
>   [[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.



--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, 
UCT), Dipl. Phys. (Germany)

University of Zürich

Cell:   +41 (0)78 630 66 57

Fax (D):+49 - (0)3 21 21 25 22 44

email:  rai...@krugs.de

Skype:  RMkrug



signature.asc
Description: Message signed with OpenPGP
__
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] bnlearn and cpquery

2017-07-14 Thread Ross Chapman
Dear Marco,

 

Thanks for your helpful comments.

 

Using the posterior estimates seems to have fixed the problem.

 

Ross

 

From: Marco Scutari [mailto:marco.scut...@gmail.com] 
Sent: Thursday, 13 July 2017 7:35 PM
To: Ross Chapman 
Cc: r-help 
Subject: Re: [R] bnlearn and cpquery

 

 

Dear Ross,

This usually happen because you have parameters with a value of NaN in your 
network, because the data you estimate the network from are sparse and you are 
using maximum likelihood estimates. You should either 1) use simpler networks 
for which you can estimate all conditional distributions from the data or 2) 
use posterior estimates for the parameters.

 

Cheers,

Marco

 

On 13 July 2017 at 06:29, Ross Chapman mailto:rosspjchap...@gmail.com> > wrote:

Hi all



I have built a Bayesian network using discrete data using the bnlearn
package.



When I try to run the cpquery function on this data it returns NaN for some
some cases.



Running the cpquery in debug mode for such a case (n=10^5, method="lw")
creates the following output:



generated a grand total of 1e+05 samples.

  > event has a probability mass of 14982.37 out of NaN (p = NaN).

[1] NaN





The cpquery command takes the following structure:



cpquery(fullFitted,event=(C1_class=="Med"),

evidence=list(GK_class = "ModHi",

  GTh_class = "Lo",

  GU_class = "Lo",

  El_class = "Hi",

  E50_class = "Med",

  E150_class = "Med"

) ,

n=10^5,   method =  "lw", debug=TRUE)



Similarly, when I try to run the predict method on the same data, it returns
the following warning:



Warning message:
In map.prediction(node = node, fitted = object, data = data, n =
extra.args$n,  :
  dropping 38073 observations because generated samples are NAs.





Could you advise me why these queries are generating NaN values, and how
they might be resolved.



The session info is as follows:



sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C   LC_TIME=English_Australia.1252

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

other attached packages:
[1] bnlearn_4.2

loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1





Many thanks in advance



Ross




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




-- 

Marco Scutari, Ph.D.
Lecturer in Statistics, Department of Statistics
University of Oxford, United Kingdom


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