Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

turn line of code into a function, dplyr, R

I need assistance to convert the following working line of code into a function that takes a dataframe, a column name (e.g., year), and a row number (e.g., 4) then returns the same integer result as the one line of code. My attempt below is not working.

library(tidyverse)
library(admiral)
library(lubridate)

date_df <- tribble(
  ~id, ~year,
  1, "2020-01-01",
  2, "2021-06-15",
  3, "2023-12-31",
  4, "1999",
  5, "1978",
  6, "2001-10-07",
)

# This function works, gives result as: 36525
as.integer(as_date(admiral::impute_dtc_dt(dtc = date_df$year[4], highest_imputation = "M", date_imputation = "06-15")) - as_date("1899-12-30")) # Result: 36525


# This doesn't work: My attempt at turning above into a function that takes a dataframe, a column name (e.g., year), and a row number (e.g., 4( then returns the same integer result
impute_year <- function(date_df, year, rownum) {
  as.integer(as_date(admiral::impute_dtc_dt(dtc = date_df$!!year[!!rownum], highest_imputation = "M", date_imputation = "06-15")) - as_date("1899-12-30"))
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(tidyverse)
library(admiral)
library(lubridate)

impute_year <- function(date_df, year_col, row_num) {
  as.integer(as_date(admiral::impute_dtc_dt(dtc = date_df[[year_col]][row_num], highest_imputation = "M", date_imputation = "06-15")) - as_date("1899-12-30"))
}

date_df <- tribble(
  ~id, ~year,
  1, "2020-01-01",
  2, "2021-06-15", 
  3, "2023-12-31",
  4, "1999",
  5, "1978",
  6, "2001-10-07"
)

# Test the function
impute_year(date_df, "year", 4) # Returns 36525

In the original code, the line date_df$year[4] directly accessed the 4th element of the year column in the date_df dataframe.
In the function, we need to use the [[ operator to access the column by name (date_df[[year_col]][row_num]). The !! operators used in the original attempt are not necessary here.
The rest of the function logic remains the same, using admiral::impute_dtc_dt() to impute the date and then calculating the number of days since "1899-12-30".
The function now takes three arguments:
date_df: the input dataframe
year_col: the name of the column containing the year data
row_num: the row number to extract the year data from
And it returns the same integer result as the original one-line code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading