如何循环使用R中的列名
这是我的风格载体 另一个数据集具有相同的列名 这是数据集的列名如何循环使用R中的列名,r,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") 我想对所有类型
genres=c("Action","Adventure","Animation","Biography","Comedy","Crime",
"Documentary","Drama","Family","Game.Show","Horror","Music","Musical",
"Mystery","Romance","Sci.Fi","Short","Thriller","War","Western")
我想对所有类型运行此命令,用值替换每个类型
"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"
data\u predict$Genre[grepl(“*genres*”,data\u predict$Genre)]=1
原始数据集
数据预测Try
或
data\u predict[-1]体裁是“科幻”“动作、冒险、科幻”“动作、戏剧、战争”我想更新那些包含体裁的列。就像一行包含动作剧和战争一样,它的动作剧和战争应该成为1我正在使用此代码data\u predict$Action[grepl(“动作”,data\u predict$体裁)]=1数据预测$冒险[grepl(“*冒险*”,数据预测$流派)]=1数据预测$动画[grepl(“*动画*”,数据预测$流派)]=1数据预测$传记[grepl(“*传记*”,数据预测$流派)]=1数据预测$喜剧[grepl(“*喜剧*”,数据预测$流派)]=1数据预测$犯罪[grepl(“*犯罪*,数据预测$流派)]=1数据预测$纪录片[grepl(“*纪录片*”,数据预测$流派)]=1数据预测$戏剧[grepl(“*戏剧*”,数据预测$流派)]=1
我想减少步骤。你能帮我吗?你可以在数据集中看到,我只想更新那些包含流派的列。例如,第二行包含动作、冒险和科幻,所以动作、冒险、科幻应该为这一行设置1。粘贴(流派,折叠=“|”)未工作图像没有帮助,因为我必须手动键入要测试的数据。请检查指南我是新的,因此很抱歉造成的麻烦。谢谢alot@PriyankPuri没问题。很高兴帮助你
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")
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")
library(qdapTools)
mtabulate(strsplit(as.character(data_predict$Genre), ', '))
data_predict[-1] <- lapply(names(data_predict)[-1],
function(x) as.numeric(grepl(x, data_predict$Genre)))