Home > Enterprise >  Create a "for" loop for columns and rows in R
Create a "for" loop for columns and rows in R

Time:12-21

I have this dataframe:

a <- c(2,5,90,77,56,65,85,75,12,24,52,32)
b <- c(45,78,98,55,63,12,23,38,75,68,99,73)
c <- c(77,85,3,22,4,69,86,39,78,36,96,11)
d <- c(52,68,4,25,79,120,97,20,7,19,37,67)
e <- c(14,73,91,87,94,38,1,685,47,102,666,74)

df <- data.frame(a,b,c,d,e)

I need to convert the following script in a loop, in R: the variable "f" indicates a column of my dataframe ("df") and I need that it ranges from 1 to 5 (that is the number of columns in my dataframe df) in the loop. Also, the loop must consider three rows at a time. In that way, in each loop, running the script I will found a "cdf_min" for each "f" considerated.

Script:

f <- 1
x <- (df[1:3,f])
y <- (df[1:3,-f])
dif_2 <- (x - y)^2
summ <- colSums(dif_2)
summa <- t(as.matrix(summ))
cmin <- which(summa == apply(summa,1,min))
cdf_min <- 
  if (f <= cmin){
    cmin 1
  } else{cmin}

I hope I was clear. Thanks everyone for helping me!

CodePudding user response:

To create the for loop, you could iterate f over the sequence over the columns, and also use it as indices of the results vector cdf_min[f].

cdf_min <- rep(NA, ncol(df))  ## initialize results vector

for (f in seq(ncol(df))) {
  x <- df[1:3, f]
  y <- df[1:3, -f]
  dif_2 <- (x - y)^2
  summ <- colSums(dif_2)
  summa <- t(as.matrix(summ))
  cmin <- which(summa == apply(summa, 1, min))
  cdf_min[f] <- 
    if (f <= cmin) {
      cmin   1
    } else { 
      cmin
    }
}

cdf_min
# [1] 5 5 4 3 2
  • Related