I wrote three functions where two of the functions (namely function1 and function2) is used in the other function of populatedataset.
I was running the function populatedataset when i got an error of Error in if (num == 1) { : argument is of length zero
I believe that it is due to function2 due to the 'if (num == 1) {: ' portion.
function2 <- function(data, table, dict) {
personindex <- substr(deparse(substitute(data)), start = 1, stop = 2)
num <- table[person == as.character(personindex)]$newpersonality
if (num == 1) {
proptable <- data %>% inner_join(dict[score == 1]) %>% count(word)
proportion <- sum(proptable$n)/nrow(data)
return(proportion)
}
else {
proptable <- data %>% inner_join(dict[score == 0]) %>% count(word)
proportion <- sum(proptable$n/nrow(data))
return(proportion)
}
}
populatedataset <- function(data, table, dict) {
list_a <- c(function1(data, dict), function2(data, table, dict))
return (list_a)
}
I have been reading up on this error on other pages but I can't seem to find a solution related to this problem.
I would greatly appreciate any insight into this error!
A if
condition has to be either TRUE
or FALSE
. This error implicates that num == 1
evaluates to a logical(0)
. This is probably caused by num
being empty, i.e. numeric(0)
, because then you are comparing a numeric value of length 0 to 1 which gives a logical of length 0. You can wrap your condition num == 1
with the isTRUE
function which would turn the logical(0)
into a FALSE
:
if (isTRUE(num == 1)){....
The function isTRUE
checks if the argument is the logical value TRUE
. Since num == 1
is logical(0)
in this case, isTRUE
will return FALSE
and the if
works as inteded.
On sidenote: num
being numeric(0)
is probably being caused by the fact that person == as.character(personindex)
is not TRUE
for any person so if you index your table no newpersonality
value is returned. In this case you would run into the else
portion of your if
-else
-construct if you use my solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With