emp_id emp_name salary start_date
1 1 Rick 623.30 2019-01-01
2 2 Dan 515.20 2020-09-23
3 3 Michelle 611.00 2014-11-15
4 4 Ryan 729.00 2014-05-11
5 5 Gary 843.25 2015-03-27
获取数据帧的结构
通过使用str()函数可以看到数据帧的结构。
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2019-01-01","2020-09-23","2014-11-15","2014-05-11","2015-03-27")),
stringsAsFactors =FALSE)# Get the structure of the data frame.
str(emp.data)
emp_id emp_name salary start_date
Min. :1 Length:5 Min. :515.2 Min. :2019-01-01
1st Qu.:2 Class :character 1st Qu.:611.0 1st Qu.:2020-09-23
Median :3 Mode :character Median :623.3 Median :2014-05-11
Mean :3 Mean :664.4 Mean :2014-01-14
3rd Qu.:4 3rd Qu.:729.0 3rd Qu.:2014-11-15
Max. :5 Max. :843.2 Max. :2015-03-27
从数据帧中提取数据
使用列名从数据帧中提取特定的列。
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2019-01-01","2020-09-23","2014-11-15","2014-05-11","2015-03-27")),
stringsAsFactors =FALSE)# Extract Specific columns.
result <- data.frame(emp.data$emp_name,emp.data$salary)
print(result)