我有来自一项开放式调查的数据。我有一个注释表和一个代码表。代码表是一组主题或字符串。
我正在尝试做的事情:
检查代码表中相关列中是否存在开放式注释中的单词/字符串。在评论表中为特定主题添加一个新列,并添加一个二进制 1 或 0 来表示已标记哪些记录。
代码表中有相当多的列,这些列是实时且不断变化的,列顺序和列数可能会发生变化。
我目前正在以一种相当复杂的方式来做这件事,我用多行代码单独检查每一列,我认为可能有更好的方法来做到这一点。
我不知道如何让 lapply 与 stringi 函数一起使用。
非常感谢您的帮助。
这是一组示例代码,以便您可以了解我正在尝试执行的操作:
#Two tables codes and comments
#codes table
codes <- structure(
list(
Support = structure(
c(2L, 3L, NA),
.Label = c("",
"help", "questions"),
class = "factor"
),
Online = structure(
c(1L,
3L, 2L),
.Label = c("activities", "discussion board", "quiz"),
class = "factor"
),
Resources = structure(
c(3L, 2L, NA),
.Label = c("", "pdf",
"textbook"),
class = "factor"
)
),
row.names = c(NA,-3L),
class = "data.frame"
)
#comments table
comments <- structure(
list(
SurveyID = structure(
1:5,
.Label = c("ID_1", "ID_2",
"ID_3", "ID_4", "ID_5"),
class = "factor"
),
Open_comments = structure(
c(2L,
4L, 3L, 5L, 1L),
.Label = c(
"I could never get the pdf to download",
"I didn’t get the help I needed on time",
"my questions went unanswered",
"staying motivated to get through the textbook",
"there wasn’t enough engagement in the discussion board"
),
class = "factor"
)
),
class = "data.frame",
row.names = c(NA,-5L)
)
#check if any words from the columns in codes table match comments
#here I am looking for a match column by column but looking for a better way - lappy?
support = paste(codes$Support, collapse = "|")
supp_stringi = stri_detect_regex(comments$Open_comments, support)
supp_grepl = grepl(pattern = support, x = comments$Open_comments)
identical(supp_stringi, supp_grepl)
comments$Support = ifelse(supp_grepl == TRUE, 1, 0)
# What I would like to do is loop through all columns in codes rather than outlining the above code for each column in codes