Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 更新功能范围之外的集合_R_Set - Fatal编程技术网

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完成