Big_Data_Slip_11

Slip No.7

1) Write an R program to create a Dataframes which contain details of 5 employees and display the details in ascending order.

1. Open RStudio.

2. Create a New Script:
Go to File > New File > R Script, or press Ctrl + Shift + N (Windows).

3. Type these code in the Script:

    # Create two vectors of different lengths
    vector1 <- c(1, 2, 3, 4, 5, 6)
    vector2 <- c(7, 8, 9, 10)

    # Determine the lengths of the vectors
    len1 <- length(vector1)
    len2 <- length(vector2)

    # Find the length of the longer vector
    max_len <- max(len1, len2)

    # Repeat the shorter vector to match the length of the longer vector
    vector1 <- rep(vector1, length.out = max_len)
    vector2 <- rep(vector2, length.out = max_len)

    # Create arrays from the vectors
    array1 <- array(vector1, dim = c(3, 2))
    array2 <- array(vector2, dim = c(3, 2))

    # Perform addition and subtraction
    addition_result <- array1 + array2
    subtraction_result <- array1 - array2

    # Print the results
    print("Array 1:")
    print(array1)
    print("Array 2:")
    print(array2)
    print("Addition of Arrays:")
    print(addition_result)
    print("Subtraction of Arrays:")
    print(subtraction_result)

4. Save the Script:
Save the script by going to File > Save, or press Ctrl + S (Windows). Name the file something like array_operations.R.

5. Run the Script:
You can run the script by clicking the Source button at the top of the script editor, or by selecting the entire script and pressing Ctrl + Enter (Windows).

No comments:

Post a Comment