从数据框的第一行创建数据框的标题 [英] Create header of a dataframe from the first row in the data frame

查看:93
本文介绍了从数据框的第一行创建数据框的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据框:

a <- data.frame(a=c("f", 2, 3), b=c("g", 3, 7), c=c("h", 2, 4))

,我想从第一行创建列名.我的猜测是:

and I would like to create column names from the first row. MY guess was:

names(a) <- a[1,]

给出:

names(a)
[1] "3" "3" "3"

我没有完全了解正在发生的事情.谁能解释和帮助我正确做事的方式?

I did not fully get what is happening. Can anyone explain and help me on how to do it the right way?

推荐答案

a的列是因子,每列具有不同的级别. R将它们转换为整数.因为,对于每一列,字母最后按字母数字顺序出现,因此为其分配了值3.

The columns of a are factors with each column having different levels. R casts them to ints. Since, for each column the letter appears last alphanumerically it gets assigned the value 3.

尝试

names(a) = as.character(unlist(a[1,]))
names(a)

这篇关于从数据框的第一行创建数据框的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