如何遍历列,然后将它们添加到数据框

时间:2018-05-03 17:15:29

标签: r

所以我正在处理的问题要求我经历一个大型的演员/女演员数据框,并根据他们已经演过的电影为他们创建一个配置文件。我创建了这个可以使用的配置文件功能对于一个演员/演员来说,但是我无法在整个数据框中循环它以获得所有演员/演员的个人资料。

profile <- function(actor, df) {
   dftest <- subset(df, df$id.x == actor)
   fit <- rpart(name.y ~ id.x, method = "class", dftest)
   p <- predict(fit, dftest)
   return(colnames(p)[max.col(p,ties.method="first")][1])
  }

这是我已经有的for循环,但是我一直在收到错误,并且不确定我是否正确地使用了这个方法。

for (k in c(1)) {
  user.frame <- data.frame()
  for (i in df2$id.x) { # df2$id.x is the column of actors names
    user.frame[i] <- data.frame(profile(i, df2))
  }
  df2final <- rbind(final, user.frame)
  View(df2fin)
}

**编辑**

这是进入配置文件功能的数据

# A tibble: 278,361 x 7
   movie_id title   id.x name.x            id.y   id1 name.y         
      <int> <chr>  <int> <chr>            <int> <int> <chr>          
 1    19995 Avatar 65731 Sam Worthington  19995    28 Action         
 2    19995 Avatar 65731 Sam Worthington  19995    12 Adventure      
 3    19995 Avatar 65731 Sam Worthington  19995    14 Fantasy        
 4    19995 Avatar 65731 Sam Worthington  19995   878 Science Fiction
 5    19995 Avatar  8691 Zoe Saldana      19995    28 Action         
 6    19995 Avatar  8691 Zoe Saldana      19995    12 Adventure      
 7    19995 Avatar  8691 Zoe Saldana      19995    14 Fantasy        
 8    19995 Avatar  8691 Zoe Saldana      19995   878 Science Fiction
 9    19995 Avatar 10205 Sigourney Weaver 19995    28 Action         
10    19995 Avatar 10205 Sigourney Weaver 19995    12 Adventure    

理想情况下,我希望for循环在最后给出一个数据框,在一列中有一个actor ID,旁边有一个profile类型。我一直得到的错误就是这个

Error in `[<-.data.frame`(`*tmp*`, i, value = list(profile.i..df2. = 1L)) : 
  new columns would leave holes after existing columns 

所以我用你的代码做了这个:

df <- data.frame(a = rep(df2$id.x))
df$b <- df2$name.y

actor.names <- unique(df$a)
result.file <- matrix(ncol = 2, nrow = length(actor.names))

for(i in 1:length(actor.names)){

  dftest  <- subset(df, a == actor.names[i])    #subset actor name
  fit     <- rpart::rpart(b ~ a, method = "class", dftest) #run model
  p       <- predict(fit, dftest) #predict genre
  temp    <- colnames(p)[max.col(p,ties.method="first")][1]

  result.file[i,1] <- actor.names[i]
  result.file[i,2] <- temp
}

它给了我这个错误:

Error in cbind(yval2, yprob, nodeprob) : 
  number of rows of matrices must match (see arg 2)

但我得到的结果是我所需要的。我应该担心吗?

result.file
     [,1]      [,2]             
[1,] "65731"   "Action"         
[2,] "8691"    "Action"         
[3,] "10205"   "Comedy"         
[4,] "32747"   "Action"         
[5,] "17647"   "Action"         
[6,] "1771"    "Drama"          
[7,] "59231"   "Comedy"         
[8,] "30485"   "Action"         
[9,] "15853"   "Adventure"      

[10,]&#34; 10964&#34; &#34;剧情&#34;

这是dput(head(df))

dput(head(df))
structure(list(a = c(65731L, 65731L, 65731L, 65731L, 8691L, 8691L
), b = c("Action", "Adventure", "Fantasy", "Science Fiction", 
"Action", "Adventure")), .Names = c("a", "b"), row.names = c(NA, 
6L), class = "data.frame")     

1 个答案:

答案 0 :(得分:0)

我想你想要这样的东西:

#generate data (4 actors, each with 2x Genre1 and 1x Genre3)
df <- data.frame(a = rep(c("Actor1","Actor2","Actor3","Actor4"),each=3),
                 b = rep(c("Genre1","Genre1","Genre3"),4),
                 stringsAsFactors = F)

#create a vector with actor names
actor.names <- unique(df$a)

#create a storage matrix for the results
result.file <- matrix(ncol = 2,
                      nrow = length(actor.names))


for(i in 1:length(actor.names)){

dftest  <- subset(df, a == actor.names[i])    #subset actor name
fit     <- rpart::rpart(b ~ a, method = "class", dftest) #run model
p       <- predict(fit, dftest) #predict genre
temp    <- colnames(p)[max.col(p,ties.method="first")][1]

result.file[i,1] <- actor.names[i]
result.file[i,2] <- temp
}

    result.file
     [,1]     [,2]    
[1,] "Actor1" "Genre1"
[2,] "Actor2" "Genre1"
[3,] "Actor3" "Genre1"
[4,] "Actor4" "Genre1"