How to loop over the variables and rows of a data matrix in the R programming language. More details: https://statisticsglobe.com/loop-thro...
R code of this video:
data <- data.frame(x1 = 1:5, # Create example data
x2 = 6:10,
x3 = 11:15)
data # Print example data
data1 <- data # Replicate example data
for(i in 1:ncol(data1)) { # for-loop over columns
data1[ , i] <- data1[ , i] + 10
}
data1 # Print updated data
data2 <- data # Replicate example data
for(i in 1:nrow(data2)) { # for-loop over rows
data2[i, ] <- data2[i, ] - 100
}
data2 # Print updated data
data3 <- data # Replicate example data
i <- 1 # Create running index
while(i <=2) { # Start while-loop
data3[ , i] <- data3[ , i] + 100
i <- i + 1
}
data3 # Print updated data
data4 <- data # Replicate example data
i <- 1 # Create running index
repeat{ # Start repeat-loop
data4[ , i] <- data4[ , i] + 100
i <- i + 1
if(i > 2) {
break
}
}
data4 # Print updated data
Follow me on Social Media:
Facebook – Statistics Globe Page: / statisticsglobecom
Facebook – R Programming Group for Discussions & Questions: / statisticsglobe
Facebook – Python Programming Group for Discussions & Questions: / statisticsglobepython
LinkedIn – Statistics Globe Page: / statisticsglobe
LinkedIn – R Programming Group for Discussions & Questions: / 12555223
LinkedIn – Python Programming Group for Discussions & Questions: / 12673534
Twitter: / joachimschork
Music by bensound.com