根据R中数据帧中的现有行分配头文件 [英] assign headers based on existing row in dataframe in R

查看:143
本文介绍了根据R中数据帧中的现有行分配头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转换数据帧后,我想根据现有行将列/名称分配给列。
我的标题目前是:
row.names X2 X3 X4 X5 X6 X7 X8 X9 ...
我想摆脱那个,并将以下行用作列标题(没有为了输出它们,因为我有很多)。



我唯一的解决方案是导出和重新加载数据(使用header = T)。 p>

任何帮助非常感谢
感谢

解决方案

尝试这个:

  colnames(DF)= DF [1,]#第一行将是头
DF = DF [-1,]#删除第一行。

但是,如果数据已正确读取,请查看。如果data.frame具有数字变量,但第一行是字符,则所有数据都被读取为字符。为了避免这个问题,最好保存数据并再次使用header = TRUE再次阅读。


After transforming a dataframe, I would like to assign heads/names to the columns based on an existing row. My headers are currently: row.names X2 X3 X4 X5 X6 X7 X8 X9 ... I would like to get rid of that and use the following row as column headers (without having to type them out since I have many).

The only solution I have for this is to export and re-load the data (with header=T).

Any help is greatly appreciated thanks

解决方案

Try this:

colnames(DF) = DF[1, ] # the first row will be the header
DF = DF[-1, ]          # removing the first row.

However, get a look if the data has been properly read. If you data.frame has numeric variables but the first row were characters, all the data has been read as character. To avoid this problem, it's better to save the data and read again with header=TRUE as you suggest.

这篇关于根据R中数据帧中的现有行分配头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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