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)
Related
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
}
I am trying to create a vector or list of values based on the output of a function performed on individual elements of a column.
library(hpoPlot)
xyz_hpo <- c("HP:0003698", "HP:0007082", "HP:0006956")
getallancs <- function(hpo_col) {
for (i in 1:length(hpo_col)) {
anc <- get.ancestors(hpo.terms, hpo_col[i])
output <- list()
output[[length(anc) + 1]] <- append(output, anc)
}
return(anc)
}
all_ancs <- getallancs(xyz_hpo)
get.ancestors outputs a character vector of variable length depending on each term. How can I loop through hpo_col adding the length of each ancs vector to the output vector?
Welcome to Stack Overflow :) Great job on providing a minimal reproducible example!
As mentioned in the comments, you need to move the output <- list() outside of your for loop, and return it after the loop. At present it is being reset for each iteration of the loop, which is not what you want. I also think you want to return a vector rather than a list, so I have changed the type of output.
Also, in your original question, you say that you want to return the length of each anc vector in the loop, so I have changed the function to output the length of each iteration, rather than the whole vector.
getallancs <- function(hpo_col) {
output <- numeric()
for (i in 1:length(hpo_col)) {
anc <- get.ancestors(hpo.terms, hpo_col[i])
output <- append(output, length(anc))
}
return(output)
}
If you are only doing this for a few cases, such as your example, this approach will be fine, however, this paradigm is typically quite slow in R and it's better to try and vectorise this style of calculation if possible. This is especially important if you are running this for a large number of elements where computation will take more than a few seconds.
For example, one way the function above could be vectorised is like so:
all_ancs <- sapply(xyz_hpo, function(x) length(get.ancestors(hpo.terms, x)))
If in fact you did mean to output the whole vector of anc, not just the lengths, the original function would look like this:
getallancs <- function(hpo_col) {
output <- character()
for (i in 1:length(hpo_col)) {
anc <- get.ancestors(hpo.terms, hpo_col[i])
output <- c(output, anc)
}
return(output)
}
Or a vectorised version could be
all_ancs <- unlist(lapply(xyz_hpo, function(x) get.ancestors(hpo.terms, x)))
Hope that helps. If it solves your problem, please mark this as the answer.
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!
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))
I am using the extract function in a loop. See below.
for (i in 1:length(list_shp_Tanzania)){
LU_Mod2000<- extract(x=rc_Mod2000_LC, y=list_shp_Tanzania[[i]], fun=maj)
}
Where maj function is:
maj <- function(x){
y <- as.numeric(names(which.max(table(x))))
return(y)
}
I was expecting to get i outputs, but I get only one output once the loop is done. Somebody knows what I am doing wrong. Thanks.
One solution in this kind of situation is to create a list and then assign the result of each iteration to the corresponding element of the list:
LU_Mod2000 <- vector("list", length(list_shp_Tanzania))
for (i in 1:length(list_shp_Tanzania)){
LU_Mod2000[[i]] <- extract(x=rc_Mod2000_LC, y=list_shp_Tanzania[[i]], fun=maj)
}
Do not do
LU_Mod2000 <- c(LU_Mod2000, extract(x=rc_Mod2000_LC, y=list_shp_Tanzania[[i]], fun=maj))
inside the loop. This will create unnecessary copies and will take long to run. Use the list method, and after the loop, convert the list of results to the desired format (usually using do.call(LU_Mod2000, <some function>))
Alternatively, you could substitute the for loop with lapply, which is what many people seem to prefer
LU_Mod2000 <- lapply(list_shp_Tanzania, function(z) extract(x=rc_Mod2000_LC, y=z, fun=maj))