本文介绍了找不到对象-在R中编写子设置功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据集:

dput(head(test1, 10))

structure(list(startdate = structure(c(1572994800, 1572994800, 
1572994800, 1572994800, 1572994800, 1572994800, 1572994800, 1572994800, 
1572994800, 1572994800), class = c("POSIXct", "POSIXt"), tzone = ""), 
    id = c("POL55", "POL56", "POL57", "POL58", "POL59", "POL60", 
    "POL61", "POL62", "POL63", "POL64"), m0_9 = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), m10_19 = c(NA, NA, NA, 32, 34, NA, NA, 
    NA, NA, 55), m20_29 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), m30_39 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 32), m40_49 = c(32, 
    34, NA, NA, NA, NA, 55, 3, NA, NA), m50_59 = c(NA, NA, NA, 
    NA, NA, NA, 32, 34, NA, NA), m60_69 = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), m70 = c(NA, NA, NA, NA, NA, NA, 32, 
    34, NA, NA), f0_9 = c(32, 34, NA, NA, NA, NA, 55, 3, NA, 
    NA), f10_19 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), f20_29 = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), f30_39 = c(NA, NA, NA, 32, 
    34, NA, NA, NA, NA, 55), f40_49 = c(NA, NA, NA, NA, NA, 32, 
    34, NA, NA, NA), f50_59 = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), f60_69 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), f70 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 32)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

我正在编写一个小函数(它将成为一个更大的函数的一部分).但是,我似乎有问题,例如

I am writing a little function (which will be a part of a much larger function). However, i seem to be something wrong, as

test1$startdate <- as.POSIXct(test1$startdate, origin="1970-01-01")  

createLists("POL55", as.POSIXct(2019-11-27, origin="1970-01-01"))

createLists <- function(id, start){
  testFiltered = test1[id == id & startdate == start]

  ageGroup <- vector("list", length == nrow(testFiltered))
  names(ageGroup) <- as.character(seq_along(ageGroup))

  return(ageGroup)
}

我该如何解决错误找不到对象'startdate'的错误 ??

how can i fix the error object 'startdate' not found ??

推荐答案

这有望完成这项工作.

createLists <- function(id, start) {
  testFiltered = test1[test1$id==id & test1$startdate == start,] # need test1$id etc.

  if(nrow(testFiltered) > 0) { # in case empty subset
     ageGroup <- vector("list", length = nrow(testFiltered))  # Only one '=' here
     names(ageGroup) <- as.character(seq_along(ageGroup))
     return(ageGroup)
  }
  else
     cat("Nothing to return\n")
}

createLists("POL55", as.POSIXct('2019-11-06', origin="1970-01-01"))

希望它对您有用.☺

这篇关于找不到对象-在R中编写子设置功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:04