Re: [R] Unexpected result for df column $ subset with non-existent name

2022-10-28 Thread Rolf Turner
On Fri, 28 Oct 2022 16:42:41 +0100 Rui Barradas wrote: > This behavior, partial matching of column or list members names when > extracting with `$` is practically a FAQ. > See the latest R-Help thread on it after the release of R 4.0 > > >

Re: [R] Partition vector of strings into lines of preferred width

2022-10-28 Thread Leonard Mada via R-help
Dear Andrew, Thank you for the fast reply. I forgot about strwrap. Though my problem is slightly different. I do have the actual vector. Of course, I could first join the strings - but this then involves both a join and a split (done by strwrap). Maybe its possible to avoid the join and

Re: [R] Partition vector of strings into lines of preferred width

2022-10-28 Thread Martin Morgan
> strwrap(text) [1] "What is the best way to split/cut a vector of strings into lines of" [2] "preferred width? I have come up with a simple solution, albeit naive," [3] "as it involves many arithmetic divisions. I have an alternative idea" [4] "which avoids this problem. But I may miss some

Re: [R] Partition vector of strings into lines of preferred width

2022-10-28 Thread Andrew Simmons
I would suggest using strwrap(), the documentation at ?strwrap has plenty of details and examples. For paragraphs, I would usually do something like: strwrap(x = , width = 80, indent = 4) On Fri, Oct 28, 2022 at 5:42 PM Leonard Mada via R-help wrote: > > Dear R-Users, > > text = " > What is the

[R] Partition vector of strings into lines of preferred width

2022-10-28 Thread Leonard Mada via R-help
Dear R-Users, text = " What is the best way to split/cut a vector of strings into lines of preferred width? I have come up with a simple solution, albeit naive, as it involves many arithmetic divisions. I have an alternative idea which avoids this problem. But I may miss some existing

Re: [R] [EXTERNAL] RE: unexpected 'else' in " else"

2022-10-28 Thread Jorgen Harmse via R-help
Thank you for the comment. I don�t like the vertical sprawl either, but I assumed for the examples that the variable names were supposed to be very long or were stand-ins for complicated sub-expressions. Sometimes it�s better to put sub-expression results into temporary variables (and possibly

Re: [R] Unexpected result for df column $ subset with non-existent name

2022-10-28 Thread Rui Barradas
Às 14:52 de 28/10/2022, Sergei Ko escreveu: Hi All, Just noticed that R returns results for non-existent name if you have another variable with the same beginning when you subset with $. See the code below: name_0 <- "ID" name_1 <- "name" name_2 <- "name1" v0 <- 1:200 v1 <- c(rep(0,100),

Re: [R] unexpected 'else' in " else"

2022-10-28 Thread Ebert,Timothy Aaron
I appreciate this thread on coding. My preference for reading is to have complete sentences. I can read this: { if (x On Behalf Of Jorgen Harmse via R-help Sent: Friday, October 28, 2022 10:39 AM To: r-help@r-project.org Subject: Re: [R] unexpected 'else' in " else" [External Email] Richard

Re: [R] unexpected 'else' in " else"

2022-10-28 Thread Jorgen Harmse via R-help
Richard O'Keefe's remarks on the workings of the interpreter are correct, but the code examples are ugly and hard to read. (On the other hand, anyone who has used the debugger may be de-sensitised to horrible code formatting.) The use of whitespace should if possible reflect the structure of

Re: [R] Unexpected result for df column $ subset with non-existent name

2022-10-28 Thread Bert Gunter
Does this explain it: (from ?Extract) name A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) **partially matched** to the names of the object. -- Bert On Fri, Oct 28, 2022 at 6:53 AM Sergei Ko wrote: > > Hi All, > >

[R] Unexpected result for df column $ subset with non-existent name

2022-10-28 Thread Sergei Ko
Hi All, Just noticed that R returns results for non-existent name if you have another variable with the same beginning when you subset with $. See the code below: name_0 <- "ID" name_1 <- "name" name_2 <- "name1" v0 <- 1:200 v1 <- c(rep(0,100), rep(1,100)) v2 <- c(rep(0,50), rep(1,150)) df <-

Re: [R] Replace values based on neither condition

2022-10-28 Thread Luigi Marongiu
Perfect, thank you! On Fri, Oct 28, 2022 at 11:53 AM Rui Barradas wrote: > > Às 10:43 de 28/10/2022, Luigi Marongiu escreveu: > > Hello, > > I have a data frame with a string column. All data that are neither > > "POS" nor "NEG" should've replaced by an NA. How can I implement that > > (even

Re: [R] Replace values based on neither condition

2022-10-28 Thread Rui Barradas
Às 10:43 de 28/10/2022, Luigi Marongiu escreveu: Hello, I have a data frame with a string column. All data that are neither "POS" nor "NEG" should've replaced by an NA. How can I implement that (even with extra libraries)? My attempts actually wipe out POS and NEG... Thank you ``` df =

[R] Replace values based on neither condition

2022-10-28 Thread Luigi Marongiu
Hello, I have a data frame with a string column. All data that are neither "POS" nor "NEG" should've replaced by an NA. How can I implement that (even with extra libraries)? My attempts actually wipe out POS and NEG... Thank you ``` df = data.frame(a = 1:5, b = c("", "31.35", "POS",

Re: [R] $ subset operator behavior in lapply

2022-10-28 Thread Hilmar Berger
Hi Andrew, thanks a lot, that fully explains it. Sorry for the HTML text. For the record I put the original code again below. Best regards Hilmar On 27.10.22 18:34, Andrew Simmons wrote: > $ does not evaluate its second argument, it does something like > as.character(substitute(name)). > >