I have three dataframes that each have a rn_description
column and a label
column. For each dataframe, I want to read the text in the description
column, and depending if grepl
finds certain strings, I want to label the label
column appropriately.
I've tried
l=list(df1_wlabel= df1_wlabel,df2_wlabel=df2_wlabel,df3_wlabel=df3_wlabel)
l <- lapply(l ,
function(df){
df$label <- ifelse(grepl("email|e-mail", df$rn_description), "email", df$label)
df$label <- ifelse(grepl("phone|call|voicemail|conversation|convo",
df$rn_description), "phone", df$label)
df$label <- ifelse(grepl("text", df$rn_description), "text", df$label)
return(df)
}
)
I've also tried a for loop
for (i in 1:length(l)){
l[i]$label <- ifelse(grepl("email|e-mail", l[i]$rn_description), "email", l[i]$label)
l[i]$label <- ifelse(grepl("phone|call|voicemail|conversation|convo",
l[i]$rn_description), "phone", l[i]$label)
l[i]$label <- ifelse(grepl("text", l[i]$rn_description), "text", l[i]$label)
}
But none of these ways properly changes the label
column. Only explicitly writing out the changes for each dataframe works like so:
df1_wlabel$label <- ifelse(grepl("email|e-mail", df1_wlabel$rn_description), "email", df1_wlabel$label)
df1_wlabel$label <- ifelse(grepl("phone|call|voicemail|conversation|convo", df1_wlabel$rn_description), "phone", df1_wlabel$label)
df1_wlabel$label <- ifelse(grepl("text", df1_wlabel$rn_description), "text", df1_wlabel$label)
#then do this again for each dataframe
Why do the first two ways not work, but the last way works?