R错误:向量的长度错误,应为2 [英] R error: Wrong length for a vector, should be 2

查看:89
本文介绍了R错误:向量的长度错误,应为2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要做什么的要点:

我有2个数据帧:
x(id是唯一的)

  id时间戳282462839 2012-12-05 10:55:00282462992 2012-12-05 12:08:00282462740 2012-12-05 12:13:00282462999 2012-12-05 12:48:00 

y(id不是唯一的)

  id value1 value2282462839 300100282462839 300200282462839 400300282462999 500400282462999 300150 

我还有一个函数myfunc(id,pvalue),该函数计算某些内容并根据pvalue和其他value1s(比pvalue == value1更复杂)返回value2值之一

我想为x创建一个第三列,其中包含相应的计算出的myfunc(id,pvalue),其中pvalue是一个恒定的整数(例如20).

所以从本质上讲,我想这样做:

  x $ t20<-myfunc(x $ id,20) 

我尝试过使用lappy并以这种方式应用:

  x $ t20<-sapply(as.vector(x $ id),myfunc,pvalue = 20) 

我尝试使用lapply,也没有使用as.vector,但是我一直收到此错误:

.pres .pointsToMatrix(p2)中的错误:向量的长度错误,应为2

当我只给出在$ t20中复制$ id的位置时,它就起作用了.

我该怎么做?

这是myfunc的骨架:

  myfunc<-函数(xid,pvalue){结果<-子集(y,id == xid)retVal<--1if(nrow(result)< 12){返回(NaN)}for(i in(1:nrow(result))){#code处理结果}返回(retVal)} 

解决方案

没有完整的代码很难帮助,但是这里有一些技巧.首先,您可以获取应处理的id的逻辑矢量,然后使用矢量化的 ifelse 语句.

  tmp<-table(y $ id)> = 12y $ t20<-ifelse(tmp [as.character(y $ id)],your_new_func(),NaN) 

Here is a gist of what I want to do:

I've got 2 data frames:
x (id is unique)

id          timestamp
282462839   2012-12-05 10:55:00
282462992   2012-12-05 12:08:00
282462740   2012-12-05 12:13:00
282462999   2012-12-05 12:48:00

y (id is not unique)

id          value1    value2
282462839   300       100
282462839   300       200
282462839   400       300
282462999   500       400
282462999   300       150

I also have a function myfunc(id,pvalue) that computes something and returns one of the value2 values depending on pvalue and other value1s (more complicated than just pvalue==value1)

I want to create a 3rd column for x that contains the corresponding computed myfunc(id,pvalue), where pvalue is an integer that is constant(say 20).

so in essence, I want to do this:

x$t20 <- myfunc(x$id,20)

I tried using lappy and sapply this way:

x$t20 <- sapply(as.vector(x$id),myfunc,pvalue=20)

I tried using lapply and without the as.vector as well, but I kept getting this error:

Error in .pointsToMatrix(p2) : Wrong length for a vector, should be 2

It works when I just give mean where it just replicates $id in $t20.

How do I do this?

EDIT 1: Here's a skeleton of myfunc:

myfunc <- function(xid,pvalue) {
  result <- subset(y,id==xid)
  retVal <- -1
  if(nrow(result) < 12){
    return(NaN)
  }
  for(i in (1:nrow(result))){
    #code to process result
  }
  return(retVal)
}

解决方案

It was very difficult to help without full code, but here are some tips. First you can obtain the logical vector of id's which should be processed, then use vectorized ifelse statment.

tmp <- table(y$id) >= 12
y$t20 <- ifelse(tmp[as.character(y$id)], your_new_func(), NaN)

这篇关于R错误:向量的长度错误,应为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