Re: [R] writing a function to work with dplyr::mutate()

2021-01-20 Thread Steven Rigatti
This works perfectly. Ah, just needed a vector as output instead of a 1-column df. Thank you!!! On Tue, Jan 19, 2021 at 2:18 PM Bill Dunlap wrote: > Your translate... function seems unnecessarily complicated and reusing the > name 'var' for both the input and the data.frame containing the input

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread Jeff Newmiller
I avoid case_when, so don't complain to me about it. Bert and I both suggested standard evaluation approaches that are very amenable to using lookup tables. On January 19, 2021 1:51:17 PM PST, Steven Rigatti wrote: >I use case_when a lot - but I have a lot of dynamic tables to treat >this >way a

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread John Kane
David library(tidyverse) char_vec <- sample(c("a", "b", "c"), 10, replace = TRUE) recode(char_vec, a = "Apple") works for me. On Tue, 19 Jan 2021 at 15:13, David Winsemius wrote: > > On 1/19/21 11:17 AM, Bill Dunlap wrote: > > Your translate... function seems unnecessarily complicated and reusi

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread David Winsemius
Sent from my iPhone > On Jan 19, 2021, at 1:52 PM, Steven Rigatti wrote: > > I use case_when a lot - but I have a lot of dynamic tables to treat this > way and case_when has to be hard-coded. But, but, but my case_when-based illustration let you pass a parameter dataframe that contain

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread Steven Rigatti
I use case_when a lot - but I have a lot of dynamic tables to treat this way and case_when has to be hard-coded. On Tue, Jan 19, 2021 at 3:48 PM Jeff Newmiller wrote: > Second this. There is also the findInterval function, which omits the > factor attributes and just returns integers that can be

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread Jeff Newmiller
Second this. There is also the findInterval function, which omits the factor attributes and just returns integers that can be used in lookup tables. On January 19, 2021 10:33:59 AM PST, Bert Gunter wrote: >If you are willing to entertain another approach, have a look at ?cut. >By >defining the '

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread David Winsemius
On 1/19/21 11:17 AM, Bill Dunlap wrote: Your translate... function seems unnecessarily complicated and reusing the name 'var' for both the input and the data.frame containing the input makes it confusing to me. The following replacement, f, uses your algorithm but I think gets the answer you w

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread Bill Dunlap
Your translate... function seems unnecessarily complicated and reusing the name 'var' for both the input and the data.frame containing the input makes it confusing to me. The following replacement, f, uses your algorithm but I think gets the answer you want. f <- function(var, upper, lookup) {

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread Steven Rigatti
It's not that I can't get the output I want. I was able to do that. It is just that I can't make it pipeable - I get that weird error message that I don't understand. On Tue, Jan 19, 2021 at 1:34 PM Bert Gunter wrote: > If you are willing to entertain another approach, have a look at ?cut. By >

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread David Winsemius
On 1/19/21 7:50 AM, Steven Rigatti wrote: I am having some problems with what seems like a pretty simple issue. I have some data where I want to convert numbers. Specifically, this is cancer data and the size of tumors is encoded using millimeter measurements. However, if the actual measurement

Re: [R] writing a function to work with dplyr::mutate()

2021-01-19 Thread Bert Gunter
If you are willing to entertain another approach, have a look at ?cut. By defining the 'breaks' argument appropriately, you can easily create a factor that tells you which values should be looked up and which accepted as is. If I understand correctly, this seems to be what you want. If I have not,

Re: [R] writing a function that will refer to the elements of a dataframe much as is done by lm

2015-10-12 Thread David Winsemius
On Oct 12, 2015, at 3:11 PM, John Sorkin wrote: > I am trying to learn how to write R functions (really to program in R). I > want to write a function that will allow me to refer to the elements of a > data frame by column name, much as is done in lm. I can't seem to get the > syntax correct.

Re: [R] writing a function that will refer to the elements of a dataframe much as is done by lm

2015-10-12 Thread Jim Lemon
Hi John, Here are a couple of ways to do this. Note that they produce slightly different results. data2<-data.frame(POSTHHMONO=c(1,2,3,4),Mo6MON=c(10,11,12,13)) doit<- function(pre,post,data) { element <- deparse(substitute(pre)) print(element) print(data[element]) frame<-deparse(substitut

Re: [R] Writing a function to return column position XXXX

2012-01-25 Thread Uwe Ligges
On 25.01.2012 02:16, R. Michael Weylandt wrote: I think you are getting stuck on the same regexp problem as before (i.e., once again the dollar sign is being interpreted as the beginning You meant "end". Uwe of the line rather than an actual dollar sign) If I understand your question, mi

Re: [R] Writing a function to return column position XXXX

2012-01-24 Thread R. Michael Weylandt
I think you are getting stuck on the same regexp problem as before (i.e., once again the dollar sign is being interpreted as the beginning of the line rather than an actual dollar sign) If I understand your question, might I suggest something much easier? x = data.frame(a = c("$1034.23","1,230"),

Re: [R] Writing a function to return column position XXXX

2012-01-24 Thread David Winsemius
On Jan 24, 2012, at 11:07 AM, Dan Abner wrote: Hi everyone, I am using Michael's approach (grepl()) to identify which columns containing $ signs. I was hoping to incorporate this into a line of code that would automatically 1) find which columns contain $ signs, 2) strip the $ and commas, and

Re: [R] Writing a function to return column position XXXX

2012-01-24 Thread Dan Abner
Hi everyone, I am using Michael's approach (grepl()) to identify which columns containing $ signs. I was hoping to incorporate this into a line of code that would automatically 1) find which columns contain $ signs, 2) strip the $ and commas, and 3) convert the result to a numeric vector. I have

Re: [R] Writing a function to return column position XXXX

2012-01-24 Thread Mikko Korpela
On 01/24/2012 03:49 PM, Dan Abner wrote: > Hello everyone, > > I am writing my own function to return the column index of all variables > (these are currently character vectors) in a data frame that contain a > dollar sign($). A small piece of the data look like this: > > can_sta can_zip ind_

Re: [R] Writing a function to return column position XXXX

2012-01-24 Thread R. Michael Weylandt
Either any(grepl("$",x, fixed = TRUE)) # You probably want grepl not grep any(grepl("\\$",x) ) ? regexpr # $ has a special value Michael PS -- Stop with HTML postings (seriously, it actually does mess up what the rest of us see and I think it causes trouble for the archives as well) On Tue, Jan

Re: [R] Writing a function, want a string argument to define the name of the excel sheet to be called

2011-12-01 Thread AOLeary
Thanks Michael, I'll keep that in mind if I want to do anything more complicated. -- View this message in context: http://r.789695.n4.nabble.com/Writing-a-function-want-a-string-argument-to-define-the-name-of-the-excel-sheet-to-be-called-tp4128384p4130233.html Sent from the R help mailing list ar

Re: [R] Writing a function, want a string argument to define the name of the excel sheet to be called

2011-12-01 Thread R. Michael Weylandt
Just a heads up -- I don't think your code will work with an actual .xls(x) file, only .txt, .csv, etc (aka, plain text files). I may be wrong about that, but if you actually need to work with Excel files directly you will need an additional package. Michael On Thu, Dec 1, 2011 at 9:10 AM, AOLear

Re: [R] Writing a function, want a string argument to define the name of the excel sheet to be called

2011-12-01 Thread AOLeary
Thank you very much, that does exactly what I want it to! :) Aodhán -- View this message in context: http://r.789695.n4.nabble.com/Writing-a-function-want-a-string-argument-to-define-the-name-of-the-excel-sheet-to-be-called-tp4128384p4128495.html Sent from the R help mailing list archive at Nabb

Re: [R] Writing a function, want a string argument to define the name of the excel sheet to be called

2011-12-01 Thread Bryan Hanson
Sure, change your example as follows and then you can pass the name properly: foo <- function(x,y,NAME = "filename.csv"){ #make a matrix with x rows and y cols M <- matrix(nrow=x,ncol=y) #write the matrix write.table(M, file = NAME,append=TRUE, sep = ",") } Bryan Prof. Bryan Ha

Re: [R] writing a function

2008-02-10 Thread Johannes Hüsing
mohamed nur anisah <[EMAIL PROTECTED]> [Fri, Feb 08, 2008 at 04:42:41PM CET]: > Dear lists, > > I'm in my process of learning of writing a function. I tried to write a > simple functions of a matrix and a vector. Here are the codes: > > mm<-function(m,n){ #matrix function

Re: [R] writing a function

2008-02-08 Thread John Kane
It is doing exactly what you ask. You are asking for the last element in the matrix w[i,j] and the last element in the vector y[i]. Try return(w) and return(y). --- mohamed nur anisah <[EMAIL PROTECTED]> wrote: > Dear lists, > > I'm in my process of learning of writing a > function. I t

Re: [R] writing a function

2008-02-08 Thread Jorge Iván Vélez
Hi Mohamed, Just change return(w[i,j]) by return(w), and return(y[i]) by return(y). I hope this helps, Jorge On 2/8/08, mohamed nur anisah <[EMAIL PROTECTED]> wrote: > > Dear lists, > > I'm in my process of learning of writing a function. I tried to write a > simple functions of a matrix and

Re: [R] writing a function

2008-02-08 Thread K. Elo
Hi Mohamed, mohamed nur anisah wrote (8.2.2008): > Dear lists, > > I'm in my process of learning of writing a function. I tried to > write a simple functions of a matrix and a vector. Here are the > codes: > > mm<-function(m,n){ #matrix function > w<-matrix(nrow=m, ncol=n) > for

Re: [R] writing a function

2008-02-08 Thread Matthew Keller
Hi Mohamed, You want to return the matrix - you're returning an element of the matrix. So in your formula, insert: return(w) instead of return(w[i,j]) On Feb 8, 2008 8:42 AM, mohamed nur anisah <[EMAIL PROTECTED]> wrote: > Dear lists, > > I'm in my process of learning of writing a function.

Re: [R] writing a function

2008-02-08 Thread Bob Flagg
Anisah, You just need to omit the indices in the return statements: mm<-function(m,n){ #matrix function w<-matrix(nrow=m, ncol=n) for(i in 1:m){ for(j in 1:n){ w[i,j]=i+j } } w } v<-function