Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
93 views
in R by (178k points)
Can you provide an example of a for loop in R?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

Certainly! Here's an example of a for loop in R that calculates the sum of numbers from 1 to 10:

sum <- 0  # Initialize sum variable

for (i in 1:10) {
  sum <- sum + i  # Add current value of i to the sum
}

print(sum)  # Print the final sum
 

In this example, the for loop iterates over the sequence 1:10, and on each iteration, the value of i is added to the sum variable using the + operator. The loop will execute 10 times, with i taking on the values 1, 2, 3, ..., 10. After the loop completes, the final sum is printed, which in this case will be 55 (1 + 2 + 3 + ... + 10).

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jun 14, 2023 in R by kvdevika (178k points)
0 votes
1 answer
0 votes
1 answer
asked Jun 14, 2023 in R by kvdevika (178k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...