Re: [R] Split String in regex while Keeping Delimiter

2023-04-12 Thread Bert Gunter
I always find regex puzzles amusing, so after changing the unicode
typo quotes and dashes to ascii, the following simple prescription,
similar to those proffered by others, seems to produce what you
requested with your example:

x <- c("leucocyten + gramnegatieve staven +++ grampositieve staven ++",
 "leucocyten - grampositieve coccen +")

strsplit(gsub("([^[:alnum:]]) ","\\1>>",x),">>")

(You can use unlist on this if you wish).

My slight variant uses character classes and a backreference to
identify where you want to split. The substitute '>>' split expression
is purely arbitrary of course. Instead of [^[:alnum:]] you could
probably use [+-], but I only wished to assume some sort of
non-alphanumeric.

I mention in passing that the above also seemed to work when I kept
the en dash instead of a minus sign, but I make no claim for
superiority -- or even noninferiority --  to the solutions proposed by
others.

Cheers,
Bert


On Wed, Apr 12, 2023 at 2:52 PM David Winsemius  wrote:
>
> I thought replacing the spaces following instances of +++,++,+,- with "\n" 
> and then reading with scan should succeed. Like Ivan Krylov I was fairly sure 
> that you meant the minus sign to be "-" rather than "–", but perhaps your 
> were using MS Word as an editor which is inconsistent with effective use of 
> R. If so, learn to use a proper programming editor, and in any case learn to 
> post to rhelp in plain text.
>
> --
> David
>
> scan(text=gsub("([-+]){1}\\s", "\\1\n", dat), what="", sep="\n")
>
>
>
> > On Apr 12, 2023, at 2:29 AM, Emily Bakker  wrote:
> >
> > Hello List,
> >
> > I have a dataset consisting of strings that I want to split while saving 
> > the delimiter.
> >
> > Some example data:
> > “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
> > “leucocyten – grampositieve coccen +”
> >
> > I want to split the strings such that I get the following result:
> > c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
> > c(“leucocyten –“, “grampositieve coccen +”)
> >
> > I have tried strsplit with a regular expression with a positive lookahead, 
> > but I am not able to achieve the results that I want.
> >
> > I have tried:
> > as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
> >
> > Which results in:
> > c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,  
> > “grampositieve staven ++”)
> > c(“leucocyten “, “–“, “grampositieve coccen +”)
> >
> >
> > Is there a function or regular expression that will make this possible?
> >
> > Kind regards,
> > Emily
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] Split String in regex while Keeping Delimiter

2023-04-12 Thread avi.e.gross
Sometimes you need to NOT use a regular expression and do things simpler. You 
have a fairly simple example that not only does not need great power but may be 
a pain to do using a very powerful technique, especially if you want to play 
with look-ahead and look behind.

Assuming you have a line with repeated runs on non-plussed text followed by one 
to three contiguous runs of a plus sign, can you write a short function that 
scans along till it finds a plus sign, then looks ahead till it sees a space or 
the end of the line. It then wraps up all the text till the last plus and adds 
a copy to a growing list or other structure. It then continues from the 
space(s) it ignores and repeats.

When done, you have what you want in the format you want.

A variant on this is to start from the end and scan backwards and stop at any 
plus sign. Keep what follows but strip any whitespace to the left of it. The 
result is the list in backwards order unless you used a stack to hold it.

There are quite a few variants that might apply and perhaps use of functions in 
modules. A dumb example might be to preprocess the string and replace all 
instances of 1 to 3 plus signs and an optional space  with itself and an added 
letter like a ":" and then a second pass using a regular expression becomes 
trivial as the colons disappear.

-Original Message-
From: R-help  On Behalf Of David Winsemius
Sent: Wednesday, April 12, 2023 6:03 PM
To: Emily Bakker 
Cc: r-help@r-project.org
Subject: Re: [R] Split String in regex while Keeping Delimiter

I thought replacing the spaces following instances of +++,++,+,- with "\n" and 
then reading with scan should succeed. Like Ivan Krylov I was fairly sure that 
you meant the minus sign to be "-" rather than "–", but perhaps your were using 
MS Word as an editor which is inconsistent with effective use of R. If so, 
learn to use a proper programming editor, and in any case learn to post to 
rhelp in plain text.

-- 
David

scan(text=gsub("([-+]){1}\\s", "\\1\n", dat), what="", sep="\n")



> On Apr 12, 2023, at 2:29 AM, Emily Bakker  wrote:
> 
> Hello List,
>  
> I have a dataset consisting of strings that I want to split while saving the 
> delimiter.
>  
> Some example data:
> “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
> “leucocyten – grampositieve coccen +”
>  
> I want to split the strings such that I get the following result:
> c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
> c(“leucocyten –“, “grampositieve coccen +”)
>  
> I have tried strsplit with a regular expression with a positive lookahead, 
> but I am not able to achieve the results that I want.
>  
> I have tried:
> as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
>  
> Which results in:
> c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,  
> “grampositieve staven ++”)
> c(“leucocyten “, “–“, “grampositieve coccen +”)
>  
>  
> Is there a function or regular expression that will make this possible?
>  
> Kind regards,
> Emily 
>  
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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

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


Re: [R] Split String in regex while Keeping Delimiter

2023-04-12 Thread David Winsemius
I thought replacing the spaces following instances of +++,++,+,- with "\n" and 
then reading with scan should succeed. Like Ivan Krylov I was fairly sure that 
you meant the minus sign to be "-" rather than "–", but perhaps your were using 
MS Word as an editor which is inconsistent with effective use of R. If so, 
learn to use a proper programming editor, and in any case learn to post to 
rhelp in plain text.

-- 
David

scan(text=gsub("([-+]){1}\\s", "\\1\n", dat), what="", sep="\n")



> On Apr 12, 2023, at 2:29 AM, Emily Bakker  wrote:
> 
> Hello List,
>  
> I have a dataset consisting of strings that I want to split while saving the 
> delimiter.
>  
> Some example data:
> “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
> “leucocyten – grampositieve coccen +”
>  
> I want to split the strings such that I get the following result:
> c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
> c(“leucocyten –“, “grampositieve coccen +”)
>  
> I have tried strsplit with a regular expression with a positive lookahead, 
> but I am not able to achieve the results that I want.
>  
> I have tried:
> as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
>  
> Which results in:
> c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,  
> “grampositieve staven ++”)
> c(“leucocyten “, “–“, “grampositieve coccen +”)
>  
>  
> Is there a function or regular expression that will make this possible?
>  
> Kind regards,
> Emily 
>  
> __
> 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] converting a character matrix into numeric....

