将参数传递给R函数for-loop语句

时间:2016-05-05 02:32:58

标签: r

我正在尝试在R中创建一个for循环,以计算数据集中20部电影中每部电影的“4或5评级”的速率,并从计算中删除缺少的约会。我很难接受一个参数并将它作为一个字符串连接成一个语句。

testing_now <- function() {

  ## Load data
  data <- read.csv("A1Ratings.csv")

  ## Rename columns
  names(data) <- c("user","x260","x1210","x356","x318","x593","x3578","x1","x2028","x296","x1259","x2396","x2916","x780","x541","x1265","x2571","x527","x2762","x1198","x34")

  ## Remove "user" column
  new_data <- data[ , c(-1)]

  ## For-loop statements
  for (movieList in names(data)) {

    ## Test statement: This works
    print(movieList)

    ## The remaining statements are returning NULL and NaN
    print(new_data$movieList)
    print((sum(new_data$movieList ==4, na.rm=TRUE) + sum(new_data$movieList==5, na.rm=TRUE))/sum(!is.na(new_data$movieList)))

  }
}

调用函数...

testing_now()

结果:...
[1]“x260”
NULL
[1] NaN
......

显然,“print(new_data $ movieList)”中的语句并没有返回所需的输出。

0 个答案:

没有答案