Loop in R: how to save the outputs?
Store each boolean matrix as an item in a list:
result <- vector("list",49)
for (i in 1:49)
{
result[[i]] <- T1>F[i] # I used print to see the results in the screen
}
#Print the first matrix on screen
result[[1]]
How to loop over on different files and save the output with filename in R?
How about putting all your names in a single vector, called names
, like this:
names<-c("TRYFG","RTDFE",...)
and then feeding each one to a function that reads the files, merges them, and returns the rows
f<-function(n) {
fs = paste0(n,c("_filter", "_data"),".txt")
C = merge(
read.delim(fs[1],sep="\t", header=F),
read.delim(fs[2],sep="\t", header=F), by="XYZ")
data.frame(Samples=n,Common=nrow(C))
}
Then just call call this function f
on each of the values in names
, row binding the result together
do.call(rbind, lapply(names, f))
An easy way to create the vector names
is like this:
p = "_(filter|data).txt"
names = unique(gsub(p,"",list.files(pattern = p)))
R: Saving the Results of a Loop
You can use expand.grid
to create all possible combinations of vec1
, vec2
and vec3
and save con$overall[1]
on each iteration in the dataframe.
library(caret)
library(rpart)
#generate data
a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5))
group_1 <- 1:1000
#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)
e <- d
vec1 <- sample(200:300, 5)
vec2 <- sample(400:500,5)
vec3 <- sample(700:800,5)
z <- 0
df <- expand.grid(vec1, vec2, vec3)
df$Accuracy <- NA
for (i in seq_along(vec1)) {
for (j in seq_along(vec2)) {
for (k in seq_along(vec3)) {
# d <- e
d$group_2 = as.integer(ifelse(d$group_1 < vec1[i] , 0, ifelse(d$group_1 >vec1[i] & d$group_1 < vec2[j] , 1, ifelse(d$group_1 >vec2[j] & d$group_1 < vec3[k] , 2,3))))
d$group_2 = as.factor(d$group_2)
fitControl <- trainControl(## 10-fold CV
method = "repeatedcv",
number = 2,
## repeated ten times
repeats = 1)
TreeFit <- train(group_2 ~ ., data = d[,-5],
method = "rpart",
trControl = fitControl)
pred <- predict(
TreeFit,
d[,-5])
con <- confusionMatrix(
d$group_2,
pred)
#update results into table
#final_table[i,j] = con$overall[1]
z <- z + 1
df$Accuracy[z] <- con$overall[1]
}
}
}
head(df)
# Var1 Var2 Var3 Accuracy
#1 300 492 767 0.299
#2 202 492 767 0.299
#3 232 492 767 0.299
#4 293 492 767 0.376
#5 231 492 767 0.299
#6 300 435 767 0.331
How to save the for loop output as data.frame in R?
Here is a solution that is most similar to your code.
The points are using the initialisation (indexing ...[NULL, ]
) and the function rbind()
output <- mtcars[NULL,]
for (i in seq_len(nrow(mtcars))) {
if (i <= 30) {
next
}
# ...
output <- rbind(output, mtcars[i, ])
}
loop to run through multiple datasets and save each output in R
Have you considered changing your code to a function? You can store all data.frames in a named list, then apply the function on each data.frame in the list and then collect the results.
library(tidyverse)
library(broom)
# changing your code to a function
my_function <- function(.df){
nls.mon <- nls(wt~A*(1-exp(k*(t0-age))),
data=.df,
start = list(A=253.6,k=.03348,t0=32.02158))
nls.von <- nls(wt ~A*(1-(1/3)*exp(k*(t0-age)))^3,
data=.df,
start= list(A=253.6,k=.03348,t0=32.02158))
# I slightly edited this part of your code
df <- bind_rows(
tidy(nls.mon) %>% mutate(model = "nls.mon"),
tidy(nls.von) %>% mutate(model = "nls.von")
) %>%
select(model, term, estimate) %>%
pivot_wider(names_from = "term", values_from = "estimate")
return(df)
}
# reading in data, the path to the data needs to be changed
df1 <- read_csv(r"{C:\Users\novot\Downloads\sample.csv}") %>%
select(-1)
df2 <- df1 %>%
filter(sex == "M")
# using map to apply the created function to each member of the list
df_out <- list("df1" = df1, "df2" = df2) %>%
map(
~my_function(.x)
) %>%
bind_rows(.id = "dataframe")
df_out
#> # A tibble: 4 x 5
#> dataframe model A k t0
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 df1 nls.mon 248. 0.0135 2.09
#> 2 df1 nls.von 246. 0.0222 32.9
#> 3 df2 nls.mon 248. 0.0135 2.09
#> 4 df2 nls.von 246. 0.0222 32.9
R: Saving model output from for loop - list is blank
Your problem is (as said by Pkumar in the comments) that you are assigning your loop to your variable. If you for example try running the code segment
x <- list()
for(i in 1:10){
x[[i]] <- rnorm(i)
}
x # List of length 10, containing vectors of length 1:10
x <- for(i in 1:10){
x[[i]] <- rnorm(i)
}
x # NULL
The for-loop
itself does not return a value, it simply iterates. So if you instead ran
Models <- list()
for(i in 1:nlayers(Y_Vals)){
s <- stack(Y_Vals[[i]], r1)
df <- data.frame(na.omit(values(s)))
names(df) <- c("response_var", "pred_var")
m <- glm(response_var~pred_var, data=df, family=binomial)
Models[[i]] <- m
}
you would see the result you expect. Note that iteratively extending a list is inefficient however. So to be more efficient, you could instead start with Models <- vector('list', nlayers(Y_Vals))
, this will increase your performance slightly for larger datasets.
Related Topics
Moving Columns Within a Data.Frame() Without Retyping
How to Coerce a List Object to Type 'Double'
R Ggplot2: Stat_Count() Must Not Be Used with a Y Aesthetic Error in Bar Graph
How to Combine 2 Plots (Ggplot) into One Plot
Writing Robust R Code: Namespaces, Masking and Using the '::' Operator
Error in Installation a R Package
Plotting Pca Biplot with Ggplot2
Lme4::Lmer Reports "Fixed-Effect Model Matrix Is Rank Deficient", Do I Need a Fix and How To
Convert Data Frame with Date Column to Timeseries
In Ggplot2, What Do the End of the Boxplot Lines Represent
How to Document Data Sets with Roxygen
MAC Os X R Error "Ld: Warning: Directory Not Found for Option"
Adding a Company Logo to Shinydashboard Header
How to Extract Just the Number from a Named Number (Without the Name)
How to Programmatically Extract/Unzip a .7Z (7-Zip) File with R