R-Help How does one pass a character string containing a hyphen? I have a function that accesses an api if I hard code the object, for example key_key <- "xxxx-yyyy" it works but when I pass the key code to the function (say something like key_code <- code_input) it returns only xxxx. So R is seeing a string with a negative operator I'm assuming Jeff [[alternative HTML version deleted]]
tmp <- function(s) { return(str(s)) } key <- "xxxx-yyyy" tmp(key) # chr "xxxx-yyyy" ... works for me. Reprex? Cheers, Boris
Tom Woolman
2020-Nov-17 21:10 UTC
[R] counting duplicate items that occur in multiple groups
Hi everyone. I have a dataframe that is a collection of Vendor IDs plus a bank account number for each vendor. I'm trying to find a way to count the number of duplicate bank accounts that occur in more than one unique Vendor_ID, and then assign the count value for each row in the dataframe in a new variable. I can do a count of bank accounts that occur within the same vendor using dplyr and group_by and count, but I can't figure out a way to count duplicates among multiple Vendor_IDs. Dataframe example code: #Create a sample data frame: set.seed(1) Data <- data.frame(Vendor_ID = sample(1:10000), Bank_Account_ID = sample(1:10000)) Thanks in advance for any help.
Boris Yes in that case it works as you have assigned a character string to key. My problem is I'm taking a string input from a shiny app and when I run the function it only sees xxxx. How to assign xxxx-yyyy to a character string. Jeff -----Original Message----- From: Boris Steipe <boris.steipe at utoronto.ca> Sent: Tuesday, November 17, 2020 3:00 PM To: reichmanj at sbcglobal.net Cc: r-help at r-project.org Subject: Re: [R] How to pass a character string with a hyphen tmp <- function(s) { return(str(s)) } key <- "xxxx-yyyy" tmp(key) # chr "xxxx-yyyy" .. works for me. Reprex? Cheers, Boris
Hi, You might want to look at ?shQuote, which wraps text in single quotes, if the source text does not include them, or double quotes otherwise, as might be used in a shell setting, where you are passing arguments that may have spaces or other characters that may be evaluated. My guess is that the API that you are passing the character vector to may be parsing/evaluating the '-' and only seeing the first part of the passed value. So, for example:> shQuote("xxxx-yyyy")[1] "'xxxx-yyyy'" See if that works. Regards, Marc Schwartz> On Nov 17, 2020, at 3:43 PM, Jeff Reichman <reichmanj at sbcglobal.net> wrote: > > R-Help > > How does one pass a character string containing a hyphen? I have a function > that accesses an api if I hard code the object, for example > > key_key <- "xxxx-yyyy" > > it works but when I pass the key code to the function (say something like > key_code <- code_input) it returns only xxxx. So R is seeing a string with > a negative operator I'm assuming > > Jeff >
Strip the left characters and strip the right characters into their own variables using one of the methods that can do that. Then pass it using something like paste(left, "-", right). On Tue, Nov 17, 2020, 2:43 PM Jeff Reichman <reichmanj at sbcglobal.net> wrote:> R-Help > > > > How does one pass a character string containing a hyphen? I have a function > that accesses an api if I hard code the object, for example > > > > key_key <- "xxxx-yyyy" > > > > it works but when I pass the key code to the function (say something like > key_code <- code_input) it returns only xxxx. So R is seeing a string with > a negative operator I'm assuming > > > > Jeff > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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]]
Robert I ended up using the paste command, its not pretty but it works thank you Jeff From: Robert Knight <bobby.knight at gmail.com> Sent: Tuesday, November 17, 2020 3:56 PM To: reichmanj at sbcglobal.net Cc: r-help at r-project.org Subject: Re: [R] How to pass a character string with a hyphen Strip the left characters and strip the right characters into their own variables using one of the methods that can do that. Then pass it using something like paste(left, "-", right). On Tue, Nov 17, 2020, 2:43 PM Jeff Reichman <reichmanj at sbcglobal.net <mailto:reichmanj at sbcglobal.net> > wrote: R-Help How does one pass a character string containing a hyphen? I have a function that accesses an api if I hard code the object, for example key_key <- "xxxx-yyyy" it works but when I pass the key code to the function (say something like key_code <- code_input) it returns only xxxx. So R is seeing a string with a negative operator I'm assuming Jeff [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org <mailto:R-help at 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]]