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
}
Related
The below R code fills an array of specified dimensions with positive integers generated randomly via a probability vector.
subset.haps <- NULL
haps <- 1:4
num.specs <- 100
probs <- rep(1/4, 4)
perms <- 10000
K <- 1
gen.perms <- function() {
if (is.null(subset.haps)) {
sample(haps, size = num.specs, replace = TRUE, prob = probs)
} else {
resample <- function(x, ...) x[sample.int(length(x), ...)]
resample(subset.haps, size = num.specs, replace = TRUE, prob = probs[subset.haps])
}
}
pop <- array(dim = c(perms, num.specs, K))
for (i in 1:K) {
pop[, , i] <- replicate(perms, gen.perms())
}
However, profiling the above code suggests that improvements can be made.
The 'for' loop can be eliminated using rep()
rep(replicate(perms, gen.perms()), K)
However, this method does not produce an array, nor preserves array dimensions.
Of course, wrapping the above modified code within as.array() will fix the second issue, but the output does not resemble a typical array in structure.
My question
How can I ensure the array structure (i.e., dimensions) is preserved?
You just make pop then set its dim attribute afterwards:
pop <- rep(replicate(perms, gen.perms()), K)
dim(pop) <- c(perms, num.specs, K)
And to prove it:
class(pop)
# [1] "array"
dim(pop)
# [1] 10000 100 1
pop[2020,23,1]
# [1] 2
Can anyone tell me what’s preventing this loop from running?
For each row i, in column 3 of the data frame ‘depth.df’, the loop preforms a mathematical function, using a second data frame, 'linker.df' (it multiplies i by a constant / a value from linker.df which is found by matching the value of i.
If I run the loop for a single instance of i, (lets say its = 50) it runs fine:
cor.depth <- function(depth.df){
result <- seq(from=1, to=(nrow(depth.df)))
x <- 8971
for(i in 1:nrow(depth.df)){
result[i] <- depth.df[i,3]*(x /( linker.df [i,2][ linker.df [i,1] == 50]))
return(result)
}
}
>97,331
but if I run it to loop over each instance of i, it always returns an error:
cor.depth <- function(depth.df){
result <- seq(from=1, to=(nrow(depth.df)))
x <- 8971
for(i in 1:nrow(depth.df)){
result[i] <- depth.df[i,3]*(x /( linker.df [i,2][ linker.df [i,1] %in% depth.df[i,3]]))
return(result)
}
}
Error in result[i] <- depth.df[i, 3] * (all_SC_bins/(depth.ea.bin.all[, :
replacement has length zero
EDIT
Here is a reproducible data set provided to illustrate data structure and issue
#make some data as an example
#make some data as an example
linker.data <- sample(x=40:50, replace = FALSE)
linker.df <- data.frame(
X = linker.data
, Y = sample(x=2000:3000, size = 11, replace = TRUE)
)
depth.df <- data.frame(
X = sample(x=9000:9999, size = 300, replace = TRUE)
, Y = sample(x=c("A","G","T","C"), size = 300, replace = TRUE)
, Z = sample(linker.data, size = 300, replace = TRUE)
)
cor.depth <- function(depth.df){
result <- seq(from=1, to=(nrow(depth.df)))
x <- 8971
for(i in 1:nrow(depth.df)){
result[i] <- depth.df[i,3]*(x /( linker.df [i,2][ linker.df [i,1] %in% depth.df[i,3]]))
return(result)
}
}
Error emerges because denominator returns integer(0) or numeric(0) or a FALSE result on most rows. Your loop attempts to find exact row number, i, where both dataframes' respective X and Z match. Likely, you intended where any of the rows match which would entail using a second, nested loop with an if conditional on matches.
cor.depth <- function(depth.df){
result <- seq(from=1, to=(nrow(depth.df)))
x <- 8971
for(i in 1:nrow(depth.df)){
for (j in 1:nrow(linker.df)){
if (linker.df[j,1] == depth.df[i,3]) {
result[i] <- depth.df[i,3]*(x /( linker.df[j,2]))
}
}
}
return(result)
}
Nonetheless, consider merge a more efficient, vectorized approach which matches any rows between both sets on ids. The setNames below renames columns to avoid duplicate headers:
mdf <- merge(setNames(linker.df, paste0(names(linker.df), "_l")),
setNames(depth.df, paste0(names(depth.df), "_d")),
by.x="X_l", by.y="Z_d")
mdf$result <- mdf$X_l * (8971 / mdf$Y_l)
And as comparison, the two approaches would be equivalent:
depth.df$result <- cor.depth(depth.df)
depth.df <- with(depth.df, depth.df[order(Z),]) # ORDER BY Z
mdf <- with(mdf, mdf[order(X_l),]) # ORDER BY X_L
all.equal(depth.df$result, mdf$result)
# [1] TRUE
i have a issue related to list()
i need to use 3 for loops to process data just like this
for(i in 1:10){ #it is number of data group
for(j in 1:5){ #it is number of sub-data group
for(k in 1:2){ #it is number of sub-data group
1.data process<br>
2.just want to make a list within information of i, j ,k
mylist[i][j][k]
#i tested some methods like this, but it does not work well<br>
}
}
}
Could you give me any ideas or solutions for this issue?
Thank you
It's unclear exactly what is necessary, but a multi-dimensionsal collection of non-atomic objects could be done like this:
lst <- lapply(1:10, function(i) {
lapply(1:5, function(k) {
lapply(1:2, function(k) {
# do something here
lm(mpg ~ cyl, data = mtcars)
})
})
})
Now to access the [i,j,k]th element, for example to get the summary of the linear model just made:
i <- 3; j <- 2; k <- 1
summary(lst[[i]][[j]][[k]])
If you absolutely must create it with for loops (not recommended), it's recommended that you pre-populate an empty structure and fill in the holes:
lst <- replicate(10, replicate(5, replicate(2, NULL, simplify = FALSE), simplify = FALSE), simplify = FALSE)
for (i in 1:10) {
for (j in 1:5) {
for (k in 1:2) {
lst[[i]][[j]][[k]] <- ...
}
}
}
I have a set of data, with it's own metadata. I get some of the columns to list all the data from the given set of data.
Then I use this loops to store it in a matrix (I tried a data.frame and a list, but didn't work either). The entries are strings.
#############
ii_c <- metadades$item_id[metadades$tipus_item == "comentari"]
g_c <- metadades$grup[metadades$tipus_item == "comentari"]
i_c <- metadades$item[metadades$tipus_item == "comentari"]
in_c <- data_ent[, ii_c]
c_l <- list()
for(i in 1:ncol(in_c)){
c_l[[i]] <- in_c[,i][!is.na(in_c[,i])]
}
j <- 0
l <- 0
c_cl <- matrix(ncol=3)
for(i in 1:ncol(in_c)){
if(mode(c_l[[i]])=="numeric"){
j=j+1
} else {
for(k in 1:length(c_l[i])){
c_cl[i-j+l,] = c(g_c[i],i_c[i],c_l[i][k])
l=l+1
}
}
}
df_cl <- as.data.frame(c_cl)
#############
This way afterwards I would be able to plot it. Nevertheless I've tried to list (instead of making a matrix) all the dataframes and later on I could be able to cbind them (but it gave me errors aswell).
The next step would be to do a tableGrob and a grid.draw, to print it in a report.
Got the solution from my workmate,,
df_comentaris <- data.frame(grup=NA, item=NA, comentari =NA)
for (i in metadades$item_id[metadades$tipus_item=='comentari']) {
comentaris <- dades[!is.na(dades[i]),i]
grup <- metadades$grup[metadades$item_id == i]
item <- metadades$item[metadades$item_id == i]
df_aux <- data.frame(grup=rep(grup,length(comentaris)), item=rep(item,length(comentaris)), comentari=comentaris)
df_comentaris <- rbind(df_comentaris, df_aux)
}
df_comentaris <- df_comentaris[2:nrow(df_comentaris),]
I'm to get the vector simualted_results to take values returned by "simulation," which produced a vector of varying length depending on the iteration.
Initially I have this code which works, but is very slow:
simulated_results<-NULL
while(as.numeric(Sys.time())-start<duration){
simulated_results <- cbind(simulated_results,simulation(J,4* (length(J)^2),0.0007,duration,start))
}
But its very slow so I modified it:
start<-as.numeric(Sys.time())
duration<-10
simulated_results<-NULL
simulated_results <- cbind(simulated_results,
replicate(n=10000,expr=(while(as.numeric(Sys.time())-start<duration)
{simulation(J,4*(length(J)^2),0.0007,duration,start)})))
Now with the new code, my problem is that despite everything running, I cant get the results of simulation to be passed to simualted_results, instead simualted_results jsut takes on a column vector of NULL values
I get no error messages
I would greatly appreciate any help!!
for reference the simulation code is:
iter<-as.numeric(Sys.getenv("PBS_ARRAY_INDEX"))
if(iter <= 40){J<-1:500
}else if(iter <= 80){J<-1:1500
}else if(iter <= 120){J<-1:2500
}else if(iter <= 160){J<-1:5000}
set.seed(iter)
simulation <- function(J,gens,v=0.1,duration,start){
species_richness <- function(J){
a <- table(J)
return(NROW(a))
}
start<-as.numeric(Sys.time())
species_richness_output <- rep(NA,gens)
for(rep in 1:gens){
if (as.numeric(Sys.time())-start<duration){
index1 <- sample(1:length(J),1)
if(runif(1,0,1) < v){
J[index1] <- (rep+100)
}
else{
index2 <- sample(1:length(J),1)
while(index1==index2) {
index2 <- sample(1:length(J),1)
}
J[index1] <- J[index2]
}
species_richness_output[rep] <- species_richness(J)} else break
}
species_abundance <- function(J){
a <- table(J)
return(a)
}
abuntable <- species_abundance(J)
octaves <- function(abuntable)
{
oct<-rep(0,floor(log2(length(J))+1))
for(i in 1:length(abuntable)){
oct2 <- floor(log2(abuntable[i])+1)
oct[oct2] <- oct[oct2]+1
}
return(oct)
}
octaves(abuntable)
}
I agree with #Nathan G, but something did catch my attention: You are trying to cbind two things that cannot be bound together, since they have different dimensions. We don't know what kind of data type your simulation function returns, but it clearly is not NULL. Consider this:
df1 <- NULL
df2 <- data.frame(x = 1:10, y = 11:20)
cbind(df1, df2)
cbind(df2, df1)
Both cbind statements give errors. Do you get an error? If this is what's going on, you should initialize simulated_results not as NULL but as an empty version of whatever the function simulation returns.
EDIT
iter = 10
set.seed(iter)
J <- 1:1500
# critical to preallocate the list size for speed
res <- vector("list", iter)
for (i in 1: iter) {
res[[i]] <- simulation(J,4* (length(J)^2),0.0007,duration = 10,start)
}
str(res)
res[[1]]
Now I don't think I'm using this quite the way you ultimately intend, but perhaps this will give you enough to get to what you actually want.