How to Start a for Loop in R Programming

How to create a for loop in R?

This isn't a loop, but it is a bit more R-centric:

N <- 1000 ## number of simulations to run
## Make this reproducible by seeding the random number generator
set.seed(1)
## read ?sample to see how this works
## Basically, sampling accept/not accept with 0.15/0.85 probability,
## N (1000) times for each of three Girls
df <- data.frame(Girl1 = sample(c(TRUE,FALSE), N, replace = TRUE,
prob = c(0.15,0.85)),
Girl2 = sample(c(TRUE,FALSE), N, replace = TRUE,
prob = c(0.15,0.85)),
Girl3 = sample(c(TRUE,FALSE), N, replace = TRUE,
prob = c(0.15,0.85)))
## Show some of the data
head(df)
## the row sums tell us how many accepts you'd get, 1, 2, or 3
outcomes <- rowSums(df)
## We want the rows with 1 acceptance **and** where Girl3 == TRUE
wanted <- with(df, which(outcomes == 1L & Girl3))
## This gives us the simulation probability
length(wanted) / N

Sorry it isn't a loop - but you can try to do it in a loop using the above for guidance. Can't have us doing all the work.

How to expand loop n times in R programming?

In a word, recursion:

#' n: length of each combination string
#' basis: the starting vector of characters to combine
#' extras: the vector of characters to be combined with basis. Defaults to basis
#' i: The current depth of recursion. Users should generally not need to access
#' this parameter.
combine <- function(n, basis=c('A','B','C','D'), extras=basis, i=1) {
x <- expand.grid(basis, extras)
y <- paste0(x$Var1, x$Var2)
if (i == n-1) {
return(y)
} else {
return(combine(n, y, extras, i+1))
}
}

Giving, for example,

> combine(2)
[1] "AA" "BA" "CA" "DA" "AB" "BB" "CB" "DB" "AC" "BC" "CC" "DC" "AD" "BD" "CD" "DD"

and

> combine(3)
[1] "AAA" "BAA" "CAA" "DAA" "ABA" "BBA" "CBA" "DBA" "ACA" "BCA" "CCA" "DCA" "ADA" "BDA" "CDA" "DDA" "AAB" "BAB" "CAB" "DAB" "ABB" "BBB" "CBB" "DBB" "ACB" "BCB" "CCB"
[28] "DCB" "ADB" "BDB" "CDB" "DDB" "AAC" "BAC" "CAC" "DAC" "ABC" "BBC" "CBC" "DBC" "ACC" "BCC" "CCC" "DCC" "ADC" "BDC" "CDC" "DDC" "AAD" "BAD" "CAD" "DAD" "ABD" "BBD"
[55] "CBD" "DBD" "ACD" "BCD" "CCD" "DCD" "ADD" "BDD" "CDD" "DDD"

etc.

Feel free to sort the output if another order is more desirable.

How to perform a for-loop for a variable in data.fame?

This particular operation would be possible without loop.

df = mtcars
df$cyl[df$cyl == 4] = "low" #Subset the cyl values equal to 4 and assign 'low'
#Repeat for other values

But for running loop, I would go about like this

df = mtcars

for (i in 1:length(df$cyl)) { #Iterate over the length of df$cyl
#You could also do "for (i in seq_along(df$cyl)"
#Run "seq_along(df$cyl)" and "1:length(df$cyl)" to understand what values are being generated
if (df$cyl[i] == 4){ #Index df$cyl by [i]. If the ith value is 4, assign 'low'
df$cyl[i] = "low"
}
if (df$cyl[i] == 6) {
df$cyl[i] = "medium"
}
if (df$cyl[i] == 8) {
df$cyl[i] = "high"
}
}

R programming (beginner): using a for loop, only gave me back the last result from the list

Got it! This returns the dataframe.

all_stas <- list()
for(i in vid_id){
stas <- get_stats(video_id = i)
all_stas <- rbind(all_stas,stas)
}
View(all_stas)
df <- data.frame(all_stas)

Thank you all for the help :)



Related Topics



Leave a reply



Submit