Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将整数(0)替换为NA_R_Integer_Transformation - Fatal编程技术网

R 将整数(0)替换为NA

R 将整数(0)替换为NA,r,integer,transformation,R,Integer,Transformation,我有一个应用于一列并将结果放入另一列的函数,它有时会将integer(0)作为输出。因此,我的输出列将类似于: 45 64 integer(0) 78 如何检测这些整数(0),并将其替换为NA?是否有类似于Is.na()的东西可以检测到它们 编辑:好的,我想我有一个可复制的例子: df1 <-data.frame(c("267119002","257051033",NA,"267098003","267099020","267047006")) names(df1)[1]<-"I

我有一个应用于一列并将结果放入另一列的函数,它有时会将
integer(0)
作为输出。因此,我的输出列将类似于:

45
64
integer(0)
78
如何检测这些
整数(0)
,并将其替换为
NA
?是否有类似于
Is.na()
的东西可以检测到它们


编辑:好的,我想我有一个可复制的例子:

df1 <-data.frame(c("267119002","257051033",NA,"267098003","267099020","267047006"))
names(df1)[1]<-"ID"

df2 <-data.frame(c("257051033","267098003","267119002","267047006","267099020"))
names(df2)[1]<-"ID"
df2$vals <-c(11,22,33,44,55)

fetcher <-function(x){
  y <- df2$vals[which(match(df2$ID,x)==TRUE)]
return(y) 
}

sapply(df1$ID,function(x) fetcher(x))

我不希望这是一个列表-我想要一个向量,而不是
num(0)
我想要
NA
(注意在这个玩具数据中它给出
num(0)
-在我的真实数据中它给出
(整数(0)
).

最好在函数中这样做,我将其称为
myFunctionForApply
,但这是您当前的函数。返回之前,请检查长度,如果它是0 return
NA

myFunctionForApply <- function(x, ...) {
    # Do your processing
    # Let's say it ends up in variable 'ret':
    if (length(ret) == 0)
        return(NA)
    return(ret)
}
myFunctionForApply这里有一种方法(a)用
NA
替换
integer(0)
,以及(b)将列表转换为向量

# a regular data frame
> dat <- data.frame(x = 1:4)
# add a list including integer(0) as a column
> dat$col <- list(45,
+                 64,
+                 integer(0),
+                 78)
> str(dat)
'data.frame':   4 obs. of  2 variables:
 $ x  : int  1 2 3 4
 $ col:List of 4
  ..$ : num 45
  ..$ : num 64
  ..$ : int 
  ..$ : num 78
# find zero-length values
> idx <- !(sapply(dat$col, length))
# replace these values with NA
> dat$col[idx] <- NA
# transform list to vector
> dat$col <- unlist(dat$col)
# now the data frame contains vector columns only
> str(dat)
'data.frame':   4 obs. of  2 variables:
 $ x  : int  1 2 3 4
 $ col: num  45 64 NA 78
#常规数据帧
>dat dat$col str(dat)
“data.frame”:2个变量中的4个对象:
$x:int 1 2 3 4
$col:4人名单
..$:num 45
..$:num 64
..$:int
..$:num 78
#查找零长度值
>idx dat$col[idx]dat$col str(dat)
“data.frame”:2个变量中的4个对象:
$x:int 1 2 3 4
$col:num 45 64 NA 78

will data.frame$column[data.frame$column==integer(0)]@pepsimax你为什么不把它作为一个答案(可能提供一个工作示例)?数据框不能包含
integer(0)
。请提供一个可复制的示例。或者你可以检查
length()==0
参见:我也有兴趣了解
integer(0)是如何实现的
确实进入了您的输出。但是为了让您继续:要测试对象是否是
整数(0)
您可以做
相同的(对象,整数(0))
。正确。非常不习惯的代码,尽管:
函数(x,…)if(长度(ret)==0)NA else ret
谢谢Ben和Konrad!感谢你们的回答!谢谢Calimo我意识到现在你们回答了,Ben编辑了Hanks Sven!通过将部分问题合并到我的函数中解决了我的问题!非常感谢大家-为可怜的原始问题道歉!
# a regular data frame
> dat <- data.frame(x = 1:4)
# add a list including integer(0) as a column
> dat$col <- list(45,
+                 64,
+                 integer(0),
+                 78)
> str(dat)
'data.frame':   4 obs. of  2 variables:
 $ x  : int  1 2 3 4
 $ col:List of 4
  ..$ : num 45
  ..$ : num 64
  ..$ : int 
  ..$ : num 78
# find zero-length values
> idx <- !(sapply(dat$col, length))
# replace these values with NA
> dat$col[idx] <- NA
# transform list to vector
> dat$col <- unlist(dat$col)
# now the data frame contains vector columns only
> str(dat)
'data.frame':   4 obs. of  2 variables:
 $ x  : int  1 2 3 4
 $ col: num  45 64 NA 78