R 更新功能范围之外的集合
我正在尝试将流派添加到我的R 更新功能范围之外的集合,r,set,R,Set,我正在尝试将流派添加到我的流派集合中。然而,对于我的类型集,我得到了NULL 功能: install.packages("sets"); library(sets) genres = set() find_all_genres = function(genres_string) { if (genres_string == "N/A") { return(NA) } genres_list = strsplit(genres_string, ",\\s+")[[1]] f
流派
集合中。然而,对于我的类型集,我得到了NULL
功能:
install.packages("sets"); library(sets)
genres = set()
find_all_genres = function(genres_string) {
if (genres_string == "N/A") {
return(NA)
}
genres_list = strsplit(genres_string, ",\\s+")[[1]]
for (genre in genres_list) {
genres = genres | set(genre)
}
}
sapply(df2$Genre, FUN = find_all_genres)
add <- function(...) {
unique(scan(text = c(...), what = "", sep = ",", na.strings = "N/A",
strip.white = TRUE, quiet = TRUE))
}
# examples
g_split <- add(g)
G <- c("Drama", "Comedy")
G <- add(G, g)
样本:
> head(df2$Genre)
[1] "Documentary, Biography, Romance" "Short, Thriller" "Documentary" "Drama, Romance" "War, Short"
[6] "Documentary, Biography"
预期的输出将是以下几行:
genres = {"Action", "Drama", "Comedy"}
当然还有更多的体裁
另外,我如何加快我的功能?我不熟悉R使用
scan
读入,使用unique
删除重复项<代码>g在末尾的注释中给出。没有使用任何软件包
unique(scan(text = g, what = "", sep = ",", na.strings = "N/A",
strip.white = TRUE, quiet = TRUE))
给予:
[1] "Documentary" "Biography" "Romance" "Short" "Thriller"
[6] "Drama" "War"
如果要排序,请在之后使用排序
作用
如果要添加到以前的一些值中,将整个内容作为函数写入:
add <- function(...) {
unique(scan(text = c(...), what = "", sep = ",", na.strings = "N/A",
strip.white = TRUE, quiet = TRUE))
}
# examples
g_split <- add(g)
G <- c("Drama", "Comedy")
G <- add(G, g)
add@G.Grothendieck已更新。我很惊讶R没有一些内置设置library@G.Grothendieck完成