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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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"))
}
</code>
<code>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")) } </code>
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"))
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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.

New contributor

Pollo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật