R markdown "argument is of length zero" - r

Code is running fine in R, but getting an "argument is of length zero" error when I run in R Markdown. Just running a loop to split a probability output into 0 or 1 at 50%.
Looked through some similar posts but didn't find anything about issues when moving to markdown.
for (i in 0: (nrow(test)-1)){
i <- i+1
if (test$pred_basemodel[i] < 0.5){
test$pred_basemodel[i] <- 0
}
else {
test$pred_basemodel[i] <- 1
}
}
Thanks for any suggestions!

I think you can remove i <- i+1 in the for loop, since i is the iterator which will run through 1:nrow(test) automatically. Also, you can use 1:nrow(test) instead of 0:(nrow(test)-1) as the for loop condition, since your indexing naturally starts from 1.
You can try the code below
for (i in 1:nrow(test)){
if (test$pred_basemodel[i] < 0.5){
test$pred_basemodel[i] <- 0
}
else {
test$pred_basemodel[i] <- 1
}
}

Related

R for loop returns NULL instead of expected values

i<-c(1:44)
diff_arbeitnehmer <- for(x in i){if(x == 44) {diff_arbeitnehmer[x] <- 0} else{diff_arbeitnehmer[x] <- 100/erwerbstaetige[x,2]*erwerbstaetige[x,4]-100/erwerbstaetige[x+1,2]*erwerbstaetige[x+1,4]}}
My data frame has 44 entriess
I am using R script could someone tell me what could be the reason?
I am lost with this
I can't run your code because I don't have your data frame, but maybe the reason is because you are trying to assing a for loop into the variable diff_arbeitnehmer. I did this change and hope that know it works:
i<-c(1:44)
diff_arbeitnehmer <- c()
for(x in i){
if(x == 44){
diff_arbeitnehmer[x] <- 0
} else{
diff_arbeitnehmer[x] <- 100/erwerbstaetige[x,2]*erwerbstaetige[x,4]-100/erwerbstaetige[x+1,2]*erwerbstaetige[x+1,4]
}
}
An advice is to take a look if the assignment in the last condition is right, maybe you need to put some parenthesis.

Why does this work as a for loop but not within a function?

I'm trying to write a function that identifies if a number within a numerical vector is odd or even. The numerical vector has a length of 1000.
I know that the for loop works fine, and I just wanted to generalize it in the form of a function that takes a vector of any length
out<-vector()
f3<- function(arg){
for(i in 1:length(arg)){
if((arg[i]%%2==0)==TRUE){
out[i]<-1
}else{out[i]<-0
}
}
}
When run within a function, however, it just returns a NULL. Why is that, or what do I need to do to generalize the function work with any numerical vector?
As already mentioned by PKumar in the comments: Your function doesn't return anything, which means, the vector out exists only in the environment of your function.
To change this you can add return(out) to the end of your function. And you should also start your function with creating out before the loop. So your function would look like outlined below.
Note, that I assume you want to pass a vector of a certain length to your function, and get as a result a vector of the same length which contains 1 for even numbers and 0 for odd numbers. f3(c(1,1,2)) would return 0 0 1.
f3 <- function(arg){
out <- vector(length = length(arg), mode = "integer")
for(i in 1:length(arg)){
if((arg[i]%%2==0)==TRUE){ # note that arg[i]%%2==0 will suffice
out[i]<-1
} else {out[i]<-0
}
}
return(out) # calling out without return is enough and more inline with the tidyverse style guide
}
However, as also pointed out by sebastiann in the comments, some_vector %% 2 yields almost the same result. The difference is, that odd numbers yield 1 and even numbers 0. You can also put this into a function and subtract 1 from arg to reverse 0 and 1 :
f3 <- function(arg){
(arg-1) %% 2
}
A few thing to note about your code:
A function must return something
The logical if((arg[i]%%2==0)==TRUE) is redundant. if(arg[i]%%2==0) is enough, but wrong, because arg[i] does not exist.
the length(arg) is the length(1000) which, if ran, returns 1
You should change arg[i] with i and assign to i all the values from 1:1000, as follows:
R
out <-vector()
f3 <- function(arg){
for(i in 1:arg){
if(arg[i] %% 2 == 0){
out[i] <- 1
}
else{
out[i] <- 0
}
}
return(out)
}
f3(1000)

Write a function in R to find perfect numbers

I am a beginner in R and am attempting the following question:
Create a function in R which takes as its
input a natural number N and returns as an output the list of
all perfect numbers between 1 and N.
There are 3 steps here:
1. Check the list of factors
2. Check whether it is a perfect number
3.check from 1 to 10000
factorlist<-function(n){
if(n<2){return("Invalid Input")}
if(n%%1!=0){return("Invalid Input")}
vec<-0
for(i in 1:(n-1)){
if(n%%i==0){
vec[length(vec)]<-i
vec<-c(vec,0)
}
}
vec<-vec[-length(vec)]
return(vec)
}
perfectcheck<-function(n){
if(n-sum(factorlist(n)) ==0) {return("Perfect Number")}
else{return("Not Perfect Number")}
}
perfectcheckN<-function(N){
for(i in 1:N){
if(perfectcheck(i)=="Perfect Number"){
vec[length(vec)]<-i
vec<-c(vec)
}
}
vec<-vec[-length(vec)]
return(vec)
}
and i got the following error for my third step
Error in sum(factorlist(n)) : invalid 'type' (character) of argument
I spent like few hours and still could not figure out my mistake, please help. Thanks!
The output of factorlist(i) is character when i==1.
There's a lot of loops and ifs in your code. You can just do
facs <- function (x) {
x <- as.integer(x)
div <- seq_len(abs(x) - 1L)
div[x%%div == 0L]
}
perfectcheckN <- function(N){
out <- 1:N
out[sapply(out, function(x) x == sum(facs(x)))]
}

R: Object not found within a function

I'm having some issue with my code, whenever I run the code the output is always comes back with an error saying "Object pv not found" can anyone help out? It'd be greatly appreciated!
Code:
lo <- function(x, y)
{
for(i in 1:(y-1))
{
for(j in 1:length(x))
{
First = function (x)
{if (i%%x[j] == 0) {pv <- cumsum(x)}}
}
}
return(pv)
}
lo(6, 20)
The problem is that your inner loop goes from 1:length(x).
You are supplying 20 as x. The length of an object that contains one number is 1. Looping from 1 to 1 means 0 iterations. You never run pv <- cumsum(x) then. So pv isn't defined when you get to the return statement.

How to check if subscript will be out of bounds?

If i want to check the existence of a variable I use
exists("variable")
In a script I am working on I sometimes encounter the problem of a "subscript out of bounds" after running, and then my script stops. In an if statement I would like to be able to check if a subscript will be out of bounds or not. If the outcome is "yes", then execute an alternative peace of the script, and if "not", then just continue the script as it was intended.
In my imagination in case of a list it would look something like:
if {subscriptOutofBounds(listvariable[[number]]) == TRUE) {
## execute this part of the code
}
else {
## execute this part
}
Does something like that exist in R?
You can compare the length of your list with other number. As an illustration, say I have a list with 3 index and want to check by comparing them with a vector of number 1 to 100.
lol <- list(c(1:10),
c(100:200),
c(3:50))
lol
check_out <- function(x) {
maxi <- max(x)
if (maxi > length(lol)) {
#Excecute this part of code
print("Yes")
}
else {
#Excecute this part of code
print("No")
}
}
num <- 1:100
check_out(num)
The biggest number of vector num is 100 and your list only has 3 index (or length =3), so it will be out of bound from your list, then it will return Yes

Resources