Page 241 - Data Science class 11
P. 241
6.11.5 extracting Data from Data Frame
Extract a specific column from a data frame using the column name.
Example
Enter the following code snippet:
# Creating the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Robert","Dharam","Mini","Sharan","Garima"),
salary = c(20000.5,35000.2,60000.0,70000.0,84253.25),
start_date = as.Date(c("2011-01-21", "2013-11-03", "2014-01-25", "2014-07-21",
"2015-05-17")),
stringsAsFactors = FALSE
)
# Extract Specific columns.
result <- data.frame(emp.data$emp_name,emp.data$salary)
print(result)
On execution of the above code, the following result is shown:
Extracting the first two rows and then all columns
Example
Enter the following code snippet:
# Creating the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Robert","Dharam","Mini","Sharan","Garima"),
salary = c(20000.5,35000.2,60000.0,70000.0,84253.25),
start_date = as.Date(c("2011-01-21", "2013-11-03", "2014-01-25", "2014-07-21",
"2015-05-17")), stringsAsFactors = FALSE)
Programming with R 239

