简体   繁体   English

为ar数据框分配一个列名

[英]Assign a single column name to a r data frame

Please pardon my ignorance ,Since i am beginner in R 因为我是R语言的初学者,请原谅我的无知

In R i am transposing a dataframe (certain rows to columns) and saving the result back to a data frame which is exactly what i need . 在R中,我正在转换数据帧(将某些行转换为列),然后将结果保存回正是我需要的数据帧。 But the column name for the 1st column is missing which i need to join it with other data frames . 但是缺少第一列的列名,我需要将其与其他数据框连接起来。

Data frame result and function used 数据框结果和使用的功能

dish_pair<-as.data.frame.matrix(xtabs(count~primary_id+subcategory_name, dishes))

output 产量

But How can i get the 1st column name as primary_id 但是我如何获得第一列名称作为primary_id
which are holding row values 50792 ,50793 持有行值50792,50793

(I just need the 1st column name value as primary_id ,renaming data frame values are correct) (我只需要第一列名称值作为primary_id,重命名数据帧值是正确的)

The first column on the picture is just the row names. 图片上的第一列只是行名。 You need to add it to the data frame and give the column a name: 您需要将其添加到数据框中并为列命名:

 dish_pair <- data.frame(primary_id=rownames(dish_pair), dish_pair)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM