Is there any way to obtain the names of functions from a list? For example is possible to do something like this?
sqrt <- function(x){
squared <- (x*x)
}
divide <- function(x){
half <- (x/2)
}
funclist <- list(sqrt, divide)
for(i in seq_along(funclist)){
printnameoffunction(funclist[[i]])
}
Related
I made a user defined function...
From a vector x, y, f(x,y) returns list of (x,y,z)...
Now I want to do iterations of
data1 <- f(x,y)
data2 <- f(data1$x, data1$y)
data3 <- f(data2$x, data2$y)
data4 <- f(data3$x, data3$y)
and so on...
Is there a way to make a loop for this?
I tried to use paste function
data1 <- f(x,y)
for (i = 2:10) {
assign(paste("data",i,sep=""), f(paste("data",i-1,"$x",sep=""), paste("data",i-1,"$y",sep=""))
}
but it gets error since input becomes "data1$x" which is string not numeric.
As Vincent just replied you can make a list, and a list of lists etc. This will make it easier to produce what you want.
I made an example for you:
x <- 1:10; y <- 11:20
f <- function(x, y) {return(list(x = x+1, y = y+1))}
data <- c()
data[[1]] <- f(x, y)
for(i in 2:10){
data[[i]] <- f(data[[i-1]]$x, data[[i-1]]$y)
}
You can then get x from time i with data[[i]]$x.
I have the a dataset, containing about 4000 matrices in vector form, each of them should be named by the date it was created.
Right now I have the following:
dates <- unique(rcov_matrix$dateid)
for(k in dates){
k <- matrix(0, 30, 30)
for(i in 1:30){
for (j in 1:i){
number <- number + 1
value <- rcov_matrix[1, number]
k[i,j] <- value
k[j,i] <- value
}
}
}
The code correctly assigns the entries of the vector to the matrix, but I only end up with one matrix named k in the end.
I understand that this is because of the way variable names are assigned in R, but I could not find a viable solution for my problem in similar posts.
assign(k, matrix(0, 30, 30))
does not work because I have to reuse the variable name later in the next for loop.
How can I solve this? Or is there a more effective way to assign my values to the matrices?
Thank you.
Maybe the simplest is to use assign at the end of the loop, rather than the start.
for (k in dates){
This_k <- matrix(0, 30, 30)
for(i in 1:30){
for (j in 1:i){
value <- rnorm(1) # I use rnorm here to make the example reproducible
This_k[i,j] <- value
This_k[j,i] <- value
}
}
assign(k, This_k)
}
Alternatively (and perhaps a little more efficient), you could put your matrices in a list and use list indexing:
klist = lapply(rep(0, length(dates)), matrix, 30, 30)
names(klist) = dates
for (k in dates){
for(i in 1:30){
for (j in 1:i){
value <- rnorm(1)
klist[[k]][i,j] <- value
klist[[k]][j,i] <- value
}
}
}
I am trying to fill a vector pred_pos with the result pred on each iteration of the for loop. However, my pred_pos vector is never filled. The my_vec object is a list of large character vectors which I don't believe needs to be reproduced for this problem as it is most likely a fundamental indexing error. I just need to know how to populate a vector from this for loop. I can't seem to work out a solution.
pred_pos <- vector("numeric" , 2)
for(i in my_vec) {
for(r in pred_pos) {
inserts <- sapply(i, function(n) { n <- cond_probs_neg[n] } )
pred <- sum(unlist(inserts) , na.rm = T) * apriori_neg
pred_pos[r] <- pred
}
}
Assuming that the rest of your code works, there is no need to explicitly state:
pred_pos <- vector("numeric" , 2)
That creates a numeric vector of length two. You ought to be able to write:
pred_pos <- vector()
Now when you wish to append to the vector you can simply use:
vector[length(vector)+1] <- someData
I believe your code should work if it is adjusted:
pred_pos <- vector()
for(i in my_vec) {
inserts <- sapply(i, function(n) { n <- cond_probs_neg[n] } )
pred <- sum(unlist(inserts) , na.rm = T) * apriori_neg
pred_pos[length(pred_pos)+1] <- pred
}
I'm trying to write a function such as to obtain a test statistic for a vector of size n over 10 simulations. I wrote the following code but I'm not getting the result I need, how can I fix this?
skw=function(n,nsims){
t=numeric(nsims)
for (i in 1:nsims) {
x=rnorm(n)
t[i]=skwness(x)
zscore=t/(6/n)
return(zscore)
}
}
where:
skwness=function(x){
n=length(x)
skew.stat=(1/(n))*(1/(sd(x)^3))*(sum((x-mean(x))^3))
return(skew.stat)
}
Thanks!
You have a couple of issues. The major one is that return should be outside the for loop. Also, you should define t and zscore as vectors, and x as a list.
I think this will work.
Side note: t seems unnecessary in this function. You could get away with using
zscore[i] <- skwness(x[[i]])/(6/n) and get rid of t all together
skwness <- function(x){
n <- length(x)
skew.stat <- (1/(n))*(1/(sd(x)^3))*(sum((x-mean(x))^3))
return(skew.stat)
}
skw <- function(n, nsims){
t <- zscore <- numeric(nsims)
x <- vector("list", nsims)
for (i in 1:nsims) {
x[[i]] <- rnorm(n)
t[i] <- skwness(x[[i]])
zscore[i] <- t[i]/(6/n)
}
return(zscore)
}
Giving it a go:
> x <- rnorm(100)
> skwness(x)
[1] 0.2332121
> skw(100, 10)
[1] 0.6643582 -1.6963196 -2.9192317 -2.7166170 4.9255001 0.0773482 3.9171435
[8] -3.3993994 -2.4258642 0.7075989
I have a function myF(g,m,alpha,gam,theta,beta). Which returns three estimates of parameters. I want to iterate this function for (i in 1:10). How can i do this it in R?
myF <- function(g,m,alpha,gam,theta,beta){
dat <- sim.data(g,m,alpha,gam,theta,beta)
time <- dat$times
delta <- dat$cens
i <- dat$group
X1<-dat$cov #cov~rbinom
n <- length(levels(as.factor(i)))
di <- aggregate(delta,by=list(i),FUN=sum)[,2]
D <- sum(di)
loglik <- function(par){
.........................................
return(-lik)
}
initial=c(0.5,0.5,-0.5,0.5)
maxF <- nlm(loglik, initial)
return(c(theta=exp(maxF$estimate[2]),beta1=maxF$estimate[3],alpha=exp(maxF$estimate[2])))
}
This can easily be done using replicate:
replicate(10, myF(g,m,alpha,gam,theta,beta))
This will create a 3*10 matrix of the parameter estimates, where each column is the result of a separate iteration.