Suppose you're asked to write a for loop in R that prints the squares of the numbers 1 to 10. How would you do it?
- for (i in 1:10) { print(i^2) }
- for (i in 1:10) { print(i * i) }
- for (i in 1:10) { print(square(i)) }
- for (i in 1:10) { print(pow(i, 2)) }
To print the squares of the numbers 1 to 10, you can use the for loop for (i in 1:10) { print(i^2) }. It iterates through the values 1 to 10, calculates the square of each value, and prints the result.
Loading...
Related Quiz
- How does R handle default and missing arguments in functions?
- Imagine you're working with a data set in R that contains missing values. How would you handle the missing values in your statistical analysis?
- How do you determine the length of a string in R?
- In R, the ______ function can be used to list all the variables in the global environment.
- In R, a basic scatter plot is created using the ______ function.