2023-04-12 Thread akshay kulkarni
Dear Jeff,
 Regrets for not trying that before...never knew that % was the 
culprit...!

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Jeff Newmiller 
Sent: Thursday, April 13, 2023 1:08 AM
To: r-help@r-project.org ; akshay kulkarni 
; Rui Barradas ; R help Mailing 
list 
Subject: Re: [R] converting a character matrix into numeric

Isn't this like trying to tie up the horse after it has left the barn? Why not 
figure all this out _before_ converting to xts?

On April 12, 2023 12:29:49 PM PDT, akshay kulkarni  
wrote:
>Dear Rui,
> Not working. I have entirely removed the column containing % 
> but am still bootless:
>
>> head(coredata(INFYTX))
> INFY Historical Data INFY Historical Data INFY Historical Data INFY 
> Historical Data
>[1,] "47.26"  "44.28"  "47.56"  "44.28"
>[2,] "46.30"  "44.92"  "46.53"  "44.06"
>[3,] "45.82"  "47.27"  "47.50"  "45.63"
>[4,] "45.62"  "46.06"  "46.16"  "44.73"
>[5,] "45.05"  "46.28"  "46.50"  "44.77"
>[6,] "45.28"  "44.80"  "46.84"  "44.53"
> INFY Historical Data
>[1,] "1805267"
>[2,] "1536300"
>[3,] "887774"
>[4,] "944036"
>[5,] "759898"
>[6,] "1185402"
>> class(coredata(INFYTX)) <- "numeric"
>> head(coredata(INFYTX))
> INFY Historical Data INFY Historical Data INFY Historical Data INFY 
> Historical Data
>[1,] "47.26"  "44.28"  "47.56"  "44.28"
>[2,] "46.3"   "44.92"  "46.53"  "44.06"
>[3,] "45.82"  "47.27"  "47.5"   "45.63"
>[4,] "45.62"  "46.06"  "46.16"  "44.73"
>[5,] "45.05"  "46.28"  "46.5"   "44.77"
>[6,] "45.28"  "44.8"   "46.84"  "44.53"
> INFY Historical Data
>[1,] "1805267"
>[2,] "1536300"
>[3,] "887774"
>[4,] "944036"
>[5,] "759898"
>[6,] "1185402"
>
>THanking you,
>Yours sincerely,
>AKSHAY M KULKARNI
>
>
>From: Rui Barradas 
>Sent: Thursday, April 13, 2023 12:46 AM
>To: akshay kulkarni ; R help Mailing list 
>
>Subject: Re: [R] converting a character matrix into numeric
>
>�s 19:57 de 12/04/2023, akshay kulkarni escreveu:
>> Dear members,
>>  I have an xts object:
>>
>>> head(INFYTX)
>> INFY Historical Data INFY Historical Data.1 INFY Historical 
>> Data.2
>> 2003-04-16 "47.26"  "44.28""47.56"
>> 2003-04-17 "46.30"  "44.92""46.53"
>> 2003-04-21 "45.82"  "47.27""47.50"
>> 2003-04-22 "45.62"  "46.06""46.16"
>> 2003-04-23 "45.05"  "46.28""46.50"
>> 2003-04-24 "45.28"  "44.80""46.84"
>> INFY Historical Data.3 INFY Historical Data.4
>> 2003-04-16 "44.28""1805267"  "5.77%"
>> 2003-04-17 "44.06""1536300"  "-2.03%"
>> 2003-04-21 "45.63""887774"   "-1.04%"
>> 2003-04-22 "44.73""944036"   "-0.44%"
>> 2003-04-23 "44.77""759898"   "-1.25%"
>> 2003-04-24 "44.53""1185402"  "0.51%"
>>
>> But it is populated with character values and I want to convert them to 
>> numeric. THe following code doesn't work:
>>
>>> head(coredata(INFYTX))
>>   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 
>> INFY Historical Data.3
>> [1,] "47.26"  "44.28""47.56"
>> "44.28"
>> [2,] "46.30"  "44.92""46.53"
>> "44.06"
>> [3,] "45.82"  "47.27""47.50"
>> "45.63"
>> [4,] "45.62"  "46.06""46.16"
>> "44.73"
>> [5,] "45.05"  "46.28""46.50"
>> "44.77"
>> [6,] "45.28"  "44.80""46.84"
>> "44.53"
>>   INFY Historical Data.4
>> [1,] "1805267"  "5.77%"
>> [2,] "1536300"  "-2.03%"
>> [3,] "887774"   "-1.04%"
>> [4,] "944036"   "-0.44%"
>> [5,] "759898"   "-1.25%"
>> [6,] "1185402"  "0.51%"
>>
>>> class(coredata(INFYTX))
>> [1] "matrix" "array"
>>> class(coredata(INFYTX)) <- "numeric"
>> Warning message:
>> In class(coredata(INFYTX)) <- "numeric" : NAs introduced by coercion
>>> class(coredata(INFYTX))
>> [1] "matrix" "array"
>>> head(coredata(INFYTX))
>>   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 
>> INFY Historical Data.3
>> [1,] "47.26"  "44.28""47.56"  

Re: [R] converting a character matrix into numeric....

2023-04-12 Thread Jeff Newmiller
Isn't this like trying to tie up the horse after it has left the barn? Why not 
figure all this out _before_ converting to xts?

