1

How do I reference parameters that are literal strings ?

In the example below I want to get the min CONTACT_DATE in the dataframe

fx1  <- function(df1, pDate ) { 
  cat( as.Date( df1[ pDate] ) %>% min( na.rm = TRUE) );
}

# Print the Min CONTACT_DATE 
fx1 ( df_Del0 , "CONTACT_DATE"); 

Thanks for your help Aubrey

tried different ways like

cat ( as.Date(df1[eval(parse(pDate)]) %>% min( na.rm = TRUE);

1 Answer 1

1

Use the [[ instead of [ to extract as a vector as as.Date or min works on vectors

fx1 <- function(df1, pDate) {
        min(as.Date(df1[[pDate]]), na.rm = TRUE)
}

The %>% is a magrittr chain. Chaining can be done in base R with |> (from R 4.1.0)

fx1 <- function(df1, pDate) {
        df1[[pDate]] |>
         as.Date() |>
         min(na.rm = TRUE)
}

Also, cat/print just print the output into console and doesn't have a return value. We can return the output for future use instead of just printing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.