Reputation: 13
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);
Upvotes: 1
Views: 162
Reputation: 887028
Use the [[
instead of [
to extract as a vector
as as.Date
or min
works on vector
s
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
Upvotes: 1