简体   繁体   中英

Can we merge all the elements of a vector in R?

Basically I am trying to create a vector for movie genres. Each element of movie genres contains either one or more than one words. So my question is can we separate the words so that each index contains one single word?

[17] "Action, Drama, Mystery " "Action, Crime, Thriller "
[19] "Action, Sci-Fi, Thriller " "Biography, Crime, Drama "
[21] "Action, Adventure, Drama " "Action, Adventure, Fantasy "
[23] "Action, Drama, Sci-Fi " "Crime, Drama "
[25] "Action, Sci-Fi " "Adventure, Drama, Sci-Fi "
[27] "Crime, Drama, Mystery " "Action, Crime, Drama "
[29] "Drama, Horror, Sci-Fi " "Action, Crime, Drama "
[31] "Comedy, Music " "Comedy, Drama, Thriller "
[33] "Comedy, Drama " "Crime, Drama "
[35] "Drama, Western " "Crime, Drama "
[37] "Action, Adventure, Drama " "Action, Adventure, Thriller "

This is the output of a single vector. My question is if I have to create a vector such that each index contains single word how can I do it?

If I understood correctly

x <- c("Action, Drama, Mystery ", "Action, Sci-Fi, Thriller ", "Action, Adventure, Drama ", 
       "Action, Drama, Sci-Fi ", "Action, Sci-Fi ", "Crime, Drama, Mystery ", 
       "Drama, Horror, Sci-Fi ", "Comedy, Music ", "Comedy, Drama ", 
       "Drama, Western ", "Action, Adventure, Drama ", "Action, Crime, Thriller ", 
       "Biography, Crime, Drama ", "Action, Adventure, Fantasy ", "Crime, Drama ", 
       "Adventure, Drama, Sci-Fi ", "Action, Crime, Drama ", "Action, Crime, Drama ", 
       "Comedy, Drama, Thriller ", "Crime, Drama ", "Crime, Drama ", 
       "Action, Adventure, Thriller ")


trimws(unique(unlist(sapply(x, strsplit, split = ", "))))
#>  [1] "Action"    "Drama"     "Mystery"   "Sci-Fi"    "Thriller"  "Adventure"
#>  [7] "Drama"     "Sci-Fi"    "Crime"     "Horror"    "Comedy"    "Music"    
#> [13] "Western"   "Biography" "Fantasy"

Created on 2021-10-14 by the reprex package (v2.0.1)

由于它只是一个向量而不是数据帧,因此不需要使用sapply或类似的东西,但这就足够了:

unlist(strsplit(trimws(x), ", "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM