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 a DataFrame with details of 5 employees
employees <- data.frame(
ID = c(101, 102, 103, 104, 105),
Name = c("Alice", "Bob", "Charlie", "David", "Eva"),
Age = c(28, 34, 23, 45, 30),
Department = c("HR", "Finance", "IT", "Marketing", "Sales"),
Salary = c(50000, 60000, 55000, 70000, 65000)
)
# Display the original DataFrame
print("Original DataFrame:")
print(employees)
# Sort the DataFrame in ascending order by ID
sorted_employees <- employees[order(employees$ID), ]
# Display the sorted DataFrame
print("Sorted DataFrame (by ID):")
print(sorted_employees)
Save the script by going to File > Save, or press Ctrl + S (Windows). Name the file something like employee_details.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