On April 12, 2023 12:29:49 PM PDT, akshay kulkarni  
wrote:
>Dear Rui,
> Not working. I have entirely removed the column containing % 
> but am still bootless:
>
>> head(coredata(INFYTX))
> INFY Historical Data INFY Historical Data INFY Historical Data INFY 
> Historical Data
>[1,] "47.26"  "44.28"  "47.56"  "44.28"
>[2,] "46.30"  "44.92"  "46.53"  "44.06"
>[3,] "45.82"  "47.27"  "47.50"  "45.63"
>[4,] "45.62"  "46.06"  "46.16"  "44.73"
>[5,] "45.05"  "46.28"  "46.50"  "44.77"
>[6,] "45.28"  "44.80"  "46.84"  "44.53"
> INFY Historical Data
>[1,] "1805267"
>[2,] "1536300"
>[3,] "887774"
>[4,] "944036"
>[5,] "759898"
>[6,] "1185402"
>> class(coredata(INFYTX)) <- "numeric"
>> head(coredata(INFYTX))
> INFY Historical Data INFY Historical Data INFY Historical Data INFY 
> Historical Data
>[1,] "47.26"  "44.28"  "47.56"  "44.28"
>[2,] "46.3"   "44.92"  "46.53"  "44.06"
>[3,] "45.82"  "47.27"  "47.5"   "45.63"
>[4,] "45.62"  "46.06"  "46.16"  "44.73"
>[5,] "45.05"  "46.28"  "46.5"   "44.77"
>[6,] "45.28"  "44.8"   "46.84"  "44.53"
> INFY Historical Data
>[1,] "1805267"
>[2,] "1536300"
>[3,] "887774"
>[4,] "944036"
>[5,] "759898"
>[6,] "1185402"
>
>THanking you,
>Yours sincerely,
>AKSHAY M KULKARNI
>
>
>From: Rui Barradas 
>Sent: Thursday, April 13, 2023 12:46 AM
>To: akshay kulkarni ; R help Mailing list 
>
>Subject: Re: [R] converting a character matrix into numeric
>
>�s 19:57 de 12/04/2023, akshay kulkarni escreveu:
>> Dear members,
>>  I have an xts object:
>>
>>> head(INFYTX)
>> INFY Historical Data INFY Historical Data.1 INFY Historical 
>> Data.2
>> 2003-04-16 "47.26"  "44.28""47.56"
>> 2003-04-17 "46.30"  "44.92""46.53"
>> 2003-04-21 "45.82"  "47.27""47.50"
>> 2003-04-22 "45.62"  "46.06""46.16"
>> 2003-04-23 "45.05"  "46.28""46.50"
>> 2003-04-24 "45.28"  "44.80""46.84"
>> INFY Historical Data.3 INFY Historical Data.4
>> 2003-04-16 "44.28""1805267"  "5.77%"
>> 2003-04-17 "44.06""1536300"  "-2.03%"
>> 2003-04-21 "45.63""887774"   "-1.04%"
>> 2003-04-22 "44.73""944036"   "-0.44%"
>> 2003-04-23 "44.77""759898"   "-1.25%"
>> 2003-04-24 "44.53""1185402"  "0.51%"
>>
>> But it is populated with character values and I want to convert them to 
>> numeric. THe following code doesn't work:
>>
>>> head(coredata(INFYTX))
>>   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 
>> INFY Historical Data.3
>> [1,] "47.26"  "44.28""47.56"
>> "44.28"
>> [2,] "46.30"  "44.92""46.53"
>> "44.06"
>> [3,] "45.82"  "47.27""47.50"
>> "45.63"
>> [4,] "45.62"  "46.06""46.16"
>> "44.73"
>> [5,] "45.05"  "46.28""46.50"
>> "44.77"
>> [6,] "45.28"  "44.80""46.84"
>> "44.53"
>>   INFY Historical Data.4
>> [1,] "1805267"  "5.77%"
>> [2,] "1536300"  "-2.03%"
>> [3,] "887774"   "-1.04%"
>> [4,] "944036"   "-0.44%"
>> [5,] "759898"   "-1.25%"
>> [6,] "1185402"  "0.51%"
>>
>>> class(coredata(INFYTX))
>> [1] "matrix" "array"
>>> class(coredata(INFYTX)) <- "numeric"
>> Warning message:
>> In class(coredata(INFYTX)) <- "numeric" : NAs introduced by coercion
>>> class(coredata(INFYTX))
>> [1] "matrix" "array"
>>> head(coredata(INFYTX))
>>   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 
>> INFY Historical Data.3
>> [1,] "47.26"  "44.28""47.56"
>> "44.28"
>> [2,] "46.3"   "44.92""46.53"
>> "44.06"
>> [3,] "45.82"  "47.27""47.5" 
>> "45.63"
>> [4,] "45.62"  "46.06""46.16"
>> "44.73"
>> [5,] "45.05"  "46.28""46.5" 
>> "44.77"
>> [6,] "45.28"  

Re: [R] converting a character matrix into numeric....

2023-04-12 Thread akshay kulkarni
Dear Rui,
 Not working. I have entirely removed the column containing % 
but am still bootless:

> head(coredata(INFYTX))
 INFY Historical Data INFY Historical Data INFY Historical Data INFY 
Historical Data
[1,] "47.26"  "44.28"  "47.56"  "44.28"
[2,] "46.30"  "44.92"  "46.53"  "44.06"
[3,] "45.82"  "47.27"  "47.50"  "45.63"
[4,] "45.62"  "46.06"  "46.16"  "44.73"
[5,] "45.05"  "46.28"  "46.50"  "44.77"
[6,] "45.28"  "44.80"  "46.84"  "44.53"
 INFY Historical Data
[1,] "1805267"
[2,] "1536300"
[3,] "887774"
[4,] "944036"
[5,] "759898"
[6,] "1185402"
> class(coredata(INFYTX)) <- "numeric"
> head(coredata(INFYTX))
 INFY Historical Data INFY Historical Data INFY Historical Data INFY 
Historical Data
[1,] "47.26"  "44.28"  "47.56"  "44.28"
[2,] "46.3"   "44.92"  "46.53"  "44.06"
[3,] "45.82"  "47.27"  "47.5"   "45.63"
[4,] "45.62"  "46.06"  "46.16"  "44.73"
[5,] "45.05"  "46.28"  "46.5"   "44.77"
[6,] "45.28"  "44.8"   "46.84"  "44.53"
 INFY Historical Data
[1,] "1805267"
[2,] "1536300"
[3,] "887774"
[4,] "944036"
[5,] "759898"
[6,] "1185402"

THanking you,
Yours sincerely,
AKSHAY M KULKARNI


From: Rui Barradas 
Sent: Thursday, April 13, 2023 12:46 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] converting a character matrix into numeric

