簡體   English   中英

如何遍歷列,然后將其添加到數據框

[英]how to loop through columns and then add them to a dataframe

因此,我要解決的問題是我需要遍歷演員/女演員的大數據框,並根據他們已經演過的電影為他們創建一個個人資料。我創建了此個人資料功能,該功能可用於單個演員,但是我很難在整個數據幀中循環獲取所有演員的資料。

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,旁邊是個人檔案類型。 我一直得到的錯誤是

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,]“ 10964”“戲劇”

這是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")     

我想你想要這樣的東西:

#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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM