Slip No.8
1) Write an R program to create a data frame using two given vectors and display the duplicated elements and unique rows of the said data frame.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
vector1 <- c(1, 2, 3, 4, 5, 3, 2)
vector2 <- c("A", "B", "C", "D", "E", "C", "B")
# Create a data frame using the two vectors
data_frame <- data.frame(Column1 = vector1, Column2 = vector2)
# Display the original data frame
print("Original Data Frame:")
print(data_frame)
# Find and display duplicated elements
duplicated_elements <- data_frame[duplicated(data_frame), ]
print("Duplicated Elements:")
print(duplicated_elements)
# Find and display unique rows
unique_rows <- unique(data_frame)
print("Unique Rows:")
print(unique_rows)
Save the script by going to File > Save, or press Ctrl + S (Windows). Name the file something like duplicated_unique_elements.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