�s 19:57 de 12/04/2023, akshay kulkarni escreveu:
> Dear members,
>  I have an xts object:
>
>> head(INFYTX)
> INFY Historical Data INFY Historical Data.1 INFY Historical Data.2
> 2003-04-16 "47.26"  "44.28""47.56"
> 2003-04-17 "46.30"  "44.92""46.53"
> 2003-04-21 "45.82"  "47.27""47.50"
> 2003-04-22 "45.62"  "46.06""46.16"
> 2003-04-23 "45.05"  "46.28""46.50"
> 2003-04-24 "45.28"  "44.80""46.84"
> INFY Historical Data.3 INFY Historical Data.4
> 2003-04-16 "44.28""1805267"  "5.77%"
> 2003-04-17 "44.06""1536300"  "-2.03%"
> 2003-04-21 "45.63""887774"   "-1.04%"
> 2003-04-22 "44.73""944036"   "-0.44%"
> 2003-04-23 "44.77""759898"   "-1.25%"
> 2003-04-24 "44.53""1185402"  "0.51%"
>
> But it is populated with character values and I want to convert them to 
> numeric. THe following code doesn't work:
>
>> head(coredata(INFYTX))
>   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY 
> Historical Data.3
> [1,] "47.26"  "44.28""47.56"
> "44.28"
> [2,] "46.30"  "44.92""46.53"
> "44.06"
> [3,] "45.82"  "47.27""47.50"
> "45.63"
> [4,] "45.62"  "46.06""46.16"
> "44.73"
> [5,] "45.05"  "46.28""46.50"
> "44.77"
> [6,] "45.28"  "44.80""46.84"
> "44.53"
>   INFY Historical Data.4
> [1,] "1805267"  "5.77%"
> [2,] "1536300"  "-2.03%"
> [3,] "887774"   "-1.04%"
> [4,] "944036"   "-0.44%"
> [5,] "759898"   "-1.25%"
> [6,] "1185402"  "0.51%"
>
>> class(coredata(INFYTX))
> [1] "matrix" "array"
>> class(coredata(INFYTX)) <- "numeric"
> Warning message:
> In class(coredata(INFYTX)) <- "numeric" : NAs introduced by coercion
>> class(coredata(INFYTX))
> [1] "matrix" "array"
>> head(coredata(INFYTX))
>   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY 
> Historical Data.3
> [1,] "47.26"  "44.28""47.56"
> "44.28"
> [2,] "46.3"   "44.92""46.53"
> "44.06"
> [3,] "45.82"  "47.27""47.5" 
> "45.63"
> [4,] "45.62"  "46.06""46.16"
> "44.73"
> [5,] "45.05"  "46.28""46.5" 
> "44.77"
> [6,] "45.28"  "44.8" "46.84"
> "44.53"
>   INFY Historical Data.4
> [1,] "1805267"  NA
> [2,] "1536300"  NA
> [3,] "887774"   NA
> [4,] "944036"   NA
> [5,] "759898"   NA
> [6,] "1185402"  NA
>
> Why is the coredata matrix 

Re: [R] converting a character matrix into numeric....

2023-04-12 Thread Rui Barradas

Às 19:57 de 12/04/2023, akshay kulkarni escreveu:

Dear members,
 I have an xts object:


head(INFYTX)

INFY Historical Data INFY Historical Data.1 INFY Historical Data.2
2003-04-16 "47.26"  "44.28""47.56"
2003-04-17 "46.30"  "44.92""46.53"
2003-04-21 "45.82"  "47.27""47.50"
2003-04-22 "45.62"  "46.06""46.16"
2003-04-23 "45.05"  "46.28""46.50"
2003-04-24 "45.28"  "44.80""46.84"
INFY Historical Data.3 INFY Historical Data.4
2003-04-16 "44.28""1805267"  "5.77%"
2003-04-17 "44.06""1536300"  "-2.03%"
2003-04-21 "45.63""887774"   "-1.04%"
2003-04-22 "44.73""944036"   "-0.44%"
2003-04-23 "44.77""759898"   "-1.25%"
2003-04-24 "44.53""1185402"  "0.51%"

But it is populated with character values and I want to convert them to 
numeric. THe following code doesn't work:


head(coredata(INFYTX))

  INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY 
Historical Data.3
[1,] "47.26"  "44.28""47.56""44.28"
[2,] "46.30"  "44.92""46.53""44.06"
[3,] "45.82"  "47.27""47.50""45.63"
[4,] "45.62"  "46.06""46.16""44.73"
[5,] "45.05"  "46.28""46.50""44.77"
[6,] "45.28"  "44.80""46.84""44.53"
  INFY Historical Data.4
[1,] "1805267"  "5.77%"
[2,] "1536300"  "-2.03%"
[3,] "887774"   "-1.04%"
[4,] "944036"   "-0.44%"
[5,] "759898"   "-1.25%"
[6,] "1185402"  "0.51%"


class(coredata(INFYTX))

[1] "matrix" "array"

class(coredata(INFYTX)) <- "numeric"

Warning message:
In class(coredata(INFYTX)) <- "numeric" : NAs introduced by coercion

class(coredata(INFYTX))

[1] "matrix" "array"

head(coredata(INFYTX))

  INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY 
Historical Data.3
[1,] "47.26"  "44.28""47.56""44.28"
[2,] "46.3"   "44.92""46.53""44.06"
[3,] "45.82"  "47.27""47.5" "45.63"
[4,] "45.62"  "46.06""46.16""44.73"
[5,] "45.05"  "46.28""46.5" "44.77"
[6,] "45.28"  "44.8" "46.84""44.53"
  INFY Historical Data.4
[1,] "1805267"  NA
[2,] "1536300"  NA
[3,] "887774"   NA
[4,] "944036"   NA
[5,] "759898"   NA
[6,] "1185402"  NA

Why is the coredata matrix not changing to numeric when the class is changed to 
numeric? How else to convert coredata into numeric?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

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

Hello,

Those NA come from trying to coerce character strings with "%" to numeric.

Try instead to first remove the unwanted characters.



mat <- matrix(c(0.1, 0.2, "5.77%", "-2.03%"), ncol = 2L)

mat[] <- apply(mat, 2, \(x) sub("%", "", x))
class(mat) <- "numeric"
mat
#>  [,1]  [,2]
#> [1,]  0.1  5.77
#> [2,]  0.2 -2.03


Hope this helps,

Rui Barradas

__
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] converting a character matrix into numeric....

2023-04-12 Thread akshay kulkarni
Dear members,
I have an xts object:

> head(INFYTX)
   INFY Historical Data INFY Historical Data.1 INFY Historical Data.2
2003-04-16 "47.26"  "44.28""47.56"
2003-04-17 "46.30"  "44.92""46.53"
2003-04-21 "45.82"  "47.27""47.50"
2003-04-22 "45.62"  "46.06""46.16"
2003-04-23 "45.05"  "46.28""46.50"
2003-04-24 "45.28"  "44.80""46.84"
   INFY Historical Data.3 INFY Historical Data.4
2003-04-16 "44.28""1805267"  "5.77%"
2003-04-17 "44.06""1536300"  "-2.03%"
2003-04-21 "45.63""887774"   "-1.04%"
2003-04-22 "44.73""944036"   "-0.44%"
2003-04-23 "44.77""759898"   "-1.25%"
2003-04-24 "44.53""1185402"  "0.51%"

But it is populated with character values and I want to convert them to 
numeric. THe following code doesn't work:

> head(coredata(INFYTX))
 INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY 
