data table R select several columns
# Load the data.table library
library(data.table)
# Create a sample data.table (replace this with your actual data)
dt <- data.table(
ID = c(1, 2, 3, 4, 5),
Name = c("Alice", "Bob", "Charlie", "David", "Eva"),
Age = c(25, 30, 22, 35, 28),
Salary = c(50000, 60000, 45000, 70000, 55000)
)
# Select multiple columns using the `[, .()` syntax
selected_columns <- dt[, .(ID, Name, Salary)]
# Print the result
print(selected_columns)
This code snippet demonstrates how to use the data.table library in R to select specific columns from a data.table named dt
. In this example, the columns selected are "ID," "Name," and "Salary." Adjust the column names inside the .()
to match your specific requirements.