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
On this page of the site you can watch the video online Loop Through Data Frame Columns & Rows in R (4 Examples) | for-, while- & repeat-Loops | Add Values with a duration of hours minute second in good quality, which was uploaded by the user Statistics Globe 14 April 2023, share the link with friends and acquaintances, this video has already been watched 5,842 times on youtube and it was liked by 87 viewers. Enjoy your viewing!