Historical Data.3
[1,] "47.26"  "44.28""47.56""44.28"
[2,] "46.30"  "44.92""46.53""44.06"
[3,] "45.82"  "47.27""47.50""45.63"
[4,] "45.62"  "46.06""46.16""44.73"
[5,] "45.05"  "46.28""46.50""44.77"
[6,] "45.28"  "44.80""46.84""44.53"
 INFY Historical Data.4
[1,] "1805267"  "5.77%"
[2,] "1536300"  "-2.03%"
[3,] "887774"   "-1.04%"
[4,] "944036"   "-0.44%"
[5,] "759898"   "-1.25%"
[6,] "1185402"  "0.51%"

> class(coredata(INFYTX))
[1] "matrix" "array"
> class(coredata(INFYTX)) <- "numeric"
Warning message:
In class(coredata(INFYTX)) <- "numeric" : NAs introduced by coercion
> class(coredata(INFYTX))
[1] "matrix" "array"
> head(coredata(INFYTX))
 INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY 
Historical Data.3
[1,] "47.26"  "44.28""47.56""44.28"
[2,] "46.3"   "44.92""46.53""44.06"
[3,] "45.82"  "47.27""47.5" "45.63"
[4,] "45.62"  "46.06""46.16""44.73"
[5,] "45.05"  "46.28""46.5" "44.77"
[6,] "45.28"  "44.8" "46.84""44.53"
 INFY Historical Data.4
[1,] "1805267"  NA
[2,] "1536300"  NA
[3,] "887774"   NA
[4,] "944036"   NA
[5,] "759898"   NA
[6,] "1185402"  NA

Why is the coredata matrix not changing to numeric when the class is changed to 
numeric? How else to convert coredata into numeric?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

[[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] R-error

2023-04-12 Thread Ivan Krylov
On Wed, 12 Apr 2023 16:04:55 +0800 (GMT+08:00)
"Dezhi Wang"  wrote:

> I run R 4.2.2 on CentOS-7.6.

Thank you for this useful information. Could you tell us how this build
of R was installed on this machine?

> > install.packages('sf')  
> --- Please select a CRAN mirror for use in this session ---
>  *** caught segfault ***
> address 0x60, cause 'memory not mapped'
> Traceback:
>  1: download.file(url, destfile = f, quiet = TRUE)

The text error is fine, there's no need for an additional screenshot.

Can you install the R development files (in particular, I'm interested
in the debugging symbols, which must live in the R-debuginfo package,
assuming you have installed R from a package) and the GNU debugger (gdb)
on this machine? Once you have done that, run R using the command 
R -d gdb, start R using the "r" command, reproduce the crash and then
post the backtrace that results from the "bt" command.

-- 
Best regards,
Ivan

__
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] Split String in regex while Keeping Delimiter

2023-04-12 Thread Ivan Krylov
On Wed, 12 Apr 2023 08:29:50 +
Emily Bakker  wrote:

> Some example data:
> “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
> “leucocyten – grampositieve coccen +”
>  
> I want to split the strings such that I get the following result:
> c(“leucocyten +”,  “gramnegatieve staven +++”,
>  “grampositieve staven ++”)
> c(“leucocyten –“, “grampositieve coccen +”)
>  
> I have tried strsplit with a regular expression with a positive
> lookahead, but I am not able to achieve the results that I want.

It sounds like you need positive look-behind, not look-ahead: split on
spaces only if they _follow_ one to three of '+' or '-'. Unfortunately,
repetition quantifiers like {n,m} or + are not directly supported in
look-behind expressions (nor in Perl itself). As a special case, you
can use \K, where anything to the left of \K is a zero-width positive
match:

x <- c(
 'leucocyten + gramnegatieve staven +++ grampositieve staven ++',
 'leucocyten - grampositieve coccen +'
)
strsplit(x, '[+-]{1,3}+\\K ', perl = TRUE)
# [[1]]
# [1] "leucocyten +" "gramnegatieve staven +++"
# "grampositieve staven ++" 
# 
# [[2]]
# [1] "leucocyten -"   "grampositieve coccen +"

-- 
Best regards,
Ivan

P.S. It looks like your e-mail client has transformed every quote
character into typographically-correct Unicode quotes “” and every
minus into an en dash, which makes it slightly harder to work with your
code, since typographically correct Unicode quotes are not R string
delimiters. Is it really – that you'd like to split upon, or is it -?

__
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] converting to date object...

2023-04-12 Thread akshay kulkarni
Hi Eric,
  THanks a lot..

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Eric Berger 
Sent: Wednesday, April 12, 2023 8:20 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] converting to date object...

lubridate::dmy("12 APR 2023")


On Wed, Apr 12, 2023 at 5:34 PM akshay kulkarni 
mailto:akshay...@hotmail.com>> wrote:
dear members,
I want to convert "12 APR 2023" into a Date object. 
I tried as_Date() from lubridate, but it is not working:

> as_date("12 APR 2023")
[1] NA
Warning message:
All formats failed to parse. No formats found.
> as_date("12-APR-2023")
[1] NA
Warning message:
All formats failed to parse. No formats found.

I am looking for a shorthand way of doing it. Of course, one can convert APR to 
4 and get the job done, but I ma looking for a short and elegant code

please help...

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

[[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] converting to date object...

2023-04-12 Thread akshay kulkarni
Hi Mark,
 Thanks a lot...

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Marc Schwartz 
Sent: Wednesday, April 12, 2023 8:33 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] converting to date object...

Hi,

You do not need to use third party packages for date or date/time objects in R.

If you review ?as.Date, you will see that there are two default formats for 
Date objects that are specified in the 'tryFormats' argument:

  tryFormats = c("%Y-%m-%d", "%Y/%m/%d")

If your date character vector does not conform to either one, which it does not 
in this case, then you need to explicitly specify the input format, the details 
of which are specified in ?strptime.

Thus:

> as.Date("12 APR 2023", format = "%d %b %Y")
[1] "2023-04-12"

> str(as.Date("12 APR 2023", format = "%d %b %Y"))
 Date[1:1], format: "2023-04-12"

> class(as.Date("12 APR 2023", format = "%d %b %Y"))
[1] "Date"

where the %d defines the day of the month number, %b defines the abbreviated 
month and %Y defines the four digit year. The spaces in between each specifier 
in the format argument reflect that there are spaces in the source vector, as 
opposed to hyphens (e.g. 12-APR-2023) or slashes (e.g. 12/APR/2023).

Note that once your vector is a Date class object, you can then manipulate them 
as dates and perform various operations on them. You can then use format() to 
alter the output format as you may need for other purposes, in which case, the 
formatted output is coerced back to a character vector, even though the 
internal storage is still a Date class object.

Regards,

Marc Schwartz



