How to port a for loop to sapply

I need to populate a column with a binary 1 if another column contains any one of a number of strings. I've worked out and tested on a small sample df but it uses a for loop which is taking ages on the large dataframe. I'm trying to port to sapply for speed but unsure how. In my understanding I need to define a function and pass to sapply like so: sapply(some_sequence, my_function).

my_function <- function(i) {
  
  if(data[i,]$DIED == 0 && data[i,]$DIED_EXTRA == 1) {
      data[i,]$DIED == 1
  }
}

# for(i in some_sequence){
#   my_function
# }

sapply(data, my_function)

I am not sure if you need a loop for this operation.
i guess tidyverse pipe operations will help you.
here my solution

library(tidyverse)


data <- tibble(DIED = c("Y","", "N","","","" , "", " "),
             SYMPTOM = c("right side of epiglottis swelled up and hinder swallowing pictures taken benadryl tylenol taken.",
                         "about 15 minutes after receiving the vaccine, the patient complained about her left arm hurting. she also complained of chest tightness and difficulty swallowing. patient also had vision changes. we gave the patient 1 tablet of benadryl 25 mg and called ems services. ems checked her out and we advised the patient to go to the er to be observed and given more benadryl.",
                         "death, extreme fatigue, dizziness,. could not lift my left arm for 72 hours" ,
                         "fever, chills, body aches, fatigue, shortness of breath, coughing, subsidied with painkillers" ,
                         "pins and needles in face started 12/29 er visit, physical exam. intermittent  pins and needles continue" ,
                         "died", "death", "dead"
             ))


data %>% 
  mutate(DIED_EXTRA  = str_detect(SYMPTOM, pattern = "died|death|dead"), .after = DIED) %>% 
  mutate(DIED = if_else(DIED == "Y", 1, 0)) %>% 
  mutate(DIED_EXTRA = DIED_EXTRA * 1) #convert logical to numeric

# # A tibble: 8 x 3
# DIED DIED_EXTRA SYMPTOM                                                                                                   
# <dbl>      <dbl> <chr>                                                                                                     
# 1          0    right side of epiglottis swelled up and hinder swallowing pictures taken benadryl tylenol taken.          
# 0          0    about 15 minutes after receiving the vaccine, the patient complained about her left arm hurting. she also…
# 0          1    death, extreme fatigue, dizziness,. could not lift my left arm for 72 hours                               
# 0          1    fever, chills, body aches, fatigue, shortness of breath, coughing, subsidied with painkillers             
# 0          0    pins and needles in face started 12/29 er visit, physical exam. intermittent  pins and needles continue   
# 0          1    died                                                                                                      
# 0          1    death                                                                                                     
# 0          1    dead     

The loop is for the final piece of porting the 1's from DIED_EXTRA into DIED column if not already there.

i have added a line to the pipe operations
here we go

library(tidyverse)


data <- tibble(DIED = c("Y","", "N","","","" , "", " "),
             SYMPTOM = c("right side of epiglottis swelled up and hinder swallowing pictures taken benadryl tylenol taken.",
                         "about 15 minutes after receiving the vaccine, the patient complained about her left arm hurting. she also complained of chest tightness and difficulty swallowing. patient also had vision changes. we gave the patient 1 tablet of benadryl 25 mg and called ems services. ems checked her out and we advised the patient to go to the er to be observed and given more benadryl.",
                         "death, extreme fatigue, dizziness,. could not lift my left arm for 72 hours" ,
                         "fever, chills, body aches, fatigue, shortness of breath, coughing, subsidied with painkillers" ,
                         "pins and needles in face started 12/29 er visit, physical exam. intermittent  pins and needles continue" ,
                         "died", "death", "dead"
             ))


data %>% 
  mutate(DIED_EXTRA  = str_detect(SYMPTOM, pattern = "died|death|dead"), .after = DIED) %>% 
  mutate(DIED = if_else(DIED == "Y", 1, 0)) %>% 
  mutate(DIED_EXTRA = DIED_EXTRA * 1) %>% #convert logical to numeric
  mutate(DIED = if_else(DIED_EXTRA == 1, 1, DIED) ) 

# 
# # A tibble: 8 x 3
# DIED DIED_EXTRA SYMPTOM                                                                                                   
# <dbl>      <dbl> <chr>                                                                                                     
#  1          0   right side of epiglottis swelled up and hinder swallowing pictures taken benadryl tylenol taken.          
#  0          0   about 15 minutes after receiving the vaccine, the patient complained about her left arm hurting. she also…
#  1          1   death, extreme fatigue, dizziness,. could not lift my left arm for 72 hours                               
#  1          1   fever, chills, body aches, fatigue, shortness of breath, coughing, subsidied with painkillers             
#  0          0   pins and needles in face started 12/29 er visit, physical exam. intermittent  pins and needles continue   
#  1          1   died                                                                                                      
#  1          1   death                                                                                                     
#  1          1   dead        
1 Like

This worked, thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.