Rajan Raju
Rajan Raju

Reputation: 123

How to apply a function to a dataframe column in R?

I'm describing my problem below:

Data Frame:

id    date    division    clade
hCoV-19/Bangladesh/BCSIR-SUST-54/2021 2021-01-27   Sylhet             20B
hCoV-19/Bangladesh/BCSIR-SUST-55/2021 2021-01-17   Sylhet             20B
hCoV-19/Bangladesh/BCSIR-SUST-56/2021 2021-01-17   Sylhet             20A
hCoV-19/Bangladesh/BSMMU-11/2020    2020-12    Dhaka             20B
hCoV-19/Bangladesh/BSMMU-12/2020    2020-12    Dhaka             20B
hCoV-19/Bangladesh/BSMMU-14/2020    2020-12    Dhaka             20B
hCoV-19/Bangladesh/BSMMU-15/2020    2020-12    Dhaka             20B
hCoV-19/Bangladesh/BSMMU-16/2020    2020-12    Dhaka             20B

Source Code:

adder_dd_with_yyyy_mm <- function(date) {
  if(str_count(date, "-") == 1) {
    return(paste(date, "01", sep = "-"))
  } else {
    return(date)
  }
}

df <- read.csv("figure_metadata.tsv", sep = "\t")
df["date"] <- lapply(df["date"], adder_dd_with_yyyy_mm)

Why lapply is not working? There is no change in date column after the execution of the code.

Upvotes: 1

Views: 144

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

All the functions that you are using are vectorized so there is no need of lapply.

You can do -

library(stringr)

df <- transform(df, date = ifelse(str_count(date, "-") == 1, 
                    paste0(date, "-01"), date))
df
#                                     id       date division clade
#1 hCoV-19/Bangladesh/BCSIR-SUST-54/2021 2021-01-27   Sylhet   20B
#2 hCoV-19/Bangladesh/BCSIR-SUST-55/2021 2021-01-17   Sylhet   20B
#3 hCoV-19/Bangladesh/BCSIR-SUST-56/2021 2021-01-17   Sylhet   20A
#4      hCoV-19/Bangladesh/BSMMU-11/2020 2020-12-01    Dhaka   20B
#5      hCoV-19/Bangladesh/BSMMU-12/2020 2020-12-01    Dhaka   20B
#6      hCoV-19/Bangladesh/BSMMU-14/2020 2020-12-01    Dhaka   20B
#7      hCoV-19/Bangladesh/BSMMU-15/2020 2020-12-01    Dhaka   20B
#8      hCoV-19/Bangladesh/BSMMU-16/2020 2020-12-01    Dhaka   20B

Upvotes: 3

Kra.P
Kra.P

Reputation: 15123

Instead of df["date"], use df$date will work.

Upvotes: 1

Related Questions