开始使用免费开始使用

指定列名

您可能已经注意到,hotdogs.txt 文件没有列名,因此 R 为这些列分配了默认的、随意的名称。

为使数据更易理解、也更便于使用,您可以在将文件读入 R 时指定合适的列名。

head(hotdogs)
    V1  V2  V3
1 Beef 186 495
2 Beef 181 477
3 Beef 176 425
4 Beef 149 322
5 Beef 184 482
6 Beef 190 587

本练习是课程的一部分

R 数据导入入门

查看课程

练习说明

  • 完成 read.delim() 调用,将列名指定为 "type"、"calories" 和 "sodium"。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Finish the read.delim() call
hotdogs <- read.delim("hotdogs.txt", header = FALSE, col.names = ___)

# Print the first 6 rows of hotdogs
head(hotdogs)
编辑并运行代码