Example 1, Here is an example of an R program that adds two vectors and prints the result:
1 2 3 4 5 6 7 8 9 10 11 | # define the vectors v1 <- c(1, 2, 3, 4) v2 <- c(5, 6, 7, 8) # add the vectors v3 <- v1 + v2 # print the result print(v3) |
The output of this program would be:
1 2 3 | [1] 6 8 10 12 |
This program defines two vectors, v1
and v2
, and assigns them the values 1, 2, 3, 4
and 5, 6, 7, 8
, respectively. It then adds the vectors using the +
operator and stores the result in a new vector v3
. Finally, it prints the contents of v3
to the console using the print()
function.
Example 2, Here is an example of an R program that prompts the user to enter two vectors and then adds them:
1 2 3 4 5 6 7 8 9 10 11 | # read the vectors from the user v1 <- as.numeric(readline(prompt = "Enter the first vector (separate elements with spaces): ")) v2 <- as.numeric(readline(prompt = "Enter the second vector (separate elements with spaces): ")) # add the vectors v3 <- v1 + v2 # print the result print(v3) |
This program reads the vectors from the user using the readline()
function and stores them in the variables v1
and v2
. It then converts the strings that are read from the user into numeric vectors using the as.numeric()
function.
Next, it adds the vectors using the +
operator and stores the result in a new vector v3
. Finally, it prints the contents of v3
to the console using the print()
function.
Here is an example of what the output of this program might look like:
1 2 3 4 5 | Enter the first vector (separate elements with spaces): 1 2 3 4 Enter the second vector (separate elements with spaces): 5 6 7 8 [1] 6 8 10 12 |
Example 3, Here is an example of an R program that reads two vectors from a file and then adds them:
1 2 3 4 5 6 7 8 9 10 11 | # read the vectors from the file v1 <- scan(file = "vector1.txt") v2 <- scan(file = "vector2.txt") # add the vectors v3 <- v1 + v2 # print the result print(v3) |
This program reads the vectors from the files “vector1.txt” and “vector2.txt” using the scan()
function and stores them in the variables v1
and v2
, respectively.
Next, it adds the vectors using the +
operator and stores the result in a new vector v3
. Finally, it prints the contents of v3
to the console using the print()
function.
The vectors in the input files should be formatted as one element per line, like this:
1 2 3 4 5 6 | 1 2 3 4 |
Example 4, It is not possible to directly mix R and Python code in a single program. However, it is possible to call R code from within a Python program using the rpy2
library.
Here is an example of a Python program that calls an R function to add two vectors:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import rpy2.robjects as robjects # define the vectors in R v1 = robjects.IntVector([1, 2, 3, 4]) v2 = robjects.IntVector([5, 6, 7, 8]) # call the R function to add the vectors v3 = robjects.r['v1+v2'] # print the result print(v3) |
This program uses the rpy2
library to call an R function to add the vectors v1
and v2
, which are defined in Python using the IntVector
class from rpy2
. The result of the addition is stored in the variable v3
, which is also defined in Python. Finally, the program prints the contents of v3
to the console.
The output of this program would be:
1 2 3 | [6, 8, 10, 12] |