这可能是一个脸部手掌的问题,但我还没有能够将下面的代码组合成一个for循环。当我尝试组合它时,所有的值返回为空,而不是1和0,当我这样做的时候。
dfs4 <- dfs4 %>%
add_column(Journal_Article = if_else(grepl("Journal Article", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Meta_Analysis = if_else(grepl("Meta-Analysis", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Systematic_Review = if_else(grepl("Systematic Review", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Case_Reports = if_else(grepl("Case Reports", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Observational_Study = if_else(grepl("Observational Study", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Randomized_Controlled_Trial = if_else(grepl("Randomized Controlled Trial", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Comparative_Study = if_else(grepl("Comparative Study", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Review = if_else(grepl("Review", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Clinical_Trial = if_else(grepl("Clinical Trial", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Historical_Article = if_else(grepl("Historical Article", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Biography = if_else(grepl("Biography", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Multicenter_Study = if_else(grepl("Multicenter Study", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Evaluation_Study = if_else(grepl("Evaluation Study", dfs4$pubtypes), 1, 0), )
dfs4 <- dfs4 %>%
add_column(Portrait = if_else(grepl("Portrait", dfs4$pubtypes), 1, 0), )
使用lapply
-
values <- c("Journal Article", "Meta-Analysis", "Systematic Review", "Case Reports")
name <- make.names(values)
dfs4[name] <- lapply(values, function(x) as.integer(grepl(x, dfs4$pubtypes)))