R creating function with For and While - r

I am trying to create a function in which i can calculate the sum of na's of each column.I tried to use for and while arguments but the result i get is always 0.This is what i tried:
sum_na <- function(x){
for (i in 1:ncol(x)){
while (i<ncol(x)){
u=sum(is.na(x[i]))
return(u)
i=i+1
}
}
}
Any suggestion to a noob like me would be appreciated!

Related

How can I store the value of each iteration of a FOR loop into a vector in R?

I need to store the variable "result" into a vector. I have been trying for days. Still can't get through it
for(i in 1:10)
{
avgcol<-c(q[i])
numofNA<-sum(is.na(clnvar[i,]))
monthsnoNA<-31-numofNA
result=avgcol/monthsnoNA
newvar<-c(result,i)
}
I Would like to have a vector with all the values gotten from the iterations of the for loop. The solution I tried won't work. It does not create a vector. What am I doing wrong?
Initialize the result outside and then assign based on the index
result <- numeric(10)
for(i in 1:10)
{
avgcol <-c(q[i])
numofNA <- sum(is.na(clnvar[i,]))
monthsnoNA<-31-numofNA
result[i] <- avgcol/monthsnoNA
}

R: Build custom cumsum function in sapply

I'm trying to build a more custom version of cumsum to use on a data.table, but I'm failing at the first step:
numbers <- data.table(num=1:10)
sum <- 0
cumFunct <- function(n) {
sum <<- sum+n
return(sum)
}
numbers[, cum:=sapply(num, cumFunct)]
While this works, it is very unclean. It also requires sum to be set to 0 before I run the function.
Now, how do I write this in a cleaner way? Essentially, how can I pass the intermediate result to the next iteration of cumFunct without using global variables?
Thanks very much!
One way to do this would be to use the datatable "numbers" within the function:
numbers <- data.table(num=1:10)
cumFunct <- function(n) {
sum <- sum(numbers[1:n])
return(sum)
}
numbers[, cum:=sapply(num, cumFunct)]
This is not the most efficient way, but depending on what you do in your custom code, one can improve it.
An answer that is also a question: is this a pattern that will work here?
complicated.wizardry <- function(a,b){
a+b
}
cumlist <- function(sofar, remaining, myfn){
if(length(remaining)==1)return(c(sofar, myfn(sofar[length(sofar)],remaining[1])))
return ( cumlist( c(sofar, myfn(sofar[length(sofar)],remaining[1])),remaining[2:length(remaining)],myfn))
}
cumlist(0,1:10,complicated.wizardry)

how to print product of vector in R

So this is my current code:
vec_prod <- function(x){
out <- 1
for(i in 1:length(x)){
out <- out*x[i]
}
out
}
however, i want to print out the product of vector [2,3,5]
but it does not accept those values. I can only input (1:3) or (1:4)
I'm new to R programming so any help is appreciated. I do not want to use any other functions.
Issue wasn't in function code, but probably in the way user called it. I propose my cleaner version. But you should really just use prod.
vec_prod <- function(x){
out <- 1
for(i in x){
out <- out*i
}
out
}
vex_prod(c(4,5,6))

R - How can I calculate a matrix with a function?

i have a problem with following code in R. I want myfun() function to write some data into the my.res matrix. I can print the statement my.vec[i]/my.vec[i-j] easily.
my.vec <-c(1:10)
my.res <-matrix( ,10,2)
myfun <-function(j=2){
for(i in (j+1):10){
my.res[i,1] <-my.vec[i]/my.vec[i-j]
print(my.vec[i]/my.vec[i-j])
}
}
You have your print in the wrong location, also changed your input j=2 to simply j that can be assigned. As mentioned - you don't need a loop to do this - but seems like you are just trying to learn how to work with loops?
myfun <- function(j){
for(i in (j + 1):10){
my.res[i,1] <- my.vec[i]/my.vec[i-j]
}
print(my.res)
}
myfun(2)

Filling matrix in R without for loops

I am a beginner in R and i know the way i have done is wrong and slow. I would like to fill a matrix and i need to compute each term. I have tried two for loops, here is the code. Do you know a better way to do it?
KernelGaussianMatrix <- function(x,delta){
Mat = matrix(0,nrow=length(x),ncol=length(x))
for (i in 1:length(x)){
for (j in 1:length(x)){
Mat[i,j] = KernelGaussian(x[i],x[j],delta)
}
}
return(Mat)
}
Thx
you want to use the function outer as in:
Mat <- outer(x,x,KernelGaussian,delta)
note that any arguments after the third argument in outer are provided as additional arguments to the function provided as the third argument to outer
If a for loop is required to generate the values than your method is fine.
If the values are already in an array values you can try mat = matrix(values, nrow=n, ncol=p) or something similar.

Resources