fread:更高级的用法
既然您已经了解了 fread() 的基础用法,还需要认识该函数的两个参数:drop 和 select,用来丢弃或选择关心的变量。
假设您有一个包含 5 个变量的数据集,想只保留第 1 个和第 5 个变量,名称分别为 "a" 和 "e"。下面这些写法都可以实现:
fread("path/to/file.txt", drop = 2:4)
fread("path/to/file.txt", select = c(1, 5))
fread("path/to/file.txt", drop = c("b", "c", "d"))
fread("path/to/file.txt", select = c("a", "e"))
我们继续用土豆举例,因为在 DataCamp 我们对它格外钟爱。数据仍保存在 potatoes.csv 文件中(查看),其中包含以逗号分隔的记录。
本练习是课程的一部分
R 数据导入入门
练习说明
- 使用
fread(),并通过参数select或drop,只导入平面文件中的texture和moistness两列。它们在"potatoes.csv"中对应第 6 列和第 8 列。将结果保存到变量potatoes。 - 使用
plot()绘制potatoes数据框中的 2 列:x 轴为texture,y 轴为moistness。请使用两次美元符号表示法。您也可以为坐标轴和图形命名。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import columns 6 and 8 of potatoes.csv: potatoes
potatoes <- ___
# Plot texture (x) and moistness (y) of potatoes
___