On April 12, 2023 at 10:34:26 AM, akshay kulkarni (akshay...@hotmail.com 
(mailto:akshay...@hotmail.com)) wrote:

> dear members,
> I want to convert "12 APR 2023" into a Date object. I tried as_Date() from 
> lubridate, but it is not working:
>
> > as_date("12 APR 2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
> > as_date("12-APR-2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
>
> I am looking for a shorthand way of doing it. Of course, one can convert APR 
> to 4 and get the job done, but I ma looking for a short and elegant code
>
> please help...
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
> [[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] converting to date object...

2023-04-12 Thread akshay kulkarni
Dear Dirk,
   Thaks a lot...

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Dirk Eddelbuettel 
Sent: Wednesday, April 12, 2023 8:38 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] converting to date object...


That is what I wrote the anytime package for: effortless automatic
parsing.  Also works for dates:

   > library(anytime)
   > anydate("12 APR 2023")
   [1] "2023-04-12"
   >

Dirk

--
dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

[[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] Split String in regex while Keeping Delimiter

2023-04-12 Thread Eric Berger
This seems to do the job but there are probably more elegant solutions:

f <- function(s) { sub("^ ","",unlist(strsplit(gsub("\\+ ","+@ ",s),"@"))) }
g <- function(s) { sub("^ ","",unlist(strsplit(gsub("- ","-@ ",s),"@"))) }
h <- function(s) { g(f(s)) }

To try it out:
s <- “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
t <- “leucocyten – grampositieve coccen +”

h(s)
h(t)

HTH,
Eric


On Wed, Apr 12, 2023 at 7:56 PM Emily Bakker 
wrote:

> Hello List,
>
> I have a dataset consisting of strings that I want to split while saving
> the delimiter.
>
> Some example data:
> “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
> “leucocyten – grampositieve coccen +”
>
> I want to split the strings such that I get the following result:
> c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
> c(“leucocyten –“, “grampositieve coccen +”)
>
> I have tried strsplit with a regular expression with a positive lookahead,
> but I am not able to achieve the results that I want.
>
> I have tried:
> as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
>
> Which results in:
> c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,
>  “grampositieve staven ++”)
> c(“leucocyten “, “–“, “grampositieve coccen +”)
>
>
> Is there a function or regular expression that will make this possible?
>
> Kind regards,
> Emily
>
> __
> 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] Split String in regex while Keeping Delimiter

2023-04-12 Thread Emily Bakker
Hello List,
 
I have a dataset consisting of strings that I want to split while saving the 
delimiter.
 
Some example data:
“leucocyten + gramnegatieve staven +++ grampositieve staven ++”
“leucocyten – grampositieve coccen +”
 
I want to split the strings such that I get the following result:
c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
c(“leucocyten –“, “grampositieve coccen +”)
 
I have tried strsplit with a regular expression with a positive lookahead, but 
I am not able to achieve the results that I want.
 
I have tried:
as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
 
Which results in:
c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,  “grampositieve 
staven ++”)
c(“leucocyten “, “–“, “grampositieve coccen +”)
 
 
Is there a function or regular expression that will make this possible?
 
Kind regards,
Emily 
 
__
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] R-error

2023-04-12 Thread Dezhi Wang
Hello,
I run R 4.2.2 on CentOS-7.6. I install  package through 
install.packages("sf").It threw the above error:


> install.packages('sf')
--- Please select a CRAN mirror for use in this session ---
 *** caught segfault ***
address 0x60, cause 'memory not mapped'
Traceback:
 1: download.file(url, destfile = f, quiet = TRUE)
 2: doTryCatch(return(expr), name, parentenv, handler)
 3: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 4: tryCatchList(expr, classes, parentenv, handlers)
 5: tryCatch({ m <- download.file(url, destfile = f, quiet = TRUE) if (m != 0L) 
stop(gettextf("'download.file()' error code '%d'", m)) read.csv(f, as.is = 
TRUE, encoding = "UTF-8")}, error = function(err) { warning(gettextf("failed to 
download mirrors file (%s); using local file '%s'", conditionMessage(err), 
local.file), call. = FALSE, immediate. = TRUE) NULL})
 6: .getMirrors("https://cran.r-project.org/CRAN_mirrors.csv;, 
file.path(R.home("doc"), "CRAN_mirrors.csv"), all = all, local.only = 
local.only)
 7: getCRANmirrors(all = FALSE, local.only = local.only)
 8: chooseCRANmirror()
 9: contrib.url(repos, type)
10: startsWith(contriburl, "file:")
11: install.packages("sf")
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection:




| |
Dezhi Wang
|
|
wangdezhist...@163.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.


Re: [R] converting to date object...

2023-04-12 Thread Dirk Eddelbuettel


That is what I wrote the anytime package for: effortless automatic
parsing.  Also works for dates:

   > library(anytime)
   > anydate("12 APR 2023")
   [1] "2023-04-12"
   > 

Dirk

-- 
dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
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] converting to date object...

2023-04-12 Thread Marc Schwartz via R-help
Hi,

You do not need to use third party packages for date or date/time objects in R.

If you review ?as.Date, you will see that there are two default formats for 
Date objects that are specified in the 'tryFormats' argument:

  tryFormats = c("%Y-%m-%d", "%Y/%m/%d")

If your date character vector does not conform to either one, which it does not 
in this case, then you need to explicitly specify the input format, the details 
of which are specified in ?strptime.

Thus:

> as.Date("12 APR 2023", format = "%d %b %Y")
[1] "2023-04-12"

> str(as.Date("12 APR 2023", format = "%d %b %Y"))
 Date[1:1], format: "2023-04-12"

> class(as.Date("12 APR 2023", format = "%d %b %Y"))
[1] "Date"

where the %d defines the day of the month number, %b defines the abbreviated 
month and %Y defines the four digit year. The spaces in between each specifier 
in the format argument reflect that there are spaces in the source vector, as 
opposed to hyphens (e.g. 12-APR-2023) or slashes (e.g. 12/APR/2023).

Note that once your vector is a Date class object, you can then manipulate them 
as dates and perform various operations on them. You can then use format() to 
alter the output format as you may need for other purposes, in which case, the 
formatted output is coerced back to a character vector, even though the 
internal storage is still a Date class object.

Regards,

Marc Schwartz



On April 12, 2023 at 10:34:26 AM, akshay kulkarni (akshay...@hotmail.com 
(mailto:akshay...@hotmail.com)) wrote:

> dear members,
> I want to convert "12 APR 2023" into a Date object. I tried as_Date() from 
> lubridate, but it is not working:
>
> > as_date("12 APR 2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
> > as_date("12-APR-2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
>
> I am looking for a shorthand way of doing it. Of course, one can convert APR 
> to 4 and get the job done, but I ma looking for a short and elegant code
>
> please help...
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
> [[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] converting to date object...

2023-04-12 Thread Eric Berger
lubridate::dmy("12 APR 2023")


On Wed, Apr 12, 2023 at 5:34 PM akshay kulkarni 
wrote:

> dear members,
> I want to convert "12 APR 2023" into a Date
> object. I tried as_Date() from lubridate, but it is not working:
>
> > as_date("12 APR 2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
> > as_date("12-APR-2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
>
> I am looking for a shorthand way of doing it. Of course, one can convert
> APR to 4 and get the job done, but I ma looking for a short and elegant code
>
> please help...
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


[R] converting to date object...

2023-04-12 Thread akshay kulkarni
dear members,
I want to convert "12 APR 2023" into a Date object. 
I tried as_Date() from lubridate, but it is not working:

> as_date("12 APR 2023")
[1] NA
Warning message:
All formats failed to parse. No formats found.
> as_date("12-APR-2023")
[1] NA
Warning message:
All formats failed to parse. No formats found.

I am looking for a shorthand way of doing it. Of course, one can convert APR to 
4 and get the job done, but I ma looking for a short and elegant code

please help...

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

[[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] aborting the execution of a script...

2023-04-12 Thread akshay kulkarni
Dear Duncan,
 But it is entirely different in my case...! As you 
said, the problem may be with my version of RStudio. The main point is that it 
doesn't pose any serious problems...

Thanks any way for your reply...

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Duncan Murdoch 
Sent: Wednesday, April 12, 2023 5:46 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] aborting the execution of a script...

This is what I get:

 > source("~/temp/test.R", echo = TRUE)

 > print(1)
[1] 1

 > stop("here")
Error in eval(ei, envir) : here

I get similar output in every variation I tried.  It never prints the 2.

On 12/04/2023 8:13 a.m., akshay kulkarni wrote:
> Dear Duncan,
>   What if I use source() with echo? I am using
> that in RStudio.
>
> THanking you,
> Yours sincerely
> AKSHAY M KULKARNI
> 
> *From:* Duncan Murdoch 
> *Sent:* Wednesday, April 12, 2023 5:35 PM
> *To:* akshay kulkarni ; R help Mailing list
> 
> *Subject:* Re: [R] aborting the execution of a script...
> On 12/04/2023 7:03 a.m., akshay kulkarni wrote:
>> Dear members,
>>  I have a script which I source it 
>> interactively. I have the following questions:
>>
>>
>>1.  If there is an error in an expression, an error message is printed, 
>> but the execution continues till the end of the script. I am sourcing with 
>> echo. Is there any way to abort the execution when the first error occurs? 
>> This happens with R CMD BATCH.  If there is an error midway, an error 
>> message is printed in the output
> file and the execution aborts. Is there a way to mimic this with source()?
>>2.  I am trying to abort the execution with ESC key, but again the same 
>> thing happens: it aborts the current expression but continues on. How to 
>> abort an R script with source()? I am using RStudio in windows.
>
> I don't see this behaviour.  If I put this in a script:
>
>print(1)
>stop("here")
>print(2)
>
> then execution stops at the "stop" line if I use source(), or R CMD
> BATCH , or in RStudio, Run or Source gives the same behaviour.  I
> think older versions of RStudio would have run all three lines using
> Run, but all the other methods have stopped at the stop() line.
>
> Duncan Murdoch
>
>
>


[[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] aborting the execution of a script...

2023-04-12 Thread Duncan Murdoch

This is what I get:

> source("~/temp/test.R", echo = TRUE)

> print(1)
[1] 1

> stop("here")
Error in eval(ei, envir) : here

I get similar output in every variation I tried.  It never prints the 2.

On 12/04/2023 8:13 a.m., akshay kulkarni wrote:

Dear Duncan,
                          What if I use source() with echo? I am using 
that in RStudio.


THanking you,
Yours sincerely
AKSHAY M KULKARNI

*From:* Duncan Murdoch 
*Sent:* Wednesday, April 12, 2023 5:35 PM
*To:* akshay kulkarni ; R help Mailing list 


*Subject:* Re: [R] aborting the execution of a script...
On 12/04/2023 7:03 a.m., akshay kulkarni wrote:

Dear members,
  I have a script which I source it interactively. 
I have the following questions:


    1.  If there is an error in an expression, an error message is printed, but the execution continues till the end of the script. I am sourcing with echo. Is there any way to abort the execution when the first error occurs? This happens with R CMD BATCH.  If there is an error midway, an error message is printed in the output 

file and the execution aborts. Is there a way to mimic this with source()?

    2.  I am trying to abort the execution with ESC key, but again the same 
thing happens: it aborts the current expression but continues on. How to abort 
an R script with source()? I am using RStudio in windows.


I don't see this behaviour.  If I put this in a script:

   print(1)
   stop("here")
   print(2)

then execution stops at the "stop" line if I use source(), or R CMD
BATCH , or in RStudio, Run or Source gives the same behaviour.  I
think older versions of RStudio would have run all three lines using
Run, but all the other methods have stopped at the stop() line.

Duncan Murdoch





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


Re: [R] aborting the execution of a script...

2023-04-12 Thread akshay kulkarni
Dear Duncan,
 What if I use source() with echo? I am using that in 
RStudio.

THanking you,
Yours sincerely
AKSHAY M KULKARNI

From: Duncan Murdoch 
Sent: Wednesday, April 12, 2023 5:35 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] aborting the execution of a script...

On 12/04/2023 7:03 a.m., akshay kulkarni wrote:
> Dear members,
>  I have a script which I source it interactively. 
> I have the following questions:
>
>
>1.  If there is an error in an expression, an error message is printed, 
> but the execution continues till the end of the script. I am sourcing with 
> echo. Is there any way to abort the execution when the first error occurs? 
> This happens with R CMD BATCH. If there is an error midway, an error message 
> is printed in the output file and the execution aborts. Is there a way to 
> mimic this with source()?
>2.  I am trying to abort the execution with ESC key, but again the same 
> thing happens: it aborts the current expression but continues on. How to 
> abort an R script with source()? I am using RStudio in windows.

I don't see this behaviour.  If I put this in a script:

  print(1)
  stop("here")
  print(2)

then execution stops at the "stop" line if I use source(), or R CMD
BATCH , or in RStudio, Run or Source gives the same behaviour.  I
think older versions of RStudio would have run all three lines using
Run, but all the other methods have stopped at the stop() line.

Duncan Murdoch




[[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] aborting the execution of a script...

2023-04-12 Thread Duncan Murdoch

On 12/04/2023 7:03 a.m., akshay kulkarni wrote:

Dear members,
 I have a script which I source it interactively. I 
have the following questions:


   1.  If there is an error in an expression, an error message is printed, but 
the execution continues till the end of the script. I am sourcing with echo. Is 
there any way to abort the execution when the first error occurs? This happens 
with R CMD BATCH. If there is an error midway, an error message is printed in 
the output file and the execution aborts. Is there a way to mimic this with 
source()?
   2.  I am trying to abort the execution with ESC key, but again the same 
thing happens: it aborts the current expression but continues on. How to abort 
an R script with source()? I am using RStudio in windows.


I don't see this behaviour.  If I put this in a script:

 print(1)
 stop("here")
 print(2)

then execution stops at the "stop" line if I use source(), or R CMD 
BATCH , or in RStudio, Run or Source gives the same behaviour.  I 
think older versions of RStudio would have run all three lines using 
Run, but all the other methods have stopped at the stop() line.


Duncan Murdoch

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


[R] aborting the execution of a script...

2023-04-12 Thread akshay kulkarni
Dear members,
I have a script which I source it interactively. I 
have the following questions:


  1.  If there is an error in an expression, an error message is printed, but 
the execution continues till the end of the script. I am sourcing with echo. Is 
there any way to abort the execution when the first error occurs? This happens 
with R CMD BATCH. If there is an error midway, an error message is printed in 
the output file and the execution aborts. Is there a way to mimic this with 
source()?
  2.  I am trying to abort the execution with ESC key, but again the same thing 
happens: it aborts the current expression but continues on. How to abort an R 
script with source()? I am using RStudio in windows.

Please help,

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

[[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] [R-pkgs] DNAmixturesLite and KinMixLite

2023-04-12 Thread Peter Green

Dear all,

We are pleased to announce that CRAN now hosts our packages 
DNAmixturesLite and KinMixLite.


These are fully R-based versions of our packages DNAmixtures and KinMix, 
which rely on a commercial software, HUGIN, for performing efficient 
computations in Bayesian networks. The lite versions rely instead on 
gRain for such computations.


DNAmixtures provides statistical methods for DNA mixture analysis and 
has been used to provide statistical evidence pertaining to DNA in 
several criminal cases in the UK and Denmark. The purpose of the newly 
released lite version is to allow users without a HUGIN software license 
to experiment with the statistical methodology. While the lite version 
aims to provide full functionality, it is noticeably less efficient than 
the original DNAmixtures package. For details on implementation and 
methodology see .


KinMix builds upon DNAmixtures and is a toolbox of methods for inference 
about relationships between contributors to a DNA mixture and other 
individuals of known genotype: a basic example would be testing whether 
a contributor to a mixture is the father of a child of known genotype. 
KinMixLite builds upon DNAmixturesLite and provides most of the 
functionality of the KinMix package, but with some loss of efficiency. 
The package implements the methods introduced in Green, P. J. and 
Mortera, J. (2017)  and Green, P. J. 
and Mortera, J. (2021) .


Regards,
Therese Graversen and Peter Green

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

__
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] Matrix scalar operation that saves memory?

2023-04-12 Thread Iago Giné Vázquez
You may take a look at the bigmemory package or other which deal with large 
memory data in 
https://cran.r-project.org/web/views/HighPerformanceComputing.html#large-memory-and-out-of-memory-data
Some extra explanation is in https://stackoverflow.com/a/11127229/997979

Iago


De: R-help  de part de Eric Berger 

Enviat el: dimecres, 12 d’abril de 2023 8:38
Per a: Bert Gunter 
A/c: R-help 
Tema: Re: [R] Matrix scalar operation that saves memory?

One possibility might be to use Rcpp.
An R matrix is stored in contiguous memory, which can be considered as a
vector.
Define a C++ function which operates on a vector in place, as in the
following:

library(Rcpp)
cppFunction(
  'void subtractConst(NumericVector x, double c) {
  for ( int i = 0; i < x.size(); ++i)
x[i] = x[i] - c;
}')

Try this function out on a matrix. Here we define a 5x2 matrix

m <- matrix(150.5 + 1:10, nrow=5)
print(m)

  [,1]  [,2]
[1,] 151.5 156.5
[2,] 152.5 157.5
[3,] 153.5 158.5
[4,] 154.5 159.5
[5,] 155.5 160.5

Now call the C++ function

subtractConst(m,100.0)
print(m)

[,1] [,2]
[1,] 51.5 56.5
[2,] 52.5 57.5
[3,] 53.5 58.5
[4,] 54.5 59.5
[5,] 55.5 60.5

HTH,
Eric


On Wed, Apr 12, 2023 at 7:34 AM Bert Gunter  wrote:

> I doubt that R's basic matrix capabilities can handle this, but have a look
> at the Matrix package, especially if your matrix is some special form.
>
> Bert
>
> On Tue, Apr 11, 2023, 19:21 Shunran Zhang <
> szh...@ngs.gen-info.osaka-u.ac.jp>
> wrote:
>
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> > [[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.
>

[[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] Matrix scalar operation that saves memory?

2023-04-12 Thread Eric Berger
One possibility might be to use Rcpp.
An R matrix is stored in contiguous memory, which can be considered as a
vector.
Define a C++ function which operates on a vector in place, as in the
following:

library(Rcpp)
cppFunction(
  'void subtractConst(NumericVector x, double c) {
  for ( int i = 0; i < x.size(); ++i)
x[i] = x[i] - c;
}')

Try this function out on a matrix. Here we define a 5x2 matrix

m <- matrix(150.5 + 1:10, nrow=5)
print(m)

  [,1]  [,2]
[1,] 151.5 156.5
[2,] 152.5 157.5
[3,] 153.5 158.5
[4,] 154.5 159.5
[5,] 155.5 160.5

Now call the C++ function

subtractConst(m,100.0)
print(m)

[,1] [,2]
[1,] 51.5 56.5
[2,] 52.5 57.5
[3,] 53.5 58.5
[4,] 54.5 59.5
[5,] 55.5 60.5

HTH,
Eric


On Wed, Apr 12, 2023 at 7:34 AM Bert Gunter  wrote:

> I doubt that R's basic matrix capabilities can handle this, but have a look
> at the Matrix package, especially if your matrix is some special form.
>
> Bert
>
> On Tue, Apr 11, 2023, 19:21 Shunran Zhang <
> szh...@ngs.gen-info.osaka-u.ac.jp>
> wrote:
>
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> > [[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.
>

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