代码之家  ›  专栏  ›  技术社区  ›  Hercules Apergis

R中的函数which()

  •  0
  • Hercules Apergis  · 技术社区  · 8 年前

    我正在为此写一个for循环

    n<-nrow(Example) #Example has two only variables but the for loop attempts to expand and work also for approx 500.
    Newdata<-matrix()
    loops<-ncol(Example)-1 #number of loops
    for(i in 1:loops){
      #Example[,1] is the the guide column and thus this doesn't change in the loop
      nhat<-which(Example[1,i+1]==Example[,1]) #finds the position of the first date in column 2 according to the first column
      nend<-which(Example[n,1]==Example[,2]) #finds the position of last date in col 2 according to the first column
    Newdata[,i]<-c(rep(NA,nhat-1),Example[1:nend,i+1],NA) #creates the new data
    }
    

    但是,当我运行它时,会出现以下错误:

    Error in Ops.data.frame(Example[1, 2], Example[, 1]) : ‘==’ only defined for equally-sized data frames

    如果我用这个: which(Example$V2[1]==Example$V1) $

    Example Data

    这个 dput 输出(迷你版,因为这里不适合完整版):

    dput(Example[1:100,c(1:3)])
    structure(list(`1` = structure(c(820540800, 820627200, 820713600, 
    820800000, 821059200, 821145600, 821232000, 821318400, 821404800, 
    821664000, 821750400, 821836800, 821923200, 822009600, 822268800, 
    822355200, 822441600, 822528000, 822614400, 822873600, 822960000, 
    823046400, 823132800, 823219200, 823478400, 823564800, 823651200, 
    823737600, 823824000, 824083200, 824169600, 824256000, 824342400, 
    824428800, 824774400, 824860800, 824947200, 825033600, 825292800, 
    825379200, 825465600, 825552000, 825638400, 825897600, 825984000, 
    826070400, 826156800, 826243200, 826502400, 826588800, 826675200, 
    826761600, 826848000, 827107200, 827193600, 827280000, 827366400, 
    827452800, 827712000, 827798400, 827884800, 827971200, 828057600, 
    828316800, 828403200, 828489600, 828576000, 828921600, 829008000, 
    829094400, 829180800, 829267200, 829526400, 829612800, 829699200, 
    829785600, 829872000, 830131200, 830217600, 830304000, 830390400, 
    830476800, 830736000, 830822400, 830908800, 830995200, 831081600, 
    831340800, 831427200, 831513600, 831600000, 831686400, 831945600, 
    832032000, 832118400, 832204800, 832291200, 832550400, 832636800, 
    832723200), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `2` = structure(c(995587200, 
    995846400, 995932800, 996019200, 996105600, 996192000, 996451200, 
    996537600, 996624000, 996710400, 996796800, 997056000, 997142400, 
    997228800, 997315200, 997401600, 997660800, 997747200, 997833600, 
    997920000, 998006400, 998265600, 998352000, 998438400, 998524800, 
    998611200, 998870400, 998956800, 999043200, 999129600, 999216000, 
    999561600, 999648000, 999734400, 999820800, 1000080000, 1000684800, 
    1000771200, 1000857600, 1000944000, 1001030400, 1001289600, 1001376000, 
    1001462400, 1001548800, 1001635200, 1001894400, 1001980800, 1002067200, 
    1002153600, 1002240000, 1002499200, 1002585600, 1002672000, 1002758400, 
    1002844800, 1003104000, 1003190400, 1003276800, 1003363200, 1003449600, 
    1003708800, 1003795200, 1003881600, 1003968000, 1004054400, 1004313600, 
    1004400000, 1004486400, 1004572800, 1004659200, 1004918400, 1005004800, 
    1005091200, 1005177600, 1005264000, 1005523200, 1005609600, 1005696000, 
    1005782400, 1005868800, 1006128000, 1006214400, 1006300800, 1006473600, 
    1006732800, 1006819200, 1006905600, 1006992000, 1007078400, 1007337600, 
    1007424000, 1007510400, 1007596800, 1007683200, 1007942400, 1008028800, 
    1008115200, 1008201600, 1008288000), class = c("POSIXct", "POSIXt"
    ), tzone = "UTC"), `3` = structure(c(820540800, 820627200, 820713600, 
    820800000, 821059200, 821145600, 821232000, 821318400, 821404800, 
    821664000, 821750400, 821836800, 821923200, 822009600, 822268800, 
    822355200, 822441600, 822528000, 822614400, 822873600, 822960000, 
    823046400, 823132800, 823219200, 823478400, 823564800, 823651200, 
    823737600, 823824000, 824083200, 824169600, 824256000, 824342400, 
    824428800, 824774400, 824860800, 824947200, 825033600, 825292800, 
    825379200, 825465600, 825552000, 825638400, 825897600, 825984000, 
    826070400, 826156800, 826243200, 826502400, 826588800, 826675200, 
    826761600, 826848000, 827107200, 827193600, 827280000, 827366400, 
    827452800, 827712000, 827798400, 827884800, 827971200, 828057600, 
    828316800, 828403200, 828489600, 828576000, 828921600, 829008000, 
    829094400, 829180800, 829267200, 829526400, 829612800, 829699200, 
    829785600, 829872000, 830131200, 830217600, 830304000, 830390400, 
    830476800, 830736000, 830822400, 830908800, 830995200, 831081600, 
    831340800, 831427200, 831513600, 831600000, 831686400, 831945600, 
    832032000, 832118400, 832204800, 832291200, 832550400, 832636800, 
    832723200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("1", 
    "2", "3"), row.names = c(NA, -100L), class = c("tbl_df", "tbl", 
    "data.frame"))
    

    DatesV1     DatesV2
     ...         ...
    17/07/2001  NA
    18/07/2001  NA
    19/07/2001  19/07/2001
    20/07/2001  20/07/2001
    21/07/2001  NA
    ...         ...
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   DanY    8 年前

    您的数据存储为tible,而不是基本数据帧。基本数据帧具有 drop=TRUE df[,1] 返回向量(不是数据帧)。藏书有 drop=FALSE 测向[,1] 仍然是tible/dataframe。在这种情况下,使用 == ,因为数据帧不像向量那样循环使用。你有两个选择,

    选项1:通过重置数据帧的类来清除tible

    class(Example) <- "data.frame"
    

    选项2:添加

    Example[n,1,drop=TRUE]
    
    推荐文章