简体   繁体   中英

Create a "for" loop for columns and rows in R

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!

To create the for loop, you could iterate f over the seq uence 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM