I have a function that I apply to a column and puts results in another column and it sometimes gives me integer(0)
as output. So my output column will be something like:
45
64
integer(0)
78
How can I detect these integer(0)
's and replace them by NA
? Is there something like is.na()
that will detect them ?
Edit: Ok I think I have a reproducible example:
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))
The output from this sapply
is the source of the problem.
> str(sapply(df1$ID,function(x) fetcher(x)))
List of 6
$ : num 33
$ : num 11
$ : num(0)
$ : num 22
$ : num 55
$ : num 44
I don't want this to be a list - I want a vector, and instead of num(0)
I want NA
(note in this toy data it gives num(0)
- in my real data it gives (integer(0)
).
integer(0)
. Please provide a reproducible example. – Currenlength()==0
see: https://mcmap.net/q/143459/-how-to-catch-integer-0 – Dubinteger(0)
did get into your output. But to get you going: To test if an object isinteger(0)
you could doidentical(object, integer(0))
. – Sore> temp[1,tn1] $<NA> integer(0) > is.na(temp[1,tn1]) <NA> FALSE > class(temp) [1] "data.frame" > class(temp[tn1]) [1] "data.frame" > class(temp[1,tn1]) [1] "list"
– Latreshiastr(temp)
(and tell us whattn1
is ...) – Fauniemerge(df1, df2, all.x = TRUE)
– Cauley