r - 在 R 读取的数据文件中分配列名

标签 r data-mining data-science

这个问题在这里已经有了答案:





Changing column names of a data frame

(18 个回答)


5年前关闭。




我正在尝试在 R 中读取网络数据(ID 图)。该文件名为“network.txt”,数据如下:

4 0
5 0
6 0
7 0
8 0
9 0
4029 1
4030 1
4031 1
4032 1
4033 1
19088 9040
19089 9040
19090 9040
19091 9040
19092 9040
19093 9040
19094 9040
19095 9040
19096 9040
19097 9040

而且,我正在使用 read.table() 模块阅读它。
data = read.table("network.txt",sep="\t",header=FALSE)
colnames( data ) <- unlist(c('to', 'from'))

Error in `colnames<-`(`*tmp*`, value = c("to", "from")) : 
  'names' attribute [2] must be the same length as the vector [1]

那么,如何分配列名呢?读取原始数据文件有错误吗?

最佳答案

您可以在 read.table 中提供列名。函数调用,例如:
read.table("network.txt", col.names = c("Col1", "Col2"))
或者,您也可以采用与 names 类似的方式进行操作。功能:

test1 <- read.table("Question1.txt")
names(test1) <- c("col1", "col2")

关于r - 在 R 读取的数据文件中分配列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711070/

相关文章:

c++ - 基于非密度的数据聚类算法

pattern-matching - 交易预测 : Which algorithm?

python - 使用 Pandas 和 Scatter_Matrix 将不会显示

python - 查找每个唯一 ID 列中特定值之前的索引列表

r - R fpc包的安装

readHTMLTables -- 检索与政府首脑相关的文章的国家名称和 url

r - 用样条线连接点

r - 当使用 `scale_x_log10` 时,如何将 `geom_text` 准确映射到 `geom_bin2d` ?

machine-learning - 如何通过简单的模拟模型生成人工数据集,用于具有二元响应和 4-5 个特征的分类分析?

python - 如何可视化作者分布?