Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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
如何基于|拆分Chr数据并为每个单词创建新条目_R_Database_Dataframe - Fatal编程技术网

如何基于|拆分Chr数据并为每个单词创建新条目

如何基于|拆分Chr数据并为每个单词创建新条目,r,database,dataframe,R,Database,Dataframe,我有数据,其中一列是genrechr,其中的值类似于戏剧|音乐|犯罪,我需要分割这些数据,并需要为每个条目创建新行,就像在这个值中有3个值,所以我需要在数据框中用所有列创建三个条目 imdbId <- "tt0118578" title <-"Albela" releaseYear<- 2010 releaseDate <- "2-12-2010" genre <- "Adventure | Drama | Musical" w

我有数据,其中一列是genrechr,其中的值类似于戏剧|音乐|犯罪,我需要分割这些数据,并需要为每个条目创建新行,就像在这个值中有3个值,所以我需要在数据框中用所有列创建三个条目

   imdbId <- "tt0118578"
   title <-"Albela"
   releaseYear<- 2010
   releaseDate  <- "2-12-2010"
   genre <- "Adventure | Drama | Musical"
   writers <- "Ashutosh Gowariker (story) | Ashutosh Gowariker (screenplay) | 
   Kumar Dave (screenplay) | Sanjay Dayma (screenplay) | K.P. Saxena 
   (dialogue)"
   actors <-"Aamir Khan | Gracy Singh | Rachel Shelley | Paul Blackthorne"
   directors<-"Ashutosh Gowariker"
   sequel <-"No"
   hitFlop <-2
   df <- data.frame(imdbId, title,  releaseYear,    releaseDate,    genre,   
   writers, actors, directors,  sequel, hitFlop
    , stringsAsFactors=FALSE)**

这是数据帧的str,现在在这里,我需要分割数据,并根据单个类型值为每部电影创建唯一的条目

类似的方法可能会奏效:

数据:

请注意:

你可能必须想出一个更精确的模式

这里是第五排。如果你的类型总是在第五排,那就是你的模式

patterni <- "^(.*?\\n){4}.*(?=\\n)"
getGenres<- stringr::str_extract(multiChar, patterni) %>% sub(".*\\n","",.) %>%
    str_split("\\|",simplify = T) %>% c %>% trimws

回答一个问题很容易。。。如果问题的框架很好。没有提供任何代码,因此让我们假设一个数据帧:

title <- "Lagaan: Once Upon a Time in India"
year <- 2001
genre <- "Adventure | Drama | Musical"
df <- data.frame(title, year, genre, stringsAsFactors=FALSE)
根据需要添加或复制尽可能多的行。然后根据需要替换“类型”列中的值

对于类型名称的单个向量:

genres <- strsplit(df$genre, " \\| ")[[1]]
genres <- strsplit(df$genre, " \\| ")
有关流派名称向量的列表:

genres <- strsplit(df$genre, " \\| ")[[1]]
genres <- strsplit(df$genre, " \\| ")

我创建了一个函数,该函数使用stringr拆分一个列,为生成的列指定模式和名称前缀

    **split_into_multiple <- function(column, pattern = ", ", into_prefix){
    cols <- str_split_fixed(column, pattern, n = Inf)
    # Sub out the ""'s returned by filling the matrix to the right, with NAs which 
    are useful
    cols[which(cols == "")] <- NA
    cols <- as.tibble(cols)
    # name the 'cols' tibble as 'into_prefix_1', 'into_prefix_2', ..., 
    'into_prefix_m' 
    # where m = # columns of 'cols'
    m <- dim(cols)[2]
    names(cols) <- paste(into_prefix, 1:m, sep = "_")
    return(cols)
    }**
然后,我们可以在dplyr管道中使用“拆分成多个”,如下所示:

    **after <- BollywoodMovieDetail %>% 
    bind_cols(split_into_multiple(.$genre,"\\|", "genre")) %>% 
    # selecting those that start with 'genre_' will remove the original 'genre' column
    select(imdbId, starts_with("genre_"))
    > after
    # A tibble: 1,284 x 4
    imdbId    genre_1      genre_2     genre_3   
    <chr>     <chr>        <chr>       <chr>     
    1 tt0118578 Romance      NA          NA        
    2 tt0169102 "Adventure " " Drama "   " Musical"
    3 tt0187279 "Action "    " Comedy"   NA        
    4 tt0222024 "Drama "     " Romance"  NA        

    # ... with 1,274 more rows**
然后我们可以用收集整理

    **> after %>% 
    +     gather(key, val, -imdbId, na.rm = T)
    A tibble: 2,826 x 3
   imdbId    key     val         
  * <chr>     <chr>   <chr>       
  1 tt0118578 genre_1 Romance     
  2 tt0169102 genre_1 "Adventure "
  3 tt0187279 genre_1 "Action "   
  4 tt0222024 genre_1 "Drama "    
  5 tt0227194 genre_1 "Action "   

  # ... with 2,816 more rows**

分享你的研究成果对每个人都有帮助。告诉我们您尝试了什么,以及为什么它不能满足您的需求。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!另请参见:除了@RalfStubner所说的,您能否提供一些您尝试过的代码以及您正在使用的库?这将帮助我们更快地给你一个答案。为什么第二部冒险剧应该是唯一的戏剧呢???我对R和Stackoverflow都是新手,我也尝试了很多东西,宝莱坞动画尾%>%分开宝莱坞动画尾$流派,cgenre_1,流派_2,流派_3,|,额外=合并,虽然它会将数据分为3列,但我甚至不知道你的数据框是什么样子?你能把你的数据转换成一个数据框,然后展示出来吗?谢谢,安德烈,是的,在这个例子中它确实起了作用,但我有大约1880行的数据集,每行中我们有不同数量的类型值,用|分隔。但是谢谢你的努力,真的很感激。最后看我的代码。我不能给你一个适合所有数据的解决方案!因为我不知道你的数据,2需要很多时间,但这应该会让你找到一个解决方案。是的,我不好,但请容忍我将在未来几天内改进如何构建问题,以便让所有人都清楚。我已经编辑了问题,看看它可能会有帮助。Deepanshu,so的目的是回答一个特定的代码问题,不要提出发展问题,要求别人为你发展。请在发布代码之前运行它。提供的代码段未运行。1使用**终止一行会引发EOF错误。2在某些情况下,以a结尾的行可以在下一行继续。行不能以“”开头,。3写入器字符串具有嵌入的CR/LF;你打算那样做吗?
    **> after %>% 
    +     gather(key, val, -imdbId, na.rm = T)
    A tibble: 2,826 x 3
   imdbId    key     val         
  * <chr>     <chr>   <chr>       
  1 tt0118578 genre_1 Romance     
  2 tt0169102 genre_1 "Adventure "
  3 tt0187279 genre_1 "Action "   
  4 tt0222024 genre_1 "Drama "    
  5 tt0227194 genre_1 "Action "   

  # ... with 2,816 more rows**