矩阵中的值赋值在r中不起作用

时间:2018-12-06 05:21:31

标签: r

假设我们有5个人的“约会脚本”,每个人包含20-25个项目。我把所有这些物品堆放在一栏中。我需要做的是创建另一列,其中包含相应人员列表中该项目所在位置(1-25)的数字。我已经写了一个函数来做,但是出了点问题。使用print(),我知道一切都在工作,直到存储项目位置。下面是我的代码的可复制版本。如果您知道我哪里出了问题,请告诉我。

script_length <- data.frame(matrix(20:25, nrow = 5, ncol = 1)
managed_data <- data.frame(matrix(1, nrow = 5, ncol = 1))

parts_before <- 0
rows_before <- 0
include_freq <- 0

 rowsb4 <- function(participant) {

      parts_before <- participant - 1

      if(parts_before == 0) {
        rows_before <- 0
      }

      if(parts_before != 0) {
        for(i in 1:parts_before) {
          rows_before <- rows_before + script_length[i,1]
        }
      }
      rows_before <- rows_before
      return(rows_before)        
    }


count_to_20_smth <- function(participant) {

  rows_before <- rowsb4(participant)
  include_freq <- script_length[participant,1]

  for(i in 1:include_freq) {
    managed_data[(rows_before + i), 1] <- i
  }

  return(managed_data)
}



    item_placement <- function(participant) {

      parts_before <- 0
      rows_before <- 0
      include_freq <- 0

      for(participant in 1:nrow(script_length)){

        parts_before <- participant - 1
        include_freq <- script_length[participant,2]

        # base case for if doing first participant
        if(parts_before == 0) {

          for(i in 1:include_freq) {
            managed_data[i,1] <- i
          }

        }

        if(parts_before != 0) {
          count_to_20_smth(participant)
        }
      }   
    }

0 个答案:

没有答案