简体   繁体   中英

how to loop through column names in R

genres=c("Action","Adventure","Animation","Biography","Comedy","Crime",
 "Documentary","Drama","Family","Game.Show","Horror","Music","Musical",
 "Mystery","Romance","Sci.Fi","Short","Thriller","War","Western")

This is my vector of genres.

Another data set has the same column names.

This is the data set column names

"Title"        "Genre"        "imdbRating"   "Release_Year" 
"Action"       "Adventure"    "Animation"    "Biography"    "Comedy"    
"Crime"        "Documentary"  "Drama"        "Family" 
"Fantasy"      "Game.Show"    "Horror"       "Music"
"Musical"      "Mystery"      "N.A"          "Romance"
"Sci.Fi"       "Short"        "Sport"        "Thriller"   
"War"          "Western"  

I want to run this command for all genres replacing each genre with the value.

     data_predict$genres[grepl("*genres*", data_predict$Genre)]=1

Orignal Data set


        data_predict<-structure(list(Genre = structure(c(3L, 1L, 2L), .Label = c("Action, Adventure, Sci-Fi", 
"Action, Drama, War", "Sci-Fi"), class = "factor"), Action = c(0, 
0, 0), Adventure = c(0, 0, 0), Animation = c(0, 0, 0), Biography = c(0, 
0, 0), Comedy = c(0, 0, 0), Crime = c(0, 0, 0), Documentary = c(0, 
0, 0), Drama = c(0, 0, 0), Family = c(0, 0, 0), Game.Show = c(0, 
0, 0), Horror = c(0, 0, 0), Music = c(0, 0, 0), Musical = c(0, 
0, 0), Mystery = c(0, 0, 0), Romance = c(0, 0, 0), Sci.Fi = c(0, 
0, 0), Short = c(0, 0, 0), Thriller = c(0, 0, 0), War = c(0, 
0, 0), Western = c(0, 0, 0)), .Names = c("Genre", "Action", "Adventure", 
"Animation", "Biography", "Comedy", "Crime", "Documentary", "Drama", 
"Family", "Game.Show", "Horror", "Music", "Musical", "Mystery", 
"Romance", "Sci.Fi", "Short", "Thriller", "War", "Western"), row.names = c(NA, 
3L), class = "data.frame") 

Expected result

data_predicted<-structure(list(Genre = structure(c(3L, 1L, 2L), .Label = c("Action, Adventure, Sci-Fi", 
    "Action, Drama, War", "Sci-Fi"), class = "factor"), Action = c(0, 
    1, 1), Adventure = c(0, 1, 0), Animation = c(0, 0, 0), Biography = c(0, 
    0, 0), Comedy = c(0, 0, 0), Crime = c(0, 0, 0), Documentary = c(0, 
    0, 0), Drama = c(0, 0, 1), Family = c(0, 0, 0), Game.Show = c(0, 
    0, 0), Horror = c(0, 0, 0), Music = c(0, 0, 0), Musical = c(0, 
    0, 0), Mystery = c(0, 0, 0), Romance = c(0, 0, 0), Sci.Fi = c(0, 
    0, 0), Short = c(0, 0, 0), Thriller = c(0, 0, 0), War = c(0, 
    0, 1), Western = c(0, 0, 0)), .Names = c("Genre", "Action", "Adventure", 
    "Animation", "Biography", "Comedy", "Crime", "Documentary", "Drama", 
    "Family", "Game.Show", "Horror", "Music", "Musical", "Mystery", 
    "Romance", "Sci.Fi", "Short", "Thriller", "War", "Western"), row.names = c(NA, 
    3L), class = "data.frame")

Try

library(qdapTools)
mtabulate(strsplit(as.character(data_predict$Genre), ', '))

Or

 data_predict[-1] <- lapply(names(data_predict)[-1],
      function(x) as.numeric(grepl(x, data_predict$Genre)